|
| 1 | +data "aws_region" "current" {} |
| 2 | + |
| 3 | +locals { |
| 4 | + github_actions_apps = { |
| 5 | + "forms-admin" = { |
| 6 | + ecr_repository_arn = module.forms_admin_container_repo.arn |
| 7 | + github_repository = "alphagov/forms-admin" |
| 8 | + } |
| 9 | + "forms-runner" = { |
| 10 | + ecr_repository_arn = module.forms_runner_container_repo.arn |
| 11 | + github_repository = "alphagov/forms-runner" |
| 12 | + } |
| 13 | + "forms-product-page" = { |
| 14 | + ecr_repository_arn = module.forms_product_page_container_repo.arn |
| 15 | + github_repository = "govuk-forms/forms-product-page" |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +# S3 bucket for CodeBuild artifacts (terraform outputs) |
| 21 | +module "codebuild_artifacts" { |
| 22 | + source = "../../../modules/secure-bucket" |
| 23 | + |
| 24 | + name = "forms-review-codebuild-artifacts" |
| 25 | + versioning_enabled = false |
| 26 | + access_logging_enabled = false |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_s3_bucket_lifecycle_configuration" "codebuild_artifacts" { |
| 30 | + bucket = module.codebuild_artifacts.name |
| 31 | + |
| 32 | + rule { |
| 33 | + id = "expire-old-artifacts" |
| 34 | + status = "Enabled" |
| 35 | + |
| 36 | + filter {} |
| 37 | + |
| 38 | + expiration { |
| 39 | + days = 1 |
| 40 | + } |
| 41 | + |
| 42 | + abort_incomplete_multipart_upload { |
| 43 | + days_after_initiation = 1 |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +# CodeBuild projects for each app |
| 49 | +module "codebuild_deploy" { |
| 50 | + for_each = local.github_actions_apps |
| 51 | + source = "./review-app-codebuild" |
| 52 | + |
| 53 | + application_name = each.key |
| 54 | + action = "deploy" |
| 55 | + github_repository = "https://github.com/${each.value.github_repository}" |
| 56 | + codeconnection_arn = data.terraform_remote_state.account.outputs.codeconnection_arn |
| 57 | + artifacts_bucket_name = module.codebuild_artifacts.name |
| 58 | + ecs_cluster_arn = aws_ecs_cluster.review.arn |
| 59 | + ecs_cluster_name = aws_ecs_cluster.review.name |
| 60 | + ecr_repository_arn = each.value.ecr_repository_arn |
| 61 | + task_execution_role_arn = aws_iam_role.ecs_execution.arn |
| 62 | + autoscaling_role_arn = aws_iam_service_linked_role.app_autoscaling.arn |
| 63 | + deploy_account_id = var.deploy_account_id |
| 64 | +} |
| 65 | + |
| 66 | +module "codebuild_destroy" { |
| 67 | + for_each = local.github_actions_apps |
| 68 | + source = "./review-app-codebuild" |
| 69 | + |
| 70 | + application_name = each.key |
| 71 | + action = "destroy" |
| 72 | + github_repository = "https://github.com/${each.value.github_repository}" |
| 73 | + codeconnection_arn = data.terraform_remote_state.account.outputs.codeconnection_arn |
| 74 | + artifacts_bucket_name = module.codebuild_artifacts.name |
| 75 | + ecs_cluster_arn = aws_ecs_cluster.review.arn |
| 76 | + ecs_cluster_name = aws_ecs_cluster.review.name |
| 77 | + ecr_repository_arn = each.value.ecr_repository_arn |
| 78 | + task_execution_role_arn = aws_iam_role.ecs_execution.arn |
| 79 | + autoscaling_role_arn = aws_iam_service_linked_role.app_autoscaling.arn |
| 80 | + deploy_account_id = var.deploy_account_id |
| 81 | +} |
| 82 | + |
| 83 | +# OIDC roles with minimal permissions (trigger CodeBuild + push to ECR) |
| 84 | +data "aws_iam_policy_document" "github_actions_assume_role" { |
| 85 | + for_each = local.github_actions_apps |
| 86 | + |
| 87 | + statement { |
| 88 | + effect = "Allow" |
| 89 | + actions = ["sts:AssumeRoleWithWebIdentity"] |
| 90 | + |
| 91 | + principals { |
| 92 | + type = "Federated" |
| 93 | + identifiers = [data.terraform_remote_state.account.outputs.github_oidc_provider_arn] |
| 94 | + } |
| 95 | + |
| 96 | + condition { |
| 97 | + test = "StringEquals" |
| 98 | + variable = "token.actions.githubusercontent.com:aud" |
| 99 | + values = ["sts.amazonaws.com"] |
| 100 | + } |
| 101 | + |
| 102 | + condition { |
| 103 | + test = "StringLike" |
| 104 | + variable = "token.actions.githubusercontent.com:sub" |
| 105 | + values = [ |
| 106 | + "repo:${each.value.github_repository}:pull_request" |
| 107 | + ] |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +resource "aws_iam_role" "github_actions" { |
| 113 | + for_each = local.github_actions_apps |
| 114 | + |
| 115 | + name = "review-github-actions-${each.key}" |
| 116 | + description = "Role assumed by GitHub Actions workflows for ${each.key} review apps" |
| 117 | + |
| 118 | + assume_role_policy = data.aws_iam_policy_document.github_actions_assume_role[each.key].json |
| 119 | +} |
| 120 | + |
| 121 | +resource "aws_iam_role_policy" "github_actions" { |
| 122 | + for_each = local.github_actions_apps |
| 123 | + |
| 124 | + role = aws_iam_role.github_actions[each.key].name |
| 125 | + policy = data.aws_iam_policy_document.github_actions[each.key].json |
| 126 | +} |
| 127 | + |
| 128 | +data "aws_iam_policy_document" "github_actions" { |
| 129 | + for_each = local.github_actions_apps |
| 130 | + |
| 131 | + # Trigger CodeBuild projects |
| 132 | + statement { |
| 133 | + sid = "TriggerCodeBuild" |
| 134 | + actions = ["codebuild:StartBuild", "codebuild:BatchGetBuilds"] |
| 135 | + resources = [ |
| 136 | + module.codebuild_deploy[each.key].project_arn, |
| 137 | + module.codebuild_destroy[each.key].project_arn |
| 138 | + ] |
| 139 | + } |
| 140 | + |
| 141 | + # Read CodeBuild logs |
| 142 | + statement { |
| 143 | + sid = "ReadCodeBuildLogs" |
| 144 | + actions = ["logs:GetLogEvents"] |
| 145 | + resources = [ |
| 146 | + module.codebuild_deploy[each.key].log_group_arn, |
| 147 | + module.codebuild_destroy[each.key].log_group_arn |
| 148 | + ] |
| 149 | + } |
| 150 | + |
| 151 | + # Push container images to ECR |
| 152 | + statement { |
| 153 | + sid = "PushToECR" |
| 154 | + actions = [ |
| 155 | + "ecr:BatchCheckLayerAvailability", |
| 156 | + "ecr:CompleteLayerUpload", |
| 157 | + "ecr:InitiateLayerUpload", |
| 158 | + "ecr:PutImage", |
| 159 | + "ecr:UploadLayerPart" |
| 160 | + ] |
| 161 | + resources = [each.value.ecr_repository_arn] |
| 162 | + } |
| 163 | + |
| 164 | + statement { |
| 165 | + sid = "ECRLogin" |
| 166 | + actions = ["ecr:GetAuthorizationToken"] |
| 167 | + resources = ["*"] |
| 168 | + } |
| 169 | + |
| 170 | + # Wait for ECS service stability (read-only) |
| 171 | + statement { |
| 172 | + sid = "WaitForECSStability" |
| 173 | + actions = ["ecs:DescribeServices"] |
| 174 | + resources = [ |
| 175 | + "arn:aws:ecs:${data.aws_region.current.id}:${data.aws_caller_identity.current.account_id}:service/${aws_ecs_cluster.review.name}/${each.key}-pr-*" |
| 176 | + ] |
| 177 | + } |
| 178 | + |
| 179 | + # Read and delete CodeBuild artifacts (terraform outputs) |
| 180 | + statement { |
| 181 | + sid = "ReadArtifacts" |
| 182 | + actions = ["s3:GetObject", "s3:DeleteObject"] |
| 183 | + resources = [ |
| 184 | + "${module.codebuild_artifacts.arn}/*/review-${each.key}-deploy/outputs.json" |
| 185 | + ] |
| 186 | + } |
| 187 | +} |
0 commit comments