2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TerragruntでCodeBuild上dockerイメージをビルドできる環境構築

Last updated at Posted at 2024-03-17

はじめに

本記事はAWS環境においてCodeBuildを使って、ビルド成功したimageをECRにpushする方法について記載しています。

構成

スクリーンショット 2024-03-16 14.31.21.png

目次

  1. ECRのTerraformコード作成
  2. ECRのTerragruntコード作成
  3. CodeBuildのTerragruntコード作成
  4. ECRのTerragruntコード作成
  5. CodeBuildのbuildspec.yml作成
  6. terragruntでコードをデプロイする

ECRのTerraformコード作成

ECR リポジトリのライフサイクルポリシーはaws_ecr_lifecycle_policyで管理します。
また、ecr_repositoryのリポジト名はterragrunt.hclのinputsから受け取る形にします。

aws/common/ecr/main.tf
resource "aws_ecr_repository" "this" {
  name                 = var.repository_name
  image_tag_mutability = "MUTABLE"

  encryption_configuration {
    encryption_type = "AES256"
  }

  image_scanning_configuration {
    scan_on_push = true
  }
}

resource "aws_ecr_lifecycle_policy" "policy" {
  repository = aws_ecr_repository.this.name

  policy = jsonencode({
    "rules" : [
      {
        "rulePriority" : 1,
        "description" : var.description,
        "selection" : {
          "tagStatus" : "any",
          "countType" : "imageCountMoreThan",
          "countNumber" : var.count_number
        },
        "action" : {
          "type" : "expire"
        }
      }
    ]
  })
}

aws/common/ecr/output.tf
output "ecr_repository" {
  value = aws_ecr_repository.this
}
aws/common/ecr/variables.tf
variable "repository_name" {
  type = string
}

variable "description" {
  type        = string
  description = "ecrライフサイクルルールの説明"
}

variable "count_number" {
  type        = number
  description = "ecr制限するタグ数"
}

ECRのTerragruntコード作成

aws/terragrunt/ecr/terragrunt.hcl
include "root" {
  path = find_in_parent_folders()
}

include "template" {
  // ecrのterraformのルートパス
  path = "../../common/ecr"
}

inputs = {
  // common/ecr/variables.tfの値はここで記入します
  repository_name = "repository_name"
  description     = "最後の5つのタグを保留する"
  count_number    = 5
}

CodeBuildのTerragruntコード作成

aws/terragrunt/codebuild/terragrunt.hcl
include "root" {
  path = find_in_parent_folders()
}

dependency "ecr" {
  config_path = "../ecr/dev"
}


inputs = {
  project_name            = "project_name"
  project_description     = "project_description"
  type                    = "ARM_CONTAINER"
  image                   = "aws/codebuild/amazonlinux2-aarch64-standard:3.0"
  build_timeout           = 120
  buildspec               = "codebuild/buildspec.yml"
  primary_repository_name = "primary_repository_name"
  ssm_github_token_name   = "ssm_github_token_name"
  privileged_mode         = true
  attach_iam_policie_names = ["AWSCodeBuildAdminAccess", "AmazonVPCFullAccess", "AmazonEC2ContainerRegistryFullAccess"]
  // vpcは必要に応じて別途作成
  vpc_id                   = vpc_id
  // security_groupは必要に応じて別途作成
  security_group_ids       = security_group_ids
    // subnetsは必要に応じて別途作成
  subnets                  = dependency.nw.outputs.main_subnet_private_ids
   // s3は必要に応じて別途作成
  bucket                   = bucket
  env_vars = {
    AWS_ACCOUNT_ID  = get_aws_account_id()
    IMAGE_REPO_NAME = dependency.ecr.outputs.ecr_repository.name
    IMAGE_TAG       = ""
    TARGET_DIR      = ""
  }
}


CodeBuildのbuildspec.yml作成

aws/codebuild/buildspec.yml
version: 0.2

phases:
  install:
    commands:
      # Terraformのインストール(バージョン確認)
      - |
        if ! terraform --version | grep "1.5.7"; then
          wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip
          sudo unzip -o terraform_1.5.7_linux_amd64.zip -d /usr/local/bin/
        fi
      - terraform --version

      # Terragruntのインストール(バージョン確認)
      - |
        if ! terragrunt --version | grep "v0.54.10"; then
          wget https://github.com/gruntwork-io/terragrunt/releases/download/v0.54.10/terragrunt_linux_amd64
          chmod +x terragrunt_linux_amd64
          sudo mv -f terragrunt_linux_amd64 /usr/local/bin/terragrunt
        fi
      - terragrunt --version

  pre_build:
    commands:
      - echo Pre-build phase...
      - cd ./
      - export BUILD_ROOT_DIR=$(pwd)

  build:
    commands:
      - echo Build phase...
      # Build
      - echo ----------------------------------
      - echo Build common
      - echo ----------------------------------
      - cd $BUILD_ROOT_DIR
      - terragrunt run-all $TERRAGRUNT_CMD --terragrunt-non-interactive

      - echo ----------------------------------
      - echo Build container
      - echo ----------------------------------
      - cd $BUILD_ROOT_DIR
      - terragrunt run-all $TERRAGRUNT_CMD --terragrunt-non-interactive --terragrunt-include-external-dependencies


cache:
  paths:
    - /usr/local/bin/**/*

terragruntでコードをデプロイする

terragruntインストール手順

terragruntでコードをデプロイ
terragrunt plan // コードが正しいか確認できる
terragrunt apply // コードをデプロイ

awsのcodebuildでデプロイしたビルドプロジェクトに対して、上書きでビルドを開始するボタンを押す。
aws/terragrunt/codebuild/terragrunt.hclのenv_varsが空欄の引数は以下の環境変数に入力し、ビルドを開始する。
スクリーンショット 2024-03-17 14.04.25.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?