Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DBTP-913 - Run terraform plan in environment pipelines #110

Merged
merged 77 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
04f785a
Initial role created with tests
antroy-madetech Apr 17, 2024
c0cb167
Added tags to role
antroy-madetech Apr 17, 2024
3a76238
Added s3 bucket for artifact store
antroy-madetech Apr 17, 2024
7b28fd3
Output test for s3 module
antroy-madetech Apr 17, 2024
a04a2d4
Working (dummy) codepipeline can now be created
antroy-madetech Apr 18, 2024
a5764a1
Added requires KMS perms
antroy-madetech Apr 18, 2024
349e9df
Codebuild failing but producing logs
antroy-madetech Apr 18, 2024
89f485e
Added S3 bucket perms to the codedeploy job
antroy-madetech Apr 19, 2024
8c27004
Restricted role permissions for logs to our log group
antroy-madetech Apr 19, 2024
9ebb2e4
Renamed roles.tf -> iam.tf
antroy-madetech Apr 19, 2024
9d59f17
Codebuild 'install' job installs terraform, copilot, platform-helper
antroy-madetech Apr 22, 2024
9a9af0a
Clean up
antroy-madetech Apr 22, 2024
be422d9
Remove Copilot string
WillGibson Apr 22, 2024
e803ee0
Add repository variable and mock AWS provider
WillGibson Apr 22, 2024
61aea48
Start poking at the tests, some progress, still failing
WillGibson Apr 22, 2024
5bdacce
Overriding data block with minimal JSON
antroy-madetech Apr 23, 2024
01c828e
Refactored tag tests
antroy-madetech Apr 23, 2024
8c76626
Merge branch 'main' into DBTP-911-Barebones-Pipeline
WillGibson Apr 23, 2024
9d001eb
terraform fmt -recursive
WillGibson Apr 23, 2024
342eb00
Add check on pipeline name and reuse the tags check
WillGibson Apr 23, 2024
af74114
Add commented out annoying asertion
WillGibson Apr 23, 2024
31a36da
Start adding assertions for aws_codepipeline.codepipeline.stage.action
WillGibson Apr 23, 2024
2b1cbcd
Strip it down to should be
WillGibson Apr 23, 2024
f155607
Add assertions for artifact_store
WillGibson Apr 23, 2024
4f09e60
Rename stages etc.
WillGibson Apr 23, 2024
51a3b5b
Test the pipeline tags
WillGibson Apr 23, 2024
9d7a146
Rename stage back to build
WillGibson Apr 23, 2024
fe34a3b
Remove test_create_pipeline_with_different_application
WillGibson Apr 23, 2024
2913249
Little reshuffle
WillGibson Apr 23, 2024
972599d
Refactoring of names to bring in line with best practice guidelines
antroy-madetech Apr 24, 2024
c1827f5
Remaining iam tests
antroy-madetech Apr 24, 2024
e836abd
Merge branch 'main' into DBTP-911-Barebones-Pipeline
antroy-madetech Apr 24, 2024
c59dc0e
Fix Opensearch tests
antroy-madetech Apr 24, 2024
b0c5054
First codebuild test
antroy-madetech Apr 24, 2024
a8f92a6
Codebuild tests finished
antroy-madetech Apr 24, 2024
2ad249f
terraform fmt
antroy-madetech Apr 24, 2024
b009673
PR comments addressed
antroy-madetech Apr 25, 2024
77803d8
terraform fmt
antroy-madetech Apr 25, 2024
1b1ecc3
Fixed tests
antroy-madetech Apr 25, 2024
0ce3ac2
WIP
antroy-madetech Apr 25, 2024
e8eb219
Update opensearch/main.tf
antroy-madetech Apr 25, 2024
3ef5295
Test changes for rename
antroy-madetech Apr 25, 2024
bddfda5
Reverted branch to main
antroy-madetech Apr 25, 2024
e9d8cab
Removed unnecessary comment
antroy-madetech Apr 25, 2024
5d8a8cc
fmt
antroy-madetech Apr 25, 2024
6099853
WIP
antroy-madetech Apr 25, 2024
e0a5596
Allow changing of branch. Use codestar to get modules repo
antroy-madetech Apr 25, 2024
3fec9c6
Debugging
antroy-madetech Apr 26, 2024
a48ed75
Merge branch 'main' into DBTP-913-Pipleline-tf-plan
antroy-madetech Apr 26, 2024
0946b0e
Merge error fixed
antroy-madetech Apr 26, 2024
d8e51fe
WIP
antroy-madetech Apr 26, 2024
3450a30
Update CodeBuild image
JohnStainsby Apr 26, 2024
46f0d6b
Merge branch 'DBTP-913-Pipleline-tf-plan' of github.com:uktrade/terra…
JohnStainsby Apr 26, 2024
010ca2f
WIP
antroy-madetech Apr 29, 2024
ebde085
Add S3, KMS Key, and DynamoDB IAM policies
JohnStainsby Apr 29, 2024
c70aa6d
Bit of renaming
antroy-madetech Apr 29, 2024
ce0adc6
Added dependency for codepipeline on the codestar role
antroy-madetech Apr 29, 2024
788b4a1
Added perms to read vpcs and subnets
antroy-madetech Apr 29, 2024
7f6e2a8
Final few perms to get redis working
antroy-madetech Apr 29, 2024
5dadd5b
SSM and further EC2 perms
antroy-madetech Apr 29, 2024
64b5bb4
Add DNS account assume role policy
JohnStainsby Apr 29, 2024
e32d0c4
Add domain alias to providers
JohnStainsby Apr 29, 2024
cbf35ee
Add env to hosted zone name
JohnStainsby Apr 29, 2024
1d33730
Revert route53 name change
JohnStainsby Apr 29, 2024
45c16d4
Remove env name
JohnStainsby Apr 29, 2024
cee1ca4
Remove module source; Add variable for dns account it
JohnStainsby Apr 30, 2024
b4b9e06
Read properties from pipelines.yml variable
JohnStainsby Apr 30, 2024
a7f2963
Remove dns variable; Fix iam issue
JohnStainsby May 1, 2024
4ef60b5
Fix existing tests
JohnStainsby May 1, 2024
61ad11f
Add codepipeline tests
JohnStainsby May 1, 2024
e7f9846
Fix IAM role names for consistency; Add additional IAM tests
JohnStainsby May 1, 2024
71e5dac
Fix formatting
JohnStainsby May 1, 2024
fbef95e
Merge branch 'main' into DBTP-913-Pipleline-tf-plan
JohnStainsby May 1, 2024
215cfcd
Fix terraform fmt issues
JohnStainsby May 1, 2024
39b9029
Update example for CodeBuild tests
JohnStainsby May 1, 2024
0722cfe
Split policy statements for ec2 read access
JohnStainsby May 1, 2024
e5da8d6
Output terraform plan file
JohnStainsby May 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions environment-pipelines/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ phases:

