Skip to content

Commit 94b8ebd

Browse files
Merge remote-tracking branch 'origin/main' into PRMP-939
2 parents 1f08be1 + 8167071 commit 94b8ebd

File tree

4 files changed

+270
-68
lines changed

4 files changed

+270
-68
lines changed

.github/workflows/deploy-pre-prod.yml

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ jobs:
4444
run: |
4545
echo Tag to deploy: ${{ steps.versioning.outputs.tag || github.event.inputs.branch_or_tag }}
4646
47-
terraform_plan_apply:
48-
name: Terraform Plan/Apply (pre-prod)
47+
terraform_plan_apply_base_iam:
48+
name: Terraform Plan/Apply base-iam (pre-prod)
4949
runs-on: ubuntu-latest
5050
needs: ["tag_main"]
5151
environment: pre-prod
@@ -56,43 +56,38 @@ jobs:
5656
ref: ${{ needs.tag_main.outputs.version }}
5757
fetch-depth: "0"
5858

59-
- name: Configure AWS Credentials
60-
uses: aws-actions/configure-aws-credentials@v5
61-
with:
62-
role-to-assume: ${{ secrets.AWS_ASSUME_ROLE }}
63-
role-skip-session-tagging: true
64-
aws-region: ${{ vars.AWS_REGION }}
65-
mask-aws-account-id: true
66-
67-
- name: Setup Terraform
68-
uses: hashicorp/setup-terraform@v3
59+
- name: Apply base_iam
60+
uses: ./.github/actions/tf-plan-apply
6961
with:
70-
terraform_version: 1.14.3
71-
terraform_wrapper: false
72-
73-
- name: Initialise Terraform
74-
id: init
75-
run: terraform init -backend-config=backend-pre-prod.conf
76-
working-directory: ./infrastructure
77-
shell: bash
62+
aws_assume_role: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/pre-prod-github-bootstrap
63+
bucket_prefix: "pre-prod"
64+
aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }}
65+
aws_region: ${{ vars.AWS_REGION }}
66+
working_directory: "./base_iam" # Use separate base_iam directory
67+
workspace: ${{ secrets.AWS_WORKSPACE }}
68+
tf_vars_file: ${{ vars.TF_VARS_FILE }}
69+
tf_extra_args: "-var aws_account_id=${{ secrets.AWS_ACCOUNT_ID }}"
7870

79-
- name: Select Terraform Workspace
80-
id: workspace
81-
run: terraform workspace select ${{ secrets.AWS_WORKSPACE }}
82-
working-directory: ./infrastructure
83-
shell: bash
8471

85-
- name: Check Terraform Formatting
86-
run: terraform fmt -check
87-
working-directory: ./infrastructure
88-
89-
- name: Run Terraform Plan
90-
id: plan
91-
run: |
92-
terraform plan -input=false -no-color -var-file="${{vars.TF_VARS_FILE}}" -out tf.plan
93-
working-directory: ./infrastructure
94-
shell: bash
72+
terraform_plan_apply:
73+
name: Terraform Plan/Apply infrastructure (pre-prod)
74+
runs-on: ubuntu-latest
75+
needs: ["tag_main", "terraform_plan_apply_base_iam"]
76+
environment: pre-prod
77+
steps:
78+
- name: Checkout main
79+
uses: actions/checkout@v6
80+
with:
81+
ref: ${{ needs.tag_main.outputs.version }}
82+
fetch-depth: "0"
9583

96-
- name: Run Terraform Apply
97-
run: terraform apply -auto-approve -input=false tf.plan
98-
working-directory: ./infrastructure
84+
- name: Apply Main
85+
uses: ./.github/actions/tf-plan-apply
86+
with:
87+
# use newly updated role
88+
aws_assume_role: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/pre-prod-github-actions-role
89+
bucket_prefix: "pre-prod"
90+
aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }}
91+
aws_region: ${{ vars.AWS_REGION }}
92+
workspace: ${{ secrets.AWS_WORKSPACE }}
93+
tf_vars_file: ${{ vars.TF_VARS_FILE }}

base_iam/iam_github_pre-prod.tf

Lines changed: 136 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# aws_iam_role.github_role_pre-prod[0]:
2-
resource "aws_iam_role" "github_role_pre_prod" {
1+
# aws_iam_role.pre_prod_github_actions[0]:
2+
resource "aws_iam_role" "pre_prod_github_actions" {
33
count = local.is_pre_production ? 1 : 0
4-
description = "This role is to provide access for GitHub actions to the pre-prod environment. "
4+
name = "${terraform.workspace}-github-actions-role"
5+
description = "This role is to provide access for GitHub Actions to the ${terraform.workspace} environment."
56
force_detach_policies = false
67
max_session_duration = 3600
7-
name = "Github-Actions-pre-prod-role"
88
name_prefix = null
99
path = "/"
1010
permissions_boundary = null
@@ -30,13 +30,6 @@ resource "aws_iam_role" "github_role_pre_prod" {
3030
Federated = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"
3131
}
3232
},
33-
{
34-
Action = "sts:AssumeRole"
35-
Effect = "Allow"
36-
Principal = {
37-
AWS = "arn:aws:sts::${data.aws_caller_identity.current.account_id}:assumed-role/AWSReservedSSO_DomainCGpit-Administrators_3f00be4c22ce78e5/ABKH2@hscic.gov.uk"
38-
}
39-
},
4033
]
4134
Version = "2012-10-17"
4235
}
@@ -48,7 +41,7 @@ resource "aws_iam_role" "github_role_pre_prod" {
4841

4942
resource "aws_iam_role_policy" "cloudfront_policy_pre_prod" {
5043
count = local.is_pre_production ? 1 : 0
51-
role = aws_iam_role.github_role_pre_prod[0].id
44+
role = aws_iam_role.pre_prod_github_actions[0].name
5245
name = "cloudfront_policy"
5346
policy = jsonencode(
5447
{
@@ -68,7 +61,7 @@ resource "aws_iam_role_policy" "cloudfront_policy_pre_prod" {
6861
"cloudfront:CreateInvalidation",
6962
"cloudfront:UpdateOriginAccessControl",
7063
"cloudfront:CreateOriginRequestPolicy",
71-
"cloudfront:UpdateOriginRequestPolicy"
64+
"cloudfront:UpdateOriginRequestPolicy",
7265
]
7366
Effect = "Allow"
7467
Resource = "*"
@@ -82,7 +75,7 @@ resource "aws_iam_role_policy" "cloudfront_policy_pre_prod" {
8275

8376
resource "aws_iam_role_policy" "cloudwatch_logs_policy_pre_prod" {
8477
count = local.is_pre_production ? 1 : 0
85-
role = aws_iam_role.github_role_pre_prod[0].id
78+
role = aws_iam_role.pre_prod_github_actions[0].name
8679
name = "cloudwatch_logs_policy"
8780
policy = jsonencode(
8881
{
@@ -112,7 +105,7 @@ resource "aws_iam_role_policy" "cloudwatch_logs_policy_pre_prod" {
112105

113106
resource "aws_iam_role_policy" "ecr_policy_pre_prod" {
114107
count = local.is_pre_production ? 1 : 0
115-
role = aws_iam_role.github_role_pre_prod[0].id
108+
role = aws_iam_role.pre_prod_github_actions[0].name
116109
name = "ecr_policy"
117110
policy = jsonencode(
118111
{
@@ -141,7 +134,7 @@ resource "aws_iam_role_policy" "ecr_policy_pre_prod" {
141134

142135
resource "aws_iam_role_policy" "ecs_policy_pre_prod" {
143136
count = local.is_pre_production ? 1 : 0
144-
role = aws_iam_role.github_role_pre_prod[0].id
137+
role = aws_iam_role.pre_prod_github_actions[0].name
145138
name = "ecs_policy"
146139
policy = jsonencode(
147140
{
@@ -163,7 +156,7 @@ resource "aws_iam_role_policy" "ecs_policy_pre_prod" {
163156

164157
resource "aws_iam_role_policy" "github_extended_policy_virus_scanner_pre_prod" {
165158
count = local.is_pre_production ? 1 : 0
166-
role = aws_iam_role.github_role_pre_prod[0].id
159+
role = aws_iam_role.pre_prod_github_actions[0].name
167160
name = "github-extended-policy-virus-scanner"
168161
policy = jsonencode(
169162
{
@@ -198,7 +191,7 @@ resource "aws_iam_role_policy" "github_extended_policy_virus_scanner_pre_prod" {
198191

199192
resource "aws_iam_role_policy" "lambda_pre_prod" {
200193
count = local.is_pre_production ? 1 : 0
201-
role = aws_iam_role.github_role_pre_prod[0].id
194+
role = aws_iam_role.pre_prod_github_actions[0].name
202195
name = "lambda"
203196
policy = jsonencode(
204197
{
@@ -234,7 +227,7 @@ resource "aws_iam_role_policy" "lambda_pre_prod" {
234227

235228
resource "aws_iam_role_policy" "mtls_gateway_pre_prod" {
236229
count = local.is_pre_production ? 1 : 0
237-
role = aws_iam_role.github_role_pre_prod[0].id
230+
role = aws_iam_role.pre_prod_github_actions[0].name
238231
name = "mtls-gateway"
239232
policy = jsonencode(
240233
{
@@ -302,7 +295,7 @@ resource "aws_iam_role_policy" "mtls_gateway_pre_prod" {
302295

303296
resource "aws_iam_role_policy" "resource_tagging_pre_prod" {
304297
count = local.is_pre_production ? 1 : 0
305-
role = aws_iam_role.github_role_pre_prod[0].id
298+
role = aws_iam_role.pre_prod_github_actions[0].name
306299
name = "resource_tagging"
307300
policy = jsonencode(
308301
{
@@ -437,23 +430,129 @@ resource "aws_iam_role_policy" "resource_tagging_pre_prod" {
437430
)
438431
}
439432

433+
resource "aws_iam_role_policy" "rum_policy_pre_prod" {
434+
count = local.is_pre_production ? 1 : 0
435+
role = aws_iam_role.pre_prod_github_actions[0].name
436+
name = "rum_policy"
437+
policy = jsonencode(
438+
{
439+
Statement = [
440+
{
441+
Action = [
442+
"cognito-identity:SetIdentityPoolRoles",
443+
"cognito-identity:CreateIdentityPool",
444+
"cognito-identity:DeleteIdentityPool",
445+
"cognito-identity:UpdateIdentityPool",
446+
]
447+
Effect = "Allow"
448+
Resource = "arn:aws:cognito-identity:eu-west-2:${data.aws_caller_identity.current.account_id}:identitypool/*"
449+
Sid = "AllowIdentityPool"
450+
},
451+
{
452+
Action = [
453+
"rum:TagResource",
454+
"rum:UntagResource",
455+
"rum:ListTagsForResource",
456+
"iam:PassRole",
457+
"rum:UpdateAppMonitor",
458+
"rum:GetAppMonitor",
459+
"rum:CreateAppMonitor",
460+
"rum:DeleteAppMonitor",
461+
]
462+
Effect = "Allow"
463+
Resource = "arn:aws:rum:eu-west-2:${data.aws_caller_identity.current.account_id}:appmonitor/*"
464+
Sid = "AllowAppMonitor"
465+
},
466+
{
467+
Action = [
468+
"logs:DeleteLogGroup",
469+
"logs:DeleteResourcePolicy",
470+
"logs:DescribeLogGroups",
471+
]
472+
Effect = "Allow"
473+
Resource = "arn:aws:logs:eu-west-2:${data.aws_caller_identity.current.account_id}:log-group:*RUMService*"
474+
Sid = "AllowRumServiceLogs"
475+
},
476+
{
477+
Action = [
478+
"logs:CreateLogDelivery",
479+
"logs:GetLogDelivery",
480+
"logs:UpdateLogDelivery",
481+
"logs:DeleteLogDelivery",
482+
"logs:ListLogDeliveries",
483+
"logs:DescribeResourcePolicies",
484+
]
485+
Effect = "Allow"
486+
Resource = "*"
487+
Sid = "AllowRumServiceAllLogs"
488+
},
489+
]
490+
Version = "2012-10-17"
491+
}
492+
)
493+
}
494+
495+
resource "aws_iam_role_policy" "scheduler_policy_pre_prod" {
496+
count = local.is_pre_production ? 1 : 0
497+
role = aws_iam_role.pre_prod_github_actions[0].name
498+
name = "scheduler_policy"
499+
policy = jsonencode(
500+
{
501+
Statement = [
502+
{
503+
Action = "scheduler:DeleteSchedule"
504+
Effect = "Allow"
505+
Resource = "*"
506+
Sid = "VisualEditor0"
507+
},
508+
]
509+
Version = "2012-10-17"
510+
}
511+
)
512+
}
513+
514+
resource "aws_iam_role_policy" "step_functions_pre_prod" {
515+
count = local.is_pre_production ? 1 : 0
516+
role = aws_iam_role.pre_prod_github_actions[0].name
517+
name = "step_functions"
518+
policy = jsonencode(
519+
{
520+
Statement = [
521+
{
522+
Action = [
523+
"states:DescribeStateMachine",
524+
"states:UpdateStateMachine",
525+
"states:DeleteStateMachine",
526+
"states:CreateStateMachine",
527+
"states:TagResource",
528+
"states:UntagResource",
529+
]
530+
Effect = "Allow"
531+
Resource = "arn:aws:states:eu-west-2:${data.aws_caller_identity.current.account_id}:stateMachine:*"
532+
Sid = "VisualEditor0"
533+
},
534+
]
535+
Version = "2012-10-17"
536+
}
537+
)
538+
}
539+
440540

441-
##############################################################################################################
442541
# ATTACHED POLICIES
443542

444543
resource "aws_iam_role_policy_attachment" "ReadOnlyAccess_pre_prod" {
445544
count = local.is_pre_production ? 1 : 0
446-
role = aws_iam_role.github_role_pre_prod[0].name
545+
role = aws_iam_role.pre_prod_github_actions[0].name
447546
policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
448547
}
449548

450549
resource "aws_iam_role_policy_attachment" "github_actions_policy_pre_prod" {
451550
count = local.is_pre_production ? 1 : 0
452-
role = aws_iam_role.github_role_pre_prod[0].name
551+
role = aws_iam_role.pre_prod_github_actions[0].name
453552
policy_arn = aws_iam_policy.github_actions_policy_pre_prod[0].arn
454553
}
455554

456-
# aws_iam_policy.github_actions_policy_pre-prod[0]:
555+
# aws_iam_policy.github_actions_policy_pre_prod[0]:
457556
resource "aws_iam_policy" "github_actions_policy_pre_prod" {
458557
count = local.is_pre_production ? 1 : 0
459558
description = null
@@ -696,7 +795,7 @@ resource "aws_iam_policy" "github_actions_policy_pre_prod" {
696795
"ec2:AllocateAddress",
697796
"ec2:CreateNatGateway",
698797
"scheduler:CreateSchedule",
699-
"scheduler:UpdateSchedule"
798+
"scheduler:UpdateSchedule",
700799
]
701800
Effect = "Allow"
702801
Resource = "*"
@@ -711,11 +810,11 @@ resource "aws_iam_policy" "github_actions_policy_pre_prod" {
711810

712811
resource "aws_iam_role_policy_attachment" "github_extended_policy_1_pre_prod" {
713812
count = local.is_pre_production ? 1 : 0
714-
role = aws_iam_role.github_role_pre_prod[0].name
813+
role = aws_iam_role.pre_prod_github_actions[0].name
715814
policy_arn = aws_iam_policy.github_extended_policy_1_pre_prod[0].arn
716815
}
717816

718-
# aws_iam_policy.github_extended_policy_1_pre-prod[0]:
817+
# aws_iam_policy.github_extended_policy_1_pre_prod[0]:
719818
resource "aws_iam_policy" "github_extended_policy_1_pre_prod" {
720819
count = local.is_pre_production ? 1 : 0
721820
description = "more required items for GitHub access"
@@ -845,12 +944,21 @@ resource "aws_iam_policy" "github_extended_policy_1_pre_prod" {
845944
"s3:PutBucketNotification",
846945
"iam:UpdateAssumeRolePolicy",
847946
"sqs:sendmessage",
848-
"kms:GenerateDataKey"
947+
"kms:GenerateDataKey",
849948
]
850949
Effect = "Allow"
851950
Resource = "*"
852951
Sid = "VisualEditor0"
853952
},
953+
{
954+
Action = [
955+
"acm:AddTagsToCertificate",
956+
"acm:DeleteCertificate",
957+
]
958+
Effect = "Allow"
959+
Resource = "arn:aws:acm:us-east-1:${data.aws_caller_identity.current.account_id}:certificate/*"
960+
Sid = "VisualEditor1"
961+
},
854962
]
855963
Version = "2012-10-17"
856964
}

base_iam/policy_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def run_command(command):
3434

3535

3636
def import_resources(aws_account_id, env, role_name, policy_names):
37-
run_command(f'terraform import -var environment={env} aws_iam_role.github_role_{env}[0] {role_name} ')
37+
run_command(f'terraform import -var environment={env} -var aws_account_id={aws_account_id} aws_iam_role.github_role_{env}[0] {role_name} ')
3838
for policy_name in policy_names:
3939
resource_name = policy_name.replace("-", "_")
40-
run_command(f'terraform import -var environment={env} aws_iam_policy.{resource_name}_{env}[0] arn:aws:iam::{aws_account_id}:policy/{policy_name}')
40+
run_command(f'terraform import -var environment={env} -var aws_account_id={aws_account_id} aws_iam_policy.{resource_name}_{env}[0] arn:aws:iam::{aws_account_id}:policy/{policy_name}')
4141

4242

4343
def tidy_resource_file(aws_account_id, env, source):

0 commit comments

Comments
 (0)