Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CodeBuildで動かすecspresso

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 CodeBuildで動かすecspresso

Avatar for shonansurvivors

shonansurvivors

August 08, 2023
Tweet

More Decks by shonansurvivors

Other Decks in Technology

Transcript

  1. 自己紹介 株式会社スマートラウンド SRE/コーポレートITチーム エンジニアリングマネージャー 山原 崇史 (やまはら たかし)  経歴等  ・SIer

    → 銀行 → Web系ベンチャー数社 → 現職  ・2023 Japan AWS Top Engineers(Software)  ・AWS Startup Community Core Member 好きなAWSサービス  IAM Identity Center / Security Hub shonansurvivors
  2. ecspressoをCodeBuildにインストールして実行する buildspecの例 version: 0.2 env: variables: ECSPRESSO_VERSION: 2.2.2 phases: install:

    commands: - curl -sL -O https://github.com/kayac/ecspresso/releases/download/ v${ECSPRESSO_VERSION}/ecspresso_${ECSPRESSO_VERSION}_linux_amd64.tar.gz - tar xzvf ecspresso_${ECSPRESSO_VERSION}_linux_amd64.tar.gz - sudo install ecspresso /usr/local/bin/ecspresso - ecspresso version # 略 build: commands: - ecspresso deploy
  3. GitHub Actionsのアクションを使う場合 よりシンプルに記述できる version: 0.2 phases: # 略 build: steps:

    - uses: kayac/ecspresso@v2 # ecspressoのインストールのみを行うアクション with: version: v2.2.2 - run: ecspresso deploy
  4. CodeBuildの各phaseにおける処理内容について phaseごとに推奨される処理内容がある (お作法のようなもの) phase ビルドを行う場合 テストを行う場合 デプロイを行う場合(※) install 各種パッケージのインストール等 各種パッケージのインストール等

    各種パッケージのインストール等 pre_build ・依存関係のインストール ・ECRへのログイン 等 依存関係のインストール 依存関係のインストール build ビルドの実行 テストの実行 デプロイの実行 post_build ・アーティファクトのパッケージ ・イメージをECRにプッシュ 等 - - ※デプロイに関しては AWS公式ドキュメントに記載は無く、筆者の独自解釈
  5. アクションを使う場合はphase分けしない 以下のようにphaseを分けるとエラーになるので注意 version: 0.2 phases: install: steps: - uses: kayac/ecspresso@v2

    # ecspressoのインストール with: version: v2.2.2 # 略 build: steps: - run: ecspresso deploy # command not foundになってしまう
  6. ecspressoが管理するファイル ecspressoではECSサービスやタスク定義をファイルで管理する . └── ecspresso ├── buildspec.yml ├── ecspresso.yml #

    ecspresso全体の設定ファイル ├── ecs-service-def.json # ECSサービスの定義 └── ecs-task-def.json # タスク定義
  7. 関連AWSリソースのARNやIDの取得について ECSサービスの定義ファイル (ecs-service-def.json)には以下の指定が必要 • ターゲットグループの ARN : arn:aws:elasticloadbalancing:region:account_id:targetgroup/name/xxxxxxxxxxxxxxxxxx • サブネットID

    : subnet-xxxxxxxxxxxxxxxxx • セキュリティグループ ID: sg-xxxxxxxxxxxxxxxx 👉こうした値を定義ファイルに直接記述すると可読性やメンテナンス性が落ちる懸念がある
  8. あるWebサービスの例で考える . ├── ... ├── batch ├── db ├── ...

    └── web ├── buildspec.yml # ビルド用 ├── ... └── ecspresso ├── buildspec.yml # デプロイ用 ├── ecspresso.yml ├── ecs-service-def.json └── ecs-task-def.json . ├── ... └── some_service ├── alb.tf ├── codebuild.tf ├── codebuild_iam.tf ├── ecs.tf ├── ecs_iam.tf ├── ... └── vpc.tf アプリケーションのリポジトリ Terraformのリポジトリ
  9. ビルドプロジェクトの環境変数経由で取得する ビルドプロジェクトの環境変数に他リソースの属性をセットし、さらに ECS定義ファイルで参照させる resource "aws_codebuild_project" "deploy_webapp" { # 略 environment

    { # 略 environment_variable { name = "TARGET_GROUP_ARN" value = aws_lb_target_group.webapp.arn type = "PLAINTEXT" } # 略 } # 略 } # ecs-service-def.jsonより抜粋 { # 略 "loadBalancers": [ { "containerName": "nginx", "containerPort": 80, "targetGroupArn": "{{ must_env `TARGET_GROUP_ARN` }}" } ], # 略 }
  10. 当初考えた案 「ecspresso rollback」を実行するbuildspecのファイルを用意しておく(実行は手動を想定) . ├── ... ├── db ├── ...

    └── web ├── buildspec.yml # ビルド用 ├── ... └── ecspresso ├── buildspec.yml # デプロイ用 ├── buildspec_rollback.yml # ロールバック用 ├── ecspresso.yml ├── ecs-service-def.json └── ecs-task-def.json アプリケーションのリポジトリ version: 0.2 # 略 phases: build: commands: # 略 - ecspresso rollback