build:
commands:
- echo "Build Phase"
- echo "Terraform Plan Phase"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these commands fall under the pre_build phase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes probably once the rest of the copilot and platform-helper commands are added. This stuff is most likely going to be moved to a separate buildspec file anyway to work with a plan -> manual approve -> apply process

- echo "Working on environment ${ENVIRONMENT}"
- cd terraform/${ENVIRONMENT}
- terraform init
- terraform plan -out=plan.tfplan
post_build:
commands:
- echo "Post Build Phase"
artifacts:
files: []
files:
- terraform/${ENVIRONMENT}/plan.tfplan
3 changes: 1 addition & 2 deletions environment-pipelines/codebuild.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ resource "aws_codebuild_project" "environment_pipeline" {

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "amazonlinux:2023"
image = "aws/codebuild/amazonlinux2-x86_64-standard:5.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"

}

logs_config {
Expand Down
44 changes: 28 additions & 16 deletions environment-pipelines/codepipeline.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ data "aws_codestarconnections_connection" "github_codestar_connection" {
}

resource "aws_codepipeline" "environment_pipeline" {
name = "${var.application}-environment-pipeline"
role_arn = aws_iam_role.environment_pipeline_codepipeline.arn
name = "${var.application}-environment-pipeline"
role_arn = aws_iam_role.environment_pipeline_codepipeline.arn
depends_on = [aws_iam_role_policy.artifact_store_access_for_environment_codebuild]

artifact_store {
location = module.artifact_store.bucket_name
Expand All @@ -25,30 +26,41 @@ resource "aws_codepipeline" "environment_pipeline" {
owner = "AWS"
provider = "CodeStarSourceConnection"
version = "1"
output_artifacts = ["source_output"]
output_artifacts = ["project_deployment_source"]

configuration = {
ConnectionArn = data.aws_codestarconnections_connection.github_codestar_connection.arn
FullRepositoryId = var.repository
BranchName = "main"
BranchName = var.branch
}
}
}

stage {
name = "Build"

action {
name = "InstallTools"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["source_output"]
output_artifacts = ["build_output"]
version = "1"
dynamic "stage" {
for_each = local.stages
content {
name = "Build"

configuration = {
ProjectName = "${var.application}-environment-pipeline"
action {
name = "InstallTools"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["project_deployment_source"]
output_artifacts = ["terraform_plan"]
version = "1"

configuration = {
ProjectName = "${var.application}-environment-pipeline"
PrimarySource = "project_deployment_source"
EnvironmentVariables = jsonencode([
{
name = "ENVIRONMENT"
value = stage.value.env
}
])
}
}
}
}
Expand Down
135 changes: 135 additions & 0 deletions environment-pipelines/iam.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

data "aws_iam_policy_document" "assume_codepipeline_role" {
statement {
effect = "Allow"
Expand Down Expand Up @@ -87,6 +90,100 @@ data "aws_iam_policy_document" "write_environment_pipeline_codebuild_logs" {
}
}

data "aws_s3_bucket" "state_bucket" {
bucket = "terraform-platform-state-${local.stages[0].accounts.deploy.name}"
}

data "aws_iam_policy_document" "state_bucket_access" {
statement {
actions = [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject"
]
resources = [
data.aws_s3_bucket.state_bucket.arn,
"${data.aws_s3_bucket.state_bucket.arn}/*"
]
}
}

data "aws_kms_key" "state_kms_key" {
key_id = "alias/terraform-platform-state-s3-key-${local.stages[0].accounts.deploy.name}"
}

data "aws_iam_policy_document" "state_kms_key_access" {
statement {
actions = [
"kms:ListKeys",
"kms:Decrypt"
]
resources = [
data.aws_kms_key.state_kms_key.arn
]
}
}

data "aws_iam_policy_document" "state_dynamo_db_access" {
statement {
actions = [
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
]
resources = [
"arn:aws:dynamodb:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/terraform-platform-lockdb-${local.stages[0].accounts.deploy.name}"
]
}
}

data "aws_iam_policy_document" "ec2_read_access" {
statement {
actions = [
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups"
]
resources = [
"*"
]
}

statement {
actions = [
"ec2:DescribeVpcAttribute"
]
resources = [
"arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:vpc/*"
]
}
}

data "aws_ssm_parameter" "central_log_group_parameter" {
name = "/copilot/tools/central_log_groups"
}

data "aws_iam_policy_document" "ssm_read_access" {
statement {
actions = [
"ssm:GetParameter",
]
resources = [
data.aws_ssm_parameter.central_log_group_parameter.arn
]
}
}

data "aws_iam_policy_document" "dns_account_assume_role" {
statement {
actions = [
"sts:AssumeRole",
]
resources = tolist(toset([for stage in local.stages : "arn:aws:iam::${stage.accounts.dns.id}:role/sandbox-codebuild-assume-role"]))
}
}

resource "aws_iam_role" "environment_pipeline_codepipeline" {
name = "${var.application}-environment-pipeline-codepipeline"
assume_role_policy = data.aws_iam_policy_document.assume_codepipeline_role.json
Expand Down Expand Up @@ -117,3 +214,41 @@ resource "aws_iam_role_policy" "log_access_for_environment_codebuild" {
policy = data.aws_iam_policy_document.write_environment_pipeline_codebuild_logs.json
}

# Terraform state access
resource "aws_iam_role_policy" "state_bucket_access_for_environment_codebuild" {
name = "${var.application}-state-bucket-access-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.state_bucket_access.json
}

resource "aws_iam_role_policy" "state_kms_key_access_for_environment_codebuild" {
name = "${var.application}-state-kms-key-access-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.state_kms_key_access.json
}

resource "aws_iam_role_policy" "state_dynamo_db_access_for_environment_codebuild" {
name = "${var.application}-state-dynamo-db-access-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.state_dynamo_db_access.json
}

# VPC and Subnets
resource "aws_iam_role_policy" "ec2_read_access_for_environment_codebuild" {
name = "${var.application}-ec2-read-access-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.ec2_read_access.json
}

resource "aws_iam_role_policy" "ssm_read_access_for_environment_codebuild" {
name = "${var.application}-ssm-read-access-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.ssm_read_access.json
}

# Assume DNS account role
resource "aws_iam_role_policy" "dns_account_assume_role_for_environment_codebuild" {
name = "${var.application}-dns-account-assume-role-for-environment-codebuild"
role = aws_iam_role.environment_pipeline_codebuild.name
policy = data.aws_iam_policy_document.dns_account_assume_role.json
}
4 changes: 4 additions & 0 deletions environment-pipelines/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ locals {
copilot-application = var.application
managed-by = "DBT Platform - Terraform"
}

stage_config = yamldecode(file("${path.module}/stage_config.yml"))

stages = [for env in var.environments : { type : "plan", env : env.name, approval : env.requires_approval, accounts : env.accounts }]
}
20 changes: 20 additions & 0 deletions environment-pipelines/stage_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
plan:
name: "Plan"
category: "Build"
owner: "AWS"
provider: "CodeBuild"
input_artifacts: ["source_output"]
output_artifacts: ["build_output"]
version: "1"
approve:
run_order: 1
name: "AWS-Admin-Approval"
category: "Approval"
owner: "AWS"
provider: "Manual"
version: "1"
input_artifacts: []
output_artifacts: []

apply:
Loading