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/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 = <