Skip to content

Commit fe00fd3

Browse files
committed
feat(runner-role): Enable using separate iam role for runners
1 parent 61d5d28 commit fe00fd3

File tree

8 files changed

+64
-24
lines changed

8 files changed

+64
-24
lines changed

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ module "runners" {
157157
subnet_ids = var.subnet_ids
158158
prefix = var.prefix
159159
tags = local.tags
160+
iam_overrides = var.iam_overrides
160161

161162
ssm_paths = {
162163
root = local.ssm_root_path

modules/runners/logging.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ resource "aws_cloudwatch_log_group" "gh_runners" {
5959
}
6060

6161
resource "aws_iam_role_policy" "cloudwatch" {
62-
count = var.enable_cloudwatch_agent ? 1 : 0
62+
count = var.iam_overrides["override_runner_role"] ? 0 : (var.enable_cloudwatch_agent ? 1 : 0)
6363
name = "CloudWatchLogginAndMetrics"
64-
role = aws_iam_role.runner.name
64+
role = aws_iam_role.runner[0].name
6565
policy = templatefile("${path.module}/policies/instance-cloudwatch-policy.json",
6666
{
6767
ssm_parameter_arn = aws_ssm_parameter.cloudwatch_agent_config_runner[0].arn

modules/runners/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ resource "aws_launch_template" "runner" {
173173
}
174174

175175
iam_instance_profile {
176-
name = aws_iam_instance_profile.runner.name
176+
name = var.iam_overrides["override_instance_profile"] ? var.iam_overrides["instance_profile_name"] : aws_iam_instance_profile.runner[0].name
177177
}
178178

179179
instance_initiated_shutdown_behavior = "terminate"

modules/runners/policies-runner.tf

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
data "aws_caller_identity" "current" {}
22

33
resource "aws_iam_role" "runner" {
4+
count = var.iam_overrides["override_runner_role"] ? 0 : 1
45
name = "${substr("${var.prefix}-runner", 0, 54)}-${substr(md5("${var.prefix}-runner"), 0, 8)}"
56
assume_role_policy = templatefile("${path.module}/policies/instance-role-trust-policy.json", {})
67
path = local.role_path
@@ -9,22 +10,24 @@ resource "aws_iam_role" "runner" {
910
}
1011

1112
resource "aws_iam_instance_profile" "runner" {
12-
name = "${var.prefix}-runner-profile"
13-
role = aws_iam_role.runner.name
14-
path = local.instance_profile_path
15-
tags = local.tags
13+
count = var.iam_overrides["override_instance_profile"] ? 0 : 1
14+
name = "${var.prefix}-runner-profile"
15+
role = aws_iam_role.runner[0].name
16+
path = local.instance_profile_path
17+
tags = local.tags
1618
}
1719

1820
resource "aws_iam_role_policy" "runner_session_manager_aws_managed" {
21+
count = var.iam_overrides["override_runner_role"] ? 0 : (var.enable_ssm_on_runners ? 1 : 0)
1922
name = "runner-ssm-session"
20-
count = var.enable_ssm_on_runners ? 1 : 0
21-
role = aws_iam_role.runner.name
23+
role = aws_iam_role.runner[0].name
2224
policy = templatefile("${path.module}/policies/instance-ssm-policy.json", {})
2325
}
2426

2527
resource "aws_iam_role_policy" "ssm_parameters" {
26-
name = "runner-ssm-parameters"
27-
role = aws_iam_role.runner.name
28+
count = var.iam_overrides["override_runner_role"] ? 0 : 1
29+
name = "runner-ssm-parameters"
30+
role = aws_iam_role.runner[0].name
2831
policy = templatefile("${path.module}/policies/instance-ssm-parameters-policy.json",
2932
{
3033
arn_ssm_parameters_path_tokens = "arn:${var.aws_partition}:ssm:${var.aws_region}:${data.aws_caller_identity.current.account_id}:parameter${var.ssm_paths.root}/${var.ssm_paths.tokens}"
@@ -34,10 +37,10 @@ resource "aws_iam_role_policy" "ssm_parameters" {
3437
}
3538

3639
resource "aws_iam_role_policy" "dist_bucket" {
37-
count = var.enable_runner_binaries_syncer ? 1 : 0
40+
count = var.iam_overrides["override_runner_role"] ? 0 : (var.enable_runner_binaries_syncer ? 1 : 0)
3841

3942
name = "distribution-bucket"
40-
role = aws_iam_role.runner.name
43+
role = aws_iam_role.runner[0].name
4144
policy = templatefile("${path.module}/policies/instance-s3-policy.json",
4245
{
4346
s3_arn = "${var.s3_runner_binaries.arn}/${var.s3_runner_binaries.key}"
@@ -46,33 +49,35 @@ resource "aws_iam_role_policy" "dist_bucket" {
4649
}
4750

4851
resource "aws_iam_role_policy_attachment" "xray_tracing" {
49-
count = var.tracing_config.mode != null ? 1 : 0
50-
role = aws_iam_role.runner.name
52+
count = var.iam_overrides["override_runner_role"] ? 0 : (var.tracing_config.mode != null ? 1 : 0)
53+
role = aws_iam_role.runner[0].name
5154
policy_arn = "arn:${var.aws_partition}:iam::aws:policy/AWSXRayDaemonWriteAccess"
5255
}
5356

5457
resource "aws_iam_role_policy" "describe_tags" {
58+
count = var.iam_overrides["override_runner_role"] ? 0 : 1
5559
name = "runner-describe-tags"
56-
role = aws_iam_role.runner.name
60+
role = aws_iam_role.runner[0].name
5761
policy = file("${path.module}/policies/instance-describe-tags-policy.json")
5862
}
5963

6064
resource "aws_iam_role_policy" "create_tag" {
65+
count = var.iam_overrides["override_runner_role"] ? 0 : 1
6166
name = "runner-create-tags"
62-
role = aws_iam_role.runner.name
67+
role = aws_iam_role.runner[0].name
6368
policy = templatefile("${path.module}/policies/instance-create-tags-policy.json", {})
6469
}
6570

6671
resource "aws_iam_role_policy_attachment" "managed_policies" {
67-
count = length(var.runner_iam_role_managed_policy_arns)
68-
role = aws_iam_role.runner.name
72+
count = var.iam_overrides["override_runner_role"] ? 0 : length(var.runner_iam_role_managed_policy_arns)
73+
role = aws_iam_role.runner[0].name
6974
policy_arn = element(var.runner_iam_role_managed_policy_arns, count.index)
7075
}
7176

72-
7377
resource "aws_iam_role_policy" "ec2" {
78+
count = var.iam_overrides["override_runner_role"] ? 0 : 1
7479
name = "ec2"
75-
role = aws_iam_role.runner.name
80+
role = aws_iam_role.runner[0].name
7681
policy = templatefile("${path.module}/policies/instance-ec2.json", {})
7782
}
7883

modules/runners/pool.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module "pool" {
4848
group_name = var.runner_group_name
4949
name_prefix = var.runner_name_prefix
5050
pool_owner = var.pool_runner_owner
51-
role = aws_iam_role.runner
51+
role = var.iam_overrides["override_runner_role"] ? var.iam_overrides["runner_role_arn"] : aws_iam_role.runner[0].name
5252
}
5353
subnet_ids = var.subnet_ids
5454
ssm_token_path = "${var.ssm_paths.root}/${var.ssm_paths.tokens}"

modules/runners/scale-up.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ resource "aws_iam_role_policy" "scale_up" {
112112
name = "scale-up-policy"
113113
role = aws_iam_role.scale_up.name
114114
policy = templatefile("${path.module}/policies/lambda-scale-up.json", {
115-
arn_runner_instance_role = aws_iam_role.runner.arn
115+
arn_runner_instance_role = var.iam_overrides["override_runner_role"] ? var.iam_overrides["runner_role_arn"] : aws_iam_role.runner[0].arn
116116
sqs_arn = var.sqs_build_queue.arn
117117
github_app_id_arn = var.github_app_parameters.id.arn
118118
github_app_key_base64_arn = var.github_app_parameters.key_base64.arn

modules/runners/variables.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ variable "subnet_ids" {
3636
}
3737

3838
variable "overrides" {
39-
description = "This map provides the possibility to override some defaults. The following attributes are supported: `name_sg` overrides the `Name` tag for all security groups created by this module. `name_runner_agent_instance` overrides the `Name` tag for the ec2 instance defined in the auto launch configuration. `name_docker_machine_runners` overrides the `Name` tag spot instances created by the runner agent."
39+
description = "This map provides the possibility to override some defaults. The following attributes are supported: `name_sg` overrides the `Name` tag for all security groups created by this module. `name_runner` overrides the `Name` tag for the ec2 instance defined in the auto launch configuration. `instance_profile_name` overrides the instance profile name used in the launch template."
4040
type = map(string)
4141

4242
default = {
@@ -45,6 +45,23 @@ variable "overrides" {
4545
}
4646
}
4747

48+
variable "iam_overrides" {
49+
description = "This map provides the possibility to override some IAM defaults. The following attributes are supported: `instance_profile_name` overrides the instance profile name used in the launch template. `runner_role_arn` overrides the IAM role ARN used for the runner instances."
50+
type = object({
51+
override_instance_profile = optional(bool, null)
52+
instance_profile_name = optional(string, null)
53+
override_runner_role = optional(bool, null)
54+
runner_role_arn = optional(string, null)
55+
})
56+
57+
default = {
58+
override_instance_profile = false
59+
instance_profile_name = null
60+
override_runner_role = false
61+
runner_role_arn = null
62+
}
63+
}
64+
4865
variable "tags" {
4966
description = "Map of tags that will be added to created resources. By default resources will be tagged with name."
5067
type = map(string)

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ variable "runner_group_name" {
108108
default = "Default"
109109
}
110110

111+
variable "iam_overrides" {
112+
description = "This map provides the possibility to override some defaults. The following attributes are supported: `name_sg` overrides the `Name` tag for all security groups created by this module. `name_runner` overrides the `Name` tag for the ec2 instance defined in the auto launch configuration. `instance_profile_name` overrides the instance profile name used in the launch template."
113+
type = object({
114+
override_instance_profile = optional(bool, null)
115+
instance_profile_name = optional(string, null)
116+
override_runner_role = optional(bool, null)
117+
runner_role_arn = optional(string, null)
118+
})
119+
120+
default = {
121+
override_instance_profile = false
122+
instance_profile_name = null
123+
override_runner_role = false
124+
runner_role_arn = null
125+
}
126+
}
127+
111128
variable "scale_up_reserved_concurrent_executions" {
112129
description = "Amount of reserved concurrent executions for the scale-up lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations."
113130
type = number

0 commit comments

Comments
 (0)