resource "aws_s3_bucket" "function_bucket" { bucket_prefix = "function-bucket" } resource "aws_s3_bucket_server_side_encryption_configuration" "aes256" { bucket = aws_s3_bucket.function_bucket.id rule { apply_server_side_encryption_by_default { sse_algorithm = "AES256" } } } resource "aws_s3_bucket_policy" "function_bucket_policy" { count = var.pipeline.inputs.environment_account_ids != "" ? 1 : 0 policy = data.aws_iam_policy_document.function_bucket_policy_document.json bucket = aws_s3_bucket.function_bucket.id } resource "aws_codebuild_project" "build_project" { name = "${var.service.name}-build-project" service_role = aws_iam_role.publish_role.arn artifacts { type = "CODEPIPELINE" } environment { compute_type = "BUILD_GENERAL1_SMALL" image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0" type = "LINUX_CONTAINER" image_pull_credentials_type = "CODEBUILD" environment_variable { name = "bucket_name" value = aws_s3_bucket.function_bucket.bucket } environment_variable { name = "service_name" value = var.service.name } } source { buildspec = < yq_linux_amd64.sha", "wget https://github.com/mikefarah/yq/releases/download/3.4.0/yq_linux_amd64", "sha256sum -c yq_linux_amd64.sha", "mv yq_linux_amd64 /usr/bin/yq", "chmod +x /usr/bin/yq" ] }, "pre_build": { "commands": [ "cd $CODEBUILD_SRC_DIR/${var.pipeline.inputs.code_dir}", "${var.pipeline.inputs.unit_test_command}" ] }, "build": { "commands": [ "${var.pipeline.inputs.packaging_command}", "FUNCTION_KEY=$CODEBUILD_BUILD_NUMBER/function.zip", "aws s3 cp function.zip s3://$bucket_name/$FUNCTION_KEY" ] }, "post_build": { "commands": [ "aws proton --region $AWS_DEFAULT_REGION get-service --name $service_name | jq -r .service.spec > service.yaml", "yq w service.yaml 'instances[*].spec.lambda_bucket' \"$bucket_name\" > rendered_service_tmp.yaml", "yq w rendered_service_tmp.yaml 'instances[*].spec.lambda_key' \"$FUNCTION_KEY\" > rendered_service.yaml" ] } }, "artifacts": { "files": [ "${var.pipeline.inputs.code_dir}/rendered_service.yaml" ] } } EOF type = "CODEPIPELINE" } encryption_key = aws_kms_key.pipeline_artifacts_bucket_key.arn } resource "aws_codebuild_project" "deploy_project" { for_each = { for instance in var.service_instances : instance.name => instance } name = "deploy-${var.service.name}-${index(var.service_instances, each.value)}" service_role = aws_iam_role.deployment_role.arn artifacts { type = "CODEPIPELINE" } environment { compute_type = "BUILD_GENERAL1_SMALL" image = "aws/codebuild/amazonlinux2-x86_64-standard:3.0" type = "LINUX_CONTAINER" privileged_mode = false image_pull_credentials_type = "CODEBUILD" environment_variable { name = "service_instance_name" value = each.value.name } environment_variable { name = "service_name" value = var.service.name } } source { type = "CODEPIPELINE" buildspec = <