Skip to content

Commit d4c1f6a

Browse files
authored
Plt 1605 establish cdap-test and cdap-prod codebuild runners (#400)
## 🎫 Ticket https://jira.cms.gov/browse/... ## 🛠 Changes <!-- What was added, updated, or removed in this PR? --> Establishes new codebuild runners in the cdap-test and cdap-prod environments. Allows for continued use of bcda-prod, as is, with x86 available to all repositories. Allows for continued use of bcda-app-arm64 and bcda-ssas-app-arm64. ## ℹ️ Context These changes are made to ensure that all codebuild resources are operating in cdap-test and cdap-prod so that cdap-mgmt may be deprecated. <!-- Why were these changes made? Add background context suitable for a non-technical audience. --> <!-- If any of the following security implications apply, this PR must not be merged without Stephen Walter's approval. Explain in this section and add @SJWalter11 as a reviewer. - Adds a new software dependency or dependencies. - Modifies or invalidates one or more of our security controls. - Stores or transmits data that was not stored or transmitted before. - Requires additional review of security implications for other reasons. --> ## 🧪 Validation There is no change for the bcda-prod runner resources, including the new arm64 runners. The changes in cdap-prod and cdap-test configurations introduce 40 resources. These will be established and then follow up PRs will be submitted to test their use with cdap lambdas and cdap actions. <!-- How were the changes verified? Did you fully test the acceptance criteria in the ticket? Provide reproducible testing instructions and screenshots if applicable. -->
1 parent 0e1deaf commit d4c1f6a

File tree

5 files changed

+154
-32
lines changed

5 files changed

+154
-32
lines changed

terraform/modules/standards/outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ output "env" {
4040
value = local.env
4141
}
4242

43+
output "account_env_suffix" {
44+
description = "[\"prod\" or \"non-prod\"] The AWS account shorthand to distinguish environment hierarchy."
45+
sensitive = false
46+
value = (var.env == "prod" || var.env == "sandbox") ? "prod" : "non-prod"
47+
}
48+
4349
output "default_tags" {
4450
description = "Map of tags for use in AWS provider block `default_tags`. Merges collection of standard tags with optional, user-specificed `additional_tags`"
4551
sensitive = false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
TARGET_ENVS="bcda-prod" # TODO this should be cdap-mgmt
1+
TARGET_ENVS="bcda-prod cdap-test" # TODO deprecate bcda-prod usage once all teams are using new codebuild images

terraform/services/codebuild-projects/data.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
data "aws_caller_identity" "current" {}
22

33
data "aws_security_group" "security_tools" {
4-
vpc_id = module.vpc.id
4+
vpc_id = var.app == "bcda" ? module.vpc[0].id : module.standards.cdap_vpc.id
55
name = "cmscloud-security-tools"
66
}
77

88
data "aws_security_group" "security_validation_egress" {
9-
vpc_id = module.vpc.id
9+
count = var.app == "bcda" ? 1 : 0
10+
vpc_id = module.vpc[0].id
1011
name = "cms-cloud-security-validation-egress"
1112
}
1213

terraform/services/codebuild-projects/main.tf

Lines changed: 127 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
locals {
2-
x86_repos = [
2+
arm64_image = "aws/codebuild/amazonlinux2-aarch64-standard:2.0"
3+
x86_image = "aws/codebuild/amazonlinux-x86_64-standard:5.0"
4+
arm64_changeover_projects = var.app == "bcda" ? ["bcda-app", "bcda-ssas-app"] : []
5+
6+
repos = [
37
"ab2d",
48
"ab2d-website",
59
"bcda-app",
@@ -10,36 +14,19 @@ locals {
1014
"dpc-ops",
1115
"dpc-static-site",
1216
]
13-
14-
arm64_repos = [
15-
"bcda-app",
16-
"bcda-ssas-app"
17-
]
1817
}
1918

2019
module "standards" {
2120
source = "../../modules/standards"
2221

2322
app = "cdap"
24-
env = "mgmt"
23+
env = var.app == "bcda" ? "mgmt" : var.env
2524
root_module = "https://github.com/CMSgov/cdap/tree/main/terraform/services/codebuild-projects"
2625
service = "codebuild-projects"
2726
providers = { aws = aws, aws.secondary = aws.secondary }
2827
}
2928

30-
module "vpc" {
31-
source = "../../modules/vpc"
32-
33-
app = "cdap"
34-
env = "mgmt"
35-
}
36-
37-
module "subnets" {
38-
source = "../../modules/subnets"
39-
40-
vpc_id = module.vpc.id
41-
use = "private"
42-
}
29+
# IAM
4330

4431
resource "aws_iam_role" "codebuild" {
4532
name = "codebuild-runner"
@@ -64,8 +51,48 @@ resource "aws_iam_role_policy_attachment" "ssm_read_only" {
6451
policy_arn = data.aws_iam_policy.ssm_read_only.arn
6552
}
6653

54+
# Network
55+
resource "aws_security_group" "codebuild_project" {
56+
for_each = var.app == "cdap" ? toset(local.repos) : toset([])
57+
58+
name = "${each.key}-${module.standards.account_env_suffix}-codebuild-project"
59+
60+
description = "For the ${module.standards.account_env_suffix} ${each.key}"
61+
vpc_id = module.standards.cdap_vpc.id
62+
}
63+
64+
resource "aws_vpc_security_group_egress_rule" "codebuild_project" {
65+
for_each = aws_security_group.codebuild_project
66+
67+
security_group_id = aws_security_group.codebuild_project[each.key].id
68+
69+
cidr_ipv4 = "0.0.0.0/0"
70+
from_port = 0
71+
ip_protocol = "tcp"
72+
to_port = 0
73+
}
74+
75+
# TODO: To deprecate cdap-mgmt runners remove conditional logic for vpc_id and set to use standards module only.
76+
module "subnets" {
77+
source = "../../modules/subnets"
78+
79+
vpc_id = var.app == "bcda" ? module.vpc[0].id : module.standards.cdap_vpc.id
80+
use = "private"
81+
}
82+
83+
## TO DO: Delete cdap-mgmt codebuilder runners that are managed through bcda-prod terraform and use the cdap-mgmt VPC.
84+
## Remove all blocks from here until the "per_repo" resource instantiations
85+
module "vpc" {
86+
count = var.app == "bcda" ? 1 : 0
87+
source = "../../modules/vpc"
88+
89+
app = "cdap"
90+
env = "mgmt"
91+
}
92+
93+
6794
resource "aws_codebuild_project" "this" {
68-
for_each = toset(local.x86_repos)
95+
for_each = var.app == "bcda" ? toset(local.repos) : toset([])
6996

7097
name = each.key
7198
description = "Codebuild project for ${each.key}"
@@ -77,18 +104,18 @@ resource "aws_codebuild_project" "this" {
77104

78105
environment {
79106
compute_type = "BUILD_GENERAL1_SMALL"
80-
image = "aws/codebuild/amazonlinux-x86_64-standard:5.0"
107+
image = local.x86_image
81108
type = "LINUX_CONTAINER"
82109
image_pull_credentials_type = "CODEBUILD"
83110
privileged_mode = true
84111
}
85112

86113
vpc_config {
87-
vpc_id = module.vpc.id
114+
vpc_id = module.vpc[0].id
88115
subnets = module.subnets.ids
89116
security_group_ids = [
90117
data.aws_security_group.security_tools.id,
91-
data.aws_security_group.security_validation_egress.id
118+
data.aws_security_group.security_validation_egress[0].id
92119
]
93120
}
94121

@@ -117,7 +144,7 @@ resource "aws_codebuild_project" "this" {
117144
}
118145

119146
resource "aws_codebuild_webhook" "this" {
120-
for_each = toset(local.x86_repos)
147+
for_each = aws_codebuild_project.this
121148

122149
project_name = each.key
123150
build_type = "BUILD"
@@ -129,10 +156,11 @@ resource "aws_codebuild_webhook" "this" {
129156
}
130157
}
131158

159+
# ARM64 Configurations that were established before migrating towards a cdap-test and cdap-prod managed codebuild project
160+
# Moving from these will require code change in bcda-app and bcda-ssas-app
132161

133-
# ARM64 Configurations
134162
resource "aws_codebuild_project" "arm64" {
135-
for_each = toset(local.arm64_repos)
163+
for_each = var.app == "bcda" ? toset(local.arm64_changeover_projects) : toset([])
136164

137165
name = "${each.key}-arm64"
138166
description = "Codebuild project for ${each.key} using arm64"
@@ -151,11 +179,11 @@ resource "aws_codebuild_project" "arm64" {
151179
}
152180

153181
vpc_config {
154-
vpc_id = module.vpc.id
182+
vpc_id = module.vpc[0].id
155183
subnets = module.subnets.ids
156184
security_group_ids = [
157185
data.aws_security_group.security_tools.id,
158-
data.aws_security_group.security_validation_egress.id
186+
data.aws_security_group.security_validation_egress[0].id
159187
]
160188
}
161189

@@ -188,3 +216,73 @@ resource "aws_codebuild_webhook" "arm64" {
188216
}
189217
}
190218
}
219+
220+
## Maintain the following after cdap-mgmt deprecation:
221+
# Create cdap-test and cdap-prod resources separately for direct deprecation of old resources by block deletion without more code change
222+
223+
resource "aws_codebuild_project" "per_repo" {
224+
for_each = var.app == "cdap" ? toset(local.repos) : toset([])
225+
226+
name = "${each.key}-${module.standards.account_env_suffix}"
227+
description = "Codebuild project for ${each.key}"
228+
service_role = aws_iam_role.codebuild.arn
229+
230+
artifacts {
231+
type = "NO_ARTIFACTS"
232+
}
233+
234+
environment {
235+
compute_type = "BUILD_GENERAL1_SMALL"
236+
image = local.arm64_image
237+
type = "ARM_CONTAINER"
238+
image_pull_credentials_type = "CODEBUILD"
239+
privileged_mode = true
240+
}
241+
242+
vpc_config {
243+
vpc_id = module.standards.cdap_vpc.id
244+
subnets = module.subnets.ids
245+
security_group_ids = [
246+
data.aws_security_group.security_tools.id,
247+
aws_security_group.codebuild_project[each.key].id
248+
]
249+
}
250+
251+
logs_config {
252+
cloudwatch_logs {
253+
status = "ENABLED"
254+
}
255+
}
256+
257+
source {
258+
type = "GITHUB"
259+
location = "https://github.com/CMSgov/${each.key}"
260+
git_clone_depth = 1
261+
262+
git_submodules_config {
263+
fetch_submodules = false
264+
}
265+
}
266+
267+
lifecycle {
268+
ignore_changes = [
269+
build_timeout,
270+
environment[0].compute_type
271+
]
272+
}
273+
}
274+
275+
resource "aws_codebuild_webhook" "per_repo" {
276+
for_each = aws_codebuild_project.per_repo
277+
278+
project_name = "${each.key}-${module.standards.account_env_suffix}"
279+
build_type = "BUILD"
280+
filter_group {
281+
filter {
282+
type = "EVENT"
283+
pattern = "WORKFLOW_JOB_QUEUED"
284+
}
285+
}
286+
}
287+
288+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "app" {
2+
description = "The application name (cdap, bcda)"
3+
type = string
4+
validation {
5+
condition = contains(["bcda", "cdap"], var.app)
6+
error_message = "Valid value for app is bcda or cdap."
7+
}
8+
}
9+
10+
variable "env" {
11+
description = "The application environment (dev, test, mgmt, sbx, sandbox, prod)"
12+
type = string
13+
validation {
14+
condition = contains(["test", "prod"], var.env)
15+
error_message = "Valid value for env is test or prod."
16+
}
17+
}

0 commit comments

Comments
 (0)