Skip to content

Commit e2f9a27

Browse files
surminusnpalm
andauthored
feat: Replace environment variable by prefix (#1858)
We're looking at using this module to deploy multiple sets of runner types (x86_64 and arm architectures) but within the same conceptual "environment". We use the "Environment" tag throughout our tooling, but the constraints of using the "environment" variable for resource naming mean that we need to essentially supply different environment names (eg "env-amd64" and "env-arm64"), even though they are not in different environments. We also use the dot character (".") in our environment names, which isn't allowed in some resource names (eg SQS queue name). This PR replaces the "environmet" variable by "prefix" to prefix resources crated by the module. The prefix is also used to set the tag: "ghr:environment" with the value of prefix for lambda's to orchestrate the instnaces. You can still set the tag "environment" to all resources via the AWS provider. Co-authored-by: Niek Palm <[email protected]>
1 parent 055e2b0 commit e2f9a27

File tree

31 files changed

+159
-88
lines changed

31 files changed

+159
-88
lines changed

examples/arm64/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "runners" {
1919
vpc_id = module.vpc.vpc_id
2020
subnet_ids = module.vpc.private_subnets
2121

22-
environment = local.environment
22+
prefix = local.environment
2323
tags = {
2424
Project = "ProjectX"
2525
}

examples/default/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "runners" {
1919
vpc_id = module.vpc.vpc_id
2020
subnet_ids = module.vpc.private_subnets
2121

22-
environment = local.environment
22+
prefix = local.environment
2323
tags = {
2424
Project = "ProjectX"
2525
}

examples/ephemeral/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module "runners" {
1616
vpc_id = module.vpc.vpc_id
1717
subnet_ids = module.vpc.private_subnets
1818

19-
environment = local.environment
19+
prefix = local.environment
2020
tags = {
2121
Project = "ProjectX"
2222
}

examples/permissions-boundary/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module "runners" {
3535
subnet_ids = module.vpc.private_subnets
3636
kms_key_arn = aws_kms_key.github.key_id
3737

38-
environment = local.environment
38+
prefix = local.environment
3939
tags = {
4040
Project = "ProjectX"
4141
}

examples/prebuilt/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module "runners" {
1515
vpc_id = module.vpc.vpc_id
1616
subnet_ids = module.vpc.private_subnets
1717

18-
environment = local.environment
18+
prefix = local.environment
1919

2020
github_app = {
2121
key_base64 = var.github_app_key_base64

examples/ubuntu/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module "runners" {
1616
vpc_id = module.vpc.vpc_id
1717
subnet_ids = module.vpc.private_subnets
1818

19-
environment = local.environment
19+
prefix = local.environment
2020
tags = {
2121
Project = "ProjectX"
2222
}

examples/windows/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ resource "random_id" "random" {
1010
module "runners" {
1111
source = "../../"
1212

13-
aws_region = local.aws_region
14-
vpc_id = module.vpc.vpc_id
15-
subnet_ids = module.vpc.private_subnets
16-
environment = local.environment
13+
aws_region = local.aws_region
14+
vpc_id = module.vpc.vpc_id
15+
subnet_ids = module.vpc.private_subnets
16+
prefix = local.environment
1717

1818
github_app = {
1919
key_base64 = var.github_app_key_base64

main.tf

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
locals {
22
tags = merge(var.tags, {
3-
Environment = var.environment,
4-
"ghr:environment" = format("%s", var.environment)
3+
"ghr:environment" = var.prefix
54
})
65

76
s3_action_runner_url = "s3://${module.runner_binaries.bucket.id}/${module.runner_binaries.runner_distribution_object_key}"
@@ -50,7 +49,7 @@ resource "aws_sqs_queue_policy" "build_queue_policy" {
5049
}
5150

5251
resource "aws_sqs_queue" "queued_builds" {
53-
name = "${var.environment}-queued-builds${var.fifo_build_queue ? ".fifo" : ""}"
52+
name = "${var.prefix}-queued-builds${var.fifo_build_queue ? ".fifo" : ""}"
5453
delay_seconds = var.delay_webhook_event
5554
visibility_timeout_seconds = var.runners_scale_up_lambda_timeout
5655
message_retention_seconds = var.job_queue_retention_in_seconds
@@ -74,7 +73,7 @@ resource "aws_sqs_queue_policy" "build_queue_dlq_policy" {
7473

7574
resource "aws_sqs_queue" "queued_builds_dlq" {
7675
count = var.redrive_build_queue.enabled ? 1 : 0
77-
name = "${var.environment}-queued-builds_dead_letter"
76+
name = "${var.prefix}-queued-builds_dead_letter"
7877

7978
tags = var.tags
8079
}
@@ -83,7 +82,7 @@ module "ssm" {
8382
source = "./modules/ssm"
8483

8584
kms_key_arn = var.kms_key_arn
86-
environment = var.environment
85+
prefix = var.prefix
8786
github_app = var.github_app
8887
tags = local.tags
8988
}
@@ -92,7 +91,7 @@ module "webhook" {
9291
source = "./modules/webhook"
9392

9493
aws_region = var.aws_region
95-
environment = var.environment
94+
prefix = var.prefix
9695
tags = local.tags
9796
kms_key_arn = var.kms_key_arn
9897

@@ -127,7 +126,7 @@ module "runners" {
127126
aws_partition = var.aws_partition
128127
vpc_id = var.vpc_id
129128
subnet_ids = var.subnet_ids
130-
environment = var.environment
129+
prefix = var.prefix
131130
tags = local.tags
132131

133132
s3_bucket_runner_binaries = module.runner_binaries.bucket
@@ -214,11 +213,11 @@ module "runners" {
214213
module "runner_binaries" {
215214
source = "./modules/runner-binaries-syncer"
216215

217-
aws_region = var.aws_region
218-
environment = var.environment
219-
tags = local.tags
216+
aws_region = var.aws_region
217+
prefix = var.prefix
218+
tags = local.tags
220219

221-
distribution_bucket_name = "${var.environment}-dist-${random_string.random.result}"
220+
distribution_bucket_name = "${var.prefix}-dist-${random_string.random.result}"
222221

223222
runner_os = var.runner_os
224223
runner_architecture = var.runner_architecture
@@ -244,10 +243,10 @@ module "runner_binaries" {
244243
}
245244

246245
resource "aws_resourcegroups_group" "resourcegroups_group" {
247-
name = "${var.environment}-group"
246+
name = "${var.prefix}-group"
248247
resource_query {
249248
query = templatefile("${path.module}/templates/resource-group.json", {
250-
environment = var.environment
249+
environment = var.prefix
251250
})
252251
}
253252
}

modules/runner-binaries-syncer/runner-binaries-syncer.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
22
lambda_zip = var.lambda_zip == null ? "${path.module}/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip" : var.lambda_zip
3-
role_path = var.role_path == null ? "/${var.environment}/" : var.role_path
3+
role_path = var.role_path == null ? "/${var.prefix}/" : var.role_path
44
gh_binary_os_label = {
55
windows = "win",
66
linux = "linux"
@@ -13,7 +13,7 @@ resource "aws_lambda_function" "syncer" {
1313
s3_object_version = var.syncer_lambda_s3_object_version != null ? var.syncer_lambda_s3_object_version : null
1414
filename = var.lambda_s3_bucket == null ? local.lambda_zip : null
1515
source_code_hash = var.lambda_s3_bucket == null ? filebase64sha256(local.lambda_zip) : null
16-
function_name = "${var.environment}-syncer"
16+
function_name = "${var.prefix}-syncer"
1717
role = aws_iam_role.syncer_lambda.arn
1818
handler = "index.handler"
1919
runtime = "nodejs14.x"
@@ -63,7 +63,7 @@ resource "aws_cloudwatch_log_group" "syncer" {
6363
}
6464

6565
resource "aws_iam_role" "syncer_lambda" {
66-
name = "${var.environment}-action-syncer-lambda-role"
66+
name = "${var.prefix}-action-syncer-lambda-role"
6767
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
6868
path = local.role_path
6969
permissions_boundary = var.role_permissions_boundary
@@ -92,7 +92,7 @@ data "aws_iam_policy_document" "lambda_assume_role_policy" {
9292
}
9393

9494
resource "aws_iam_role_policy" "lambda_logging" {
95-
name = "${var.environment}-lambda-logging-policy-syncer"
95+
name = "${var.prefix}-lambda-logging-policy-syncer"
9696
role = aws_iam_role.syncer_lambda.id
9797

9898
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {
@@ -101,7 +101,7 @@ resource "aws_iam_role_policy" "lambda_logging" {
101101
}
102102

103103
resource "aws_iam_role_policy" "syncer" {
104-
name = "${var.environment}-lambda-syncer-s3-policy"
104+
name = "${var.prefix}-lambda-syncer-s3-policy"
105105
role = aws_iam_role.syncer_lambda.id
106106

107107
policy = templatefile("${path.module}/policies/lambda-syncer.json", {
@@ -110,7 +110,7 @@ resource "aws_iam_role_policy" "syncer" {
110110
}
111111

112112
resource "aws_cloudwatch_event_rule" "syncer" {
113-
name = "${var.environment}-syncer-rule"
113+
name = "${var.prefix}-syncer-rule"
114114
schedule_expression = var.lambda_schedule_expression
115115
tags = var.tags
116116
}

modules/runner-binaries-syncer/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ variable "tags" {
1212
variable "environment" {
1313
description = "A name that identifies the environment, used as prefix and for tagging."
1414
type = string
15+
default = null
16+
17+
validation {
18+
condition = var.environment == null
19+
error_message = "The \"environment\" variable is no longer used. To migrate, set the \"prefix\" variable to the original value of \"environment\" and optionally, add \"Environment\" to the \"tags\" variable map with the same value."
20+
}
21+
}
22+
23+
variable "prefix" {
24+
description = "The prefix used for naming resources"
25+
type = string
26+
default = "github-actions"
1527
}
1628

1729
variable "distribution_bucket_name" {

0 commit comments

Comments
 (0)