Skip to content

Commit cc09260

Browse files
authored
KEH-2460 - Concourse IAM Refactor (#87)
* chore: bump deps * docs: update README * chore: remove unused tfvars * refactor(concourse): Use IAM Roles instead of users
1 parent bb2852c commit cc09260

9 files changed

Lines changed: 617 additions & 491 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ changes to Concourse instance itself. Make sure to export all necessary environm
215215

216216
#### Setting up a pipeline
217217

218-
When setting up our pipelines, we use ecs-infra-user on sdp-dev to be able to interact with our infrastructure on AWS. The credentials for this are stored on
218+
When setting up our pipelines, we use IAM roles to be able to interact with our infrastructure on AWS. The credentials for this are stored on
219219
AWS Secrets Manager so you do not need to set up anything yourself.
220220

221221
To set the pipeline, run the following script:

concourse/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ terraform-task: &terraform-task
5757
secrets: ((sdp_((env))_copilot_usage_lambda_secrets))
5858
# kics-scan ignore-line
5959
github_access_token: ((github_access_token))
60+
aws_role_arn: arn:aws:iam::((aws_account_sdp_((env)))):role/sdp-concourse-((env))
6061
env: ((env))
6162
branch: ((branch))
6263
run:
@@ -76,7 +77,9 @@ terraform-task: &terraform-task
7677
echo "ERROR: Tag '$tag' is not in semantic versioning format (vX.Y.Z)"
7778
exit 1
7879
fi
80+
chmod u+x ./resource-repo/concourse/scripts/assume_role.sh
7981
chmod u+x ./resource-repo/concourse/scripts/terraform_infra.sh
82+
source ./resource-repo/concourse/scripts/assume_role.sh
8083
./resource-repo/concourse/scripts/terraform_infra.sh
8184
timeout: 30m
8285

concourse/scripts/terraform_infra.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,15 @@ set -euo pipefail
66

77
apk add --no-cache jq
88

9-
aws_account_id=$(echo "$secrets" | jq -r .aws_account_id)
10-
aws_access_key_id=$(echo "$secrets" | jq -r .aws_access_key_id)
11-
aws_secret_access_key=$(echo "$secrets" | jq -r .aws_secret_access_key)
12-
139
lambda_name=$(echo "$secrets" | jq -r .lambda_name)
1410
env_name=$(echo "$secrets" | jq -r .env_name)
15-
ecr_repository=$(echo "$secrets" | jq -r .ecr_repository)
1611

1712
lambda_timeout=$(echo "$secrets" | jq -r .lambda_timeout)
1813

1914
github_app_client_id=$(echo "$secrets" | jq -r .github_app_client_id)
2015
aws_secret_name=$(echo "$secrets" | jq -r .aws_secret_name)
2116
github_org=$(echo "$secrets" | jq -r .github_org)
2217

23-
export AWS_ACCESS_KEY_ID="$aws_access_key_id"
24-
export AWS_SECRET_ACCESS_KEY="$aws_secret_access_key"
25-
2618
# kingfisher:ignore
2719
git config --global url."https://x-access-token:$github_access_token@github.com/".insteadOf "https://github.com/"
2820

@@ -39,15 +31,11 @@ terraform init -backend-config=env/"${env}"/backend-"${env}".tfbackend -reconfig
3931
# The following terraform-apply may need to change if the environment variables change
4032

4133
terraform apply \
42-
-var "aws_account_id=$aws_account_id" \
43-
-var "aws_access_key_id=$aws_access_key_id" \
44-
-var "aws_secret_access_key=$aws_secret_access_key" \
4534
-var "env_name=$env_name" \
4635
-var "lambda_name=${lambda_name}" \
4736
-var "github_app_client_id=$github_app_client_id" \
4837
-var "aws_secret_name=$aws_secret_name" \
4938
-var "github_org=$github_org" \
5039
-var "lambda_timeout=${lambda_timeout}" \
51-
-var "ecr_repository=${ecr_repository}" \
5240
-var "container_ver=${tag}" \
5341
-auto-approve

poetry.lock

Lines changed: 586 additions & 450 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/data.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ data "aws_ecr_repository" "profile_lambda_ecr_repo" {
22
name = local.lambda_repo
33
}
44

5+
data "aws_caller_identity" "current" {}
6+
57
data "aws_ecr_image" "lambda_image" {
68
repository_name = data.aws_ecr_repository.profile_lambda_ecr_repo.name
79
image_tag = var.container_ver
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Do NOT commit the real tfvars file to GitHub
2-
aws_account_id = "99999999999"
3-
aws_access_key_id = "AWSACCCESSKEYID"
4-
aws_secret_access_key = "AWSSECRETASSOCIATEDWITHACCESSKEY"
52
aws_secret_name = "sdp/pem"
63
env_name = "sdp-dev"
74
lambda_name = "copilot-usage-lambda"
85
lambda_arch = "x86_64"
96
lambda_timeout = 300
107
schedule = "cron(0 6 ? * 2 *)"
8+
log_retention_days = 30
119
github_org = "ONS-Innovation"
1210
github_app_client_id = "123456789"
13-
ecr_repository = "sdp-dev-address-book-lambda"
14-
container_ver = "v1.0.0"
11+
region = "eu-west-2"
12+
container_ver = "v1.0.0"
13+
project_tag = "SDP"
14+
team_owner_tag = "Knowledge Exchange Hub"
15+
business_owner_tag = "DST"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Do NOT commit the real tfvars file to GitHub
2+
aws_secret_name = "sdp/pem"
3+
env_name = "sdp-prod"
4+
lambda_name = "copilot-usage-lambda"
5+
lambda_arch = "x86_64"
6+
lambda_timeout = 300
7+
schedule = "cron(0 6 ? * 2 *)"
8+
log_retention_days = 30
9+
github_org = "ONS-Innovation"
10+
github_app_client_id = "123456789"
11+
region = "eu-west-2"
12+
container_ver = "v1.0.0"
13+
project_tag = "SDP"
14+
team_owner_tag = "Knowledge Exchange Hub"
15+
business_owner_tag = "DST"

terraform/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ resource "aws_iam_role" "lambda_function_role" {
8080
Action = "sts:AssumeRole"
8181
Effect = "Allow"
8282
Principal = {
83-
AWS = "arn:aws:iam::${var.aws_account_id}:root"
83+
AWS = "arn:aws:iam::${local.aws_account_id}:root"
8484
}
8585
Condition = {
8686
ArnLike = {
8787
"aws:PrincipalArn" = [
88-
"arn:aws:iam::${var.aws_account_id}:role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_Standard_Administrator_Access_*"
88+
"arn:aws:iam::${local.aws_account_id}:role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_Standard_Administrator_Access_*"
8989
]
9090
}
9191
}

terraform/variables.tf

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
variable "aws_account_id" {
2-
description = "AWS Account ID"
3-
type = string
4-
}
5-
6-
variable "aws_access_key_id" {
7-
description = "AWS Access Key ID"
8-
type = string
9-
}
10-
11-
variable "aws_secret_access_key" {
12-
description = "AWS Secret Access Key"
13-
type = string
14-
}
15-
161
variable "aws_secret_name" {
172
description = "The path to the AWS Secret Manager resource which contains the Github App .pem file"
183
type = string
@@ -71,11 +56,6 @@ variable "region" {
7156
default = "eu-west-2"
7257
}
7358

74-
variable "ecr_repository" {
75-
description = "Name of the ECR repository containing the Lambda image"
76-
type = string
77-
}
78-
7959
variable "container_ver" {
8060
description = "Container tag"
8161
type = string
@@ -100,5 +80,6 @@ variable "business_owner_tag" {
10080
}
10181

10282
locals {
103-
lambda_repo = "${var.env_name}-${var.lambda_name}"
83+
aws_account_id = data.aws_caller_identity.current.account_id
84+
lambda_repo = "${var.env_name}-${var.lambda_name}"
10485
}

0 commit comments

Comments
 (0)