From 56639254f21227e26d59230c12eaf0e3872615af Mon Sep 17 00:00:00 2001 From: kobayashi-m42 Date: Fri, 20 Sep 2024 22:29:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?lgtm-cat-processor=E3=81=AE=E3=83=87?= =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=A4=E7=94=A8=E3=81=AEIAM=20Role?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../files/policy.json | 35 +++++++++++++++++ .../lgtm-cat-processor-deploy-role/role.tf | 38 +++++++++++++++++++ .../variables.tf | 6 +++ .../aws/environments/prod/15-iam/main.tf | 6 +++ 4 files changed, 85 insertions(+) create mode 100644 modules/aws/iam/lgtm-cat-processor-deploy-role/files/policy.json create mode 100644 modules/aws/iam/lgtm-cat-processor-deploy-role/role.tf create mode 100644 modules/aws/iam/lgtm-cat-processor-deploy-role/variables.tf diff --git a/modules/aws/iam/lgtm-cat-processor-deploy-role/files/policy.json b/modules/aws/iam/lgtm-cat-processor-deploy-role/files/policy.json new file mode 100644 index 0000000..ebeb2da --- /dev/null +++ b/modules/aws/iam/lgtm-cat-processor-deploy-role/files/policy.json @@ -0,0 +1,35 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "ecr:GetAuthorizationToken" + ], + "Resource": "*" + }, + { + "Effect": "Allow", + "Action": [ + "ecr:BatchCheckLayerAvailability", + "ecr:CompleteLayerUpload", + "ecr:InitiateLayerUpload", + "ecr:PutImage", + "ecr:UploadLayerPart", + "ecr:GetDownloadUrlForLayer", + "ecr:BatchGetImage" + ], + "Resource": "arn:aws:ecr:${region}:${account_id}:repository/*" + }, + { + "Effect":"Allow", + "Action":[ + "codebuild:StartBuild", + "codebuild:BatchGetBuilds" + ], + "Resource":[ + "arn:aws:codebuild:${region}:${account_id}:project/*" + ] + } + ] +} diff --git a/modules/aws/iam/lgtm-cat-processor-deploy-role/role.tf b/modules/aws/iam/lgtm-cat-processor-deploy-role/role.tf new file mode 100644 index 0000000..1fb1312 --- /dev/null +++ b/modules/aws/iam/lgtm-cat-processor-deploy-role/role.tf @@ -0,0 +1,38 @@ +data "aws_iam_policy_document" "assume_role" { + statement { + effect = "Allow" + actions = ["sts:AssumeRoleWithWebIdentity"] + principals { + type = "Federated" + identifiers = [var.github_actions_oidc_provider_arn] + } + condition { + test = "StringEquals" + variable = "token.actions.githubusercontent.com:aud" + values = ["sts.amazonaws.com"] + } + condition { + test = "StringLike" + variable = "token.actions.githubusercontent.com:sub" + values = ["repo:nekochans/lgtm-cat-processor:*"] + } + } +} + +resource "aws_iam_role" "deploy" { + name = "lgtm-cat-processor-deploy-role" + assume_role_policy = data.aws_iam_policy_document.assume_role.json +} + +resource "aws_iam_policy" "deploy" { + name = "lgtm-cat-processor-deploy-policy" + policy = templatefile("${path.module}/files/policy.json", { + region = data.aws_region.current.name + account_id = data.aws_caller_identity.current.account_id + }) +} + +resource "aws_iam_role_policy_attachment" "deploy" { + role = aws_iam_role.deploy.name + policy_arn = aws_iam_policy.deploy.arn +} diff --git a/modules/aws/iam/lgtm-cat-processor-deploy-role/variables.tf b/modules/aws/iam/lgtm-cat-processor-deploy-role/variables.tf new file mode 100644 index 0000000..7b768d4 --- /dev/null +++ b/modules/aws/iam/lgtm-cat-processor-deploy-role/variables.tf @@ -0,0 +1,6 @@ +variable "github_actions_oidc_provider_arn" { + type = string +} + +data "aws_region" "current" {} +data "aws_caller_identity" "current" {} diff --git a/providers/aws/environments/prod/15-iam/main.tf b/providers/aws/environments/prod/15-iam/main.tf index d617a89..1294f63 100644 --- a/providers/aws/environments/prod/15-iam/main.tf +++ b/providers/aws/environments/prod/15-iam/main.tf @@ -12,3 +12,9 @@ module "api_deploy_role" { github_actions_oidc_provider_arn = module.identity_provider.github_actions_oidc_provider_arn } + +module "lgtm_cat_processor_deploy_role" { + source = "../../../../../modules/aws/iam/lgtm-cat-processor-deploy-role" + + github_actions_oidc_provider_arn = module.identity_provider.github_actions_oidc_provider_arn +} From 24f2ca9accc2888c404ffe5abe50f36eca0a518f Mon Sep 17 00:00:00 2001 From: kobayashi-m42 Date: Sat, 21 Sep 2024 14:17:09 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Lambda=E3=81=AEupdate=E3=82=92=E8=A1=8C?= =?UTF-8?q?=E3=81=86=E3=81=9F=E3=82=81=E3=81=AECodeBuild=E3=82=92=E6=A7=8B?= =?UTF-8?q?=E7=AF=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aws/lgtm-image-processor/cloudwatch.tf | 2 +- .../codebuild/codebuild.tf | 36 +++++++++++++++ .../aws/lgtm-image-processor/codebuild/iam.tf | 44 +++++++++++++++++++ .../codebuild/variables.tf | 15 +++++++ modules/aws/lgtm-image-processor/iam.tf | 2 +- modules/aws/lgtm-image-processor/lambda.tf | 2 +- modules/aws/lgtm-image-processor/variables.tf | 8 ++-- .../.terraform.lock.hcl | 21 +++++++-- .../stg/22-lgtm-image-processor/main.tf | 10 +++++ .../stg/22-lgtm-image-processor/variables.tf | 2 + .../stg/22-lgtm-image-processor/versions.tf | 2 +- 11 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 modules/aws/lgtm-image-processor/codebuild/codebuild.tf create mode 100644 modules/aws/lgtm-image-processor/codebuild/iam.tf create mode 100644 modules/aws/lgtm-image-processor/codebuild/variables.tf diff --git a/modules/aws/lgtm-image-processor/cloudwatch.tf b/modules/aws/lgtm-image-processor/cloudwatch.tf index 92b84d8..65d4e6b 100644 --- a/modules/aws/lgtm-image-processor/cloudwatch.tf +++ b/modules/aws/lgtm-image-processor/cloudwatch.tf @@ -1,4 +1,4 @@ resource "aws_cloudwatch_log_group" "lgtm_image_processor" { - name = "/aws/lambda/${local.lambda_function_name}" + name = "/aws/lambda/${var.lambda_function_name}" retention_in_days = var.log_retention_in_days } diff --git a/modules/aws/lgtm-image-processor/codebuild/codebuild.tf b/modules/aws/lgtm-image-processor/codebuild/codebuild.tf new file mode 100644 index 0000000..f5602e4 --- /dev/null +++ b/modules/aws/lgtm-image-processor/codebuild/codebuild.tf @@ -0,0 +1,36 @@ +resource "aws_codebuild_project" "codebuild" { + name = "${var.env}-${var.service_name}-deploy" + build_timeout = 5 + service_role = aws_iam_role.codebuild.arn + + environment { + compute_type = "BUILD_LAMBDA_1GB" + type = "ARM_LAMBDA_CONTAINER" + image = "aws/codebuild/amazonlinux-aarch64-lambda-standard:python3.12" + image_pull_credentials_type = "CODEBUILD" + } + + # リポジトリで設定するので何もしない + source { + type = "NO_SOURCE" + buildspec = <