Skip to content

Commit 3380111

Browse files
authored
Feature: Allow users to pass their own IAM roles (#44)
* Feature: Allow users to pass their own IAM roles * Rename variables following best practices * update wording for variables & remove unused variable
1 parent 862ddde commit 3380111

11 files changed

+93
-26
lines changed

.github/CODEOWNERS

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
# Cloud Posse must review any changes to standard context definition,
1717
# but some changes can be rubber-stamped.
18-
**/*.tf @cloudposse/engineering @cloudposse/approvers
19-
README.yaml @cloudposse/engineering @cloudposse/approvers
18+
**/*.tf @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers
19+
README.yaml @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers
2020
README.md @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers
2121
docs/*.md @cloudposse/engineering @cloudposse/contributors @cloudposse/approvers
2222

.github/auto-release.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ version-resolver:
1717
- 'bugfix'
1818
- 'bug'
1919
- 'hotfix'
20+
- 'no-release'
2021
default: 'minor'
2122

2223
categories:
@@ -46,7 +47,7 @@ template: |
4647
4748
replacers:
4849
# Remove irrelevant information from Renovate bot
49-
- search: '/---\s+^#.*Renovate configuration(?:.|\n)*?This PR has been generated .*/gm'
50+
- search: '/(?<=---\s+)+^#.*(Renovate configuration|Configuration)(?:.|\n)*?This PR has been generated .*/gm'
5051
replace: ''
5152
# Remove Renovate bot banner image
5253
- search: '/\[!\[[^\]]*Renovate\][^\]]*\](\([^)]*\))?\s*\n+/gm'

.github/mergify.yml

+7
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ pull_request_rules:
5656
changes_requested: true
5757
approved: true
5858
message: "This Pull Request has been updated, so we're dismissing all reviews."
59+
60+
- name: "close Pull Requests without files changed"
61+
conditions:
62+
- "#files=0"
63+
actions:
64+
close:
65+
message: "This pull request has been automatically closed by Mergify because there are no longer any changes."

.github/workflows/auto-format.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
jobs:
77
auto-format:
88
runs-on: ubuntu-latest
9-
container: cloudposse/build-harness:slim-latest
9+
container: cloudposse/build-harness:latest
1010
steps:
1111
# Checkout the pull request branch
1212
# "An action in a workflow run can’t trigger a new workflow run. For example, if an action pushes code using
@@ -29,6 +29,8 @@ jobs:
2929
- name: Auto Format
3030
if: github.event.pull_request.state == 'open'
3131
shell: bash
32+
env:
33+
GITHUB_TOKEN: "${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}"
3234
run: make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true -f /build-harness/templates/Makefile.build-harness pr/auto-format/host
3335

3436
# Commit changes (if any) to the PR branch

.github/workflows/auto-release.yml

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ name: auto-release
33
on:
44
push:
55
branches:
6-
- master
6+
- main
7+
- master
8+
- production
79

810
jobs:
911
publish:
1012
runs-on: ubuntu-latest
1113
steps:
12-
# Drafts your next Release notes as Pull Requests are merged into "master"
13-
- uses: release-drafter/release-drafter@v5
14-
with:
15-
publish: true
16-
prerelease: false
17-
config-name: auto-release.yml
18-
env:
19-
GITHUB_TOKEN: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}
14+
# Get PR from merged commit to master
15+
- uses: actions-ecosystem/action-get-merged-pull-request@v1
16+
id: get-merged-pull-request
17+
with:
18+
github_token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}
19+
# Drafts your next Release notes as Pull Requests are merged into "main"
20+
- uses: release-drafter/release-drafter@v5
21+
with:
22+
publish: ${{ !contains(steps.get-merged-pull-request.outputs.labels, 'no-release') }}
23+
prerelease: false
24+
config-name: auto-release.yml
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}

.github/workflows/validate-codeowners.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: Validate Codeowners
22
on:
3+
workflow_dispatch:
4+
35
pull_request:
46

57
jobs:

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,13 @@ Available targets:
297297
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
298298
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
299299
| <a name="input_ebs_root_volume_size"></a> [ebs\_root\_volume\_size](#input\_ebs\_root\_volume\_size) | Size in GiB of the EBS root device volume of the Linux AMI that is used for each EC2 instance. Available in Amazon EMR version 4.x and later | `number` | `10` | no |
300+
| <a name="input_ec2_autoscaling_role_enabled"></a> [ec2\_autoscaling\_role\_enabled](#input\_ec2\_autoscaling\_role\_enabled) | If set to `false`, will use `existing_ec2_autoscaling_role_arn` for an existing EC2 autoscaling IAM role that was created outside of this module | `bool` | `true` | no |
301+
| <a name="input_ec2_role_enabled"></a> [ec2\_role\_enabled](#input\_ec2\_role\_enabled) | If set to `false`, will use `existing_ec2_instance_profile_arn` for an existing EC2 IAM role that was created outside of this module | `bool` | `true` | no |
300302
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
301303
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
304+
| <a name="input_existing_ec2_autoscaling_role_arn"></a> [existing\_ec2\_autoscaling\_role\_arn](#input\_existing\_ec2\_autoscaling\_role\_arn) | ARN of an existing EC2 autoscaling role to attach to the cluster | `string` | `""` | no |
305+
| <a name="input_existing_ec2_instance_profile_arn"></a> [existing\_ec2\_instance\_profile\_arn](#input\_existing\_ec2\_instance\_profile\_arn) | ARN of an existing EC2 instance profile | `string` | `""` | no |
306+
| <a name="input_existing_service_role_arn"></a> [existing\_service\_role\_arn](#input\_existing\_service\_role\_arn) | ARN of an existing EMR service role to attach to the cluster | `string` | `""` | no |
302307
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
303308
| <a name="input_keep_job_flow_alive_when_no_steps"></a> [keep\_job\_flow\_alive\_when\_no\_steps](#input\_keep\_job\_flow\_alive\_when\_no\_steps) | Switch on/off run cluster with no steps or when all steps are complete | `bool` | `true` | no |
304309
| <a name="input_kerberos_ad_domain_join_password"></a> [kerberos\_ad\_domain\_join\_password](#input\_kerberos\_ad\_domain\_join\_password) | The Active Directory password for ad\_domain\_join\_user. Terraform cannot perform drift detection of this configuration. | `string` | `null` | no |
@@ -334,6 +339,7 @@ Available targets:
334339
| <a name="input_scale_down_behavior"></a> [scale\_down\_behavior](#input\_scale\_down\_behavior) | The way that individual Amazon EC2 instances terminate when an automatic scale-in activity occurs or an instance group is resized | `string` | `null` | no |
335340
| <a name="input_security_configuration"></a> [security\_configuration](#input\_security\_configuration) | The security configuration name to attach to the EMR cluster. Only valid for EMR clusters with `release_label` 4.8.0 or greater. See https://www.terraform.io/docs/providers/aws/r/emr_security_configuration.html for more info | `string` | `null` | no |
336341
| <a name="input_service_access_security_group"></a> [service\_access\_security\_group](#input\_service\_access\_security\_group) | The name of the existing additional security group that will be used for EMR core & task nodes. If empty, a new security group will be created | `string` | `""` | no |
342+
| <a name="input_service_role_enabled"></a> [service\_role\_enabled](#input\_service\_role\_enabled) | If set to `false`, will use `existing_service_role_arn` for an existing IAM role that was created outside of this module | `bool` | `true` | no |
337343
| <a name="input_slave_allowed_cidr_blocks"></a> [slave\_allowed\_cidr\_blocks](#input\_slave\_allowed\_cidr\_blocks) | List of CIDR blocks to be allowed to access the slave instances | `list(string)` | `[]` | no |
338344
| <a name="input_slave_allowed_security_groups"></a> [slave\_allowed\_security\_groups](#input\_slave\_allowed\_security\_groups) | List of security groups to be allowed to connect to the slave instances | `list(string)` | `[]` | no |
339345
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |

docs/terraform.md

+6
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@
9191
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
9292
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
9393
| <a name="input_ebs_root_volume_size"></a> [ebs\_root\_volume\_size](#input\_ebs\_root\_volume\_size) | Size in GiB of the EBS root device volume of the Linux AMI that is used for each EC2 instance. Available in Amazon EMR version 4.x and later | `number` | `10` | no |
94+
| <a name="input_ec2_autoscaling_role_enabled"></a> [ec2\_autoscaling\_role\_enabled](#input\_ec2\_autoscaling\_role\_enabled) | If set to `false`, will use `existing_ec2_autoscaling_role_arn` for an existing EC2 autoscaling IAM role that was created outside of this module | `bool` | `true` | no |
95+
| <a name="input_ec2_role_enabled"></a> [ec2\_role\_enabled](#input\_ec2\_role\_enabled) | If set to `false`, will use `existing_ec2_instance_profile_arn` for an existing EC2 IAM role that was created outside of this module | `bool` | `true` | no |
9496
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
9597
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
98+
| <a name="input_existing_ec2_autoscaling_role_arn"></a> [existing\_ec2\_autoscaling\_role\_arn](#input\_existing\_ec2\_autoscaling\_role\_arn) | ARN of an existing EC2 autoscaling role to attach to the cluster | `string` | `""` | no |
99+
| <a name="input_existing_ec2_instance_profile_arn"></a> [existing\_ec2\_instance\_profile\_arn](#input\_existing\_ec2\_instance\_profile\_arn) | ARN of an existing EC2 instance profile | `string` | `""` | no |
100+
| <a name="input_existing_service_role_arn"></a> [existing\_service\_role\_arn](#input\_existing\_service\_role\_arn) | ARN of an existing EMR service role to attach to the cluster | `string` | `""` | no |
96101
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
97102
| <a name="input_keep_job_flow_alive_when_no_steps"></a> [keep\_job\_flow\_alive\_when\_no\_steps](#input\_keep\_job\_flow\_alive\_when\_no\_steps) | Switch on/off run cluster with no steps or when all steps are complete | `bool` | `true` | no |
98103
| <a name="input_kerberos_ad_domain_join_password"></a> [kerberos\_ad\_domain\_join\_password](#input\_kerberos\_ad\_domain\_join\_password) | The Active Directory password for ad\_domain\_join\_user. Terraform cannot perform drift detection of this configuration. | `string` | `null` | no |
@@ -128,6 +133,7 @@
128133
| <a name="input_scale_down_behavior"></a> [scale\_down\_behavior](#input\_scale\_down\_behavior) | The way that individual Amazon EC2 instances terminate when an automatic scale-in activity occurs or an instance group is resized | `string` | `null` | no |
129134
| <a name="input_security_configuration"></a> [security\_configuration](#input\_security\_configuration) | The security configuration name to attach to the EMR cluster. Only valid for EMR clusters with `release_label` 4.8.0 or greater. See https://www.terraform.io/docs/providers/aws/r/emr_security_configuration.html for more info | `string` | `null` | no |
130135
| <a name="input_service_access_security_group"></a> [service\_access\_security\_group](#input\_service\_access\_security\_group) | The name of the existing additional security group that will be used for EMR core & task nodes. If empty, a new security group will be created | `string` | `""` | no |
136+
| <a name="input_service_role_enabled"></a> [service\_role\_enabled](#input\_service\_role\_enabled) | If set to `false`, will use `existing_service_role_arn` for an existing IAM role that was created outside of this module | `bool` | `true` | no |
131137
| <a name="input_slave_allowed_cidr_blocks"></a> [slave\_allowed\_cidr\_blocks](#input\_slave\_allowed\_cidr\_blocks) | List of CIDR blocks to be allowed to access the slave instances | `list(string)` | `[]` | no |
132138
| <a name="input_slave_allowed_security_groups"></a> [slave\_allowed\_security\_groups](#input\_slave\_allowed\_security\_groups) | List of security groups to be allowed to connect to the slave instances | `list(string)` | `[]` | no |
133139
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |

main.tf

+12-12
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ This role is required for all clusters.
264264
https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
265265
*/
266266
data "aws_iam_policy_document" "assume_role_emr" {
267-
count = module.this.enabled ? 1 : 0
267+
count = module.this.enabled && var.service_role_enabled ? 1 : 0
268268

269269
statement {
270270
effect = "Allow"
@@ -279,7 +279,7 @@ data "aws_iam_policy_document" "assume_role_emr" {
279279
}
280280

281281
resource "aws_iam_role" "emr" {
282-
count = module.this.enabled ? 1 : 0
282+
count = module.this.enabled && var.service_role_enabled ? 1 : 0
283283
name = module.label_emr.id
284284
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_emr.*.json)
285285

@@ -288,7 +288,7 @@ resource "aws_iam_role" "emr" {
288288

289289
# https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
290290
resource "aws_iam_role_policy_attachment" "emr" {
291-
count = module.this.enabled ? 1 : 0
291+
count = module.this.enabled && var.service_role_enabled ? 1 : 0
292292
role = join("", aws_iam_role.emr.*.name)
293293
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole"
294294
}
@@ -301,7 +301,7 @@ This role is required for all clusters.
301301
https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
302302
*/
303303
data "aws_iam_policy_document" "assume_role_ec2" {
304-
count = module.this.enabled ? 1 : 0
304+
count = module.this.enabled && var.ec2_role_enabled ? 1 : 0
305305

306306
statement {
307307
effect = "Allow"
@@ -316,7 +316,7 @@ data "aws_iam_policy_document" "assume_role_ec2" {
316316
}
317317

318318
resource "aws_iam_role" "ec2" {
319-
count = module.this.enabled ? 1 : 0
319+
count = module.this.enabled && var.ec2_role_enabled ? 1 : 0
320320
name = module.label_ec2.id
321321
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_ec2.*.json)
322322

@@ -325,13 +325,13 @@ resource "aws_iam_role" "ec2" {
325325

326326
# https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
327327
resource "aws_iam_role_policy_attachment" "ec2" {
328-
count = module.this.enabled ? 1 : 0
328+
count = module.this.enabled && var.ec2_role_enabled ? 1 : 0
329329
role = join("", aws_iam_role.ec2.*.name)
330330
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"
331331
}
332332

333333
resource "aws_iam_instance_profile" "ec2" {
334-
count = module.this.enabled ? 1 : 0
334+
count = module.this.enabled && var.ec2_role_enabled ? 1 : 0
335335
name = join("", aws_iam_role.ec2.*.name)
336336
role = join("", aws_iam_role.ec2.*.name)
337337
}
@@ -342,7 +342,7 @@ This role is required for all clusters.
342342
https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
343343
*/
344344
resource "aws_iam_role" "ec2_autoscaling" {
345-
count = module.this.enabled ? 1 : 0
345+
count = module.this.enabled && var.ec2_autoscaling_role_enabled ? 1 : 0
346346
name = module.label_ec2_autoscaling.id
347347
assume_role_policy = join("", data.aws_iam_policy_document.assume_role_emr.*.json)
348348

@@ -351,7 +351,7 @@ resource "aws_iam_role" "ec2_autoscaling" {
351351

352352
# https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles.html
353353
resource "aws_iam_role_policy_attachment" "ec2_autoscaling" {
354-
count = module.this.enabled ? 1 : 0
354+
count = module.this.enabled && var.ec2_autoscaling_role_enabled ? 1 : 0
355355
role = join("", aws_iam_role.ec2_autoscaling.*.name)
356356
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforAutoScalingRole"
357357
}
@@ -392,7 +392,7 @@ resource "aws_emr_cluster" "default" {
392392
emr_managed_master_security_group = var.use_existing_managed_master_security_group == false ? join("", aws_security_group.managed_master.*.id) : var.managed_master_security_group
393393
emr_managed_slave_security_group = var.use_existing_managed_slave_security_group == false ? join("", aws_security_group.managed_slave.*.id) : var.managed_slave_security_group
394394
service_access_security_group = var.use_existing_service_access_security_group == false && var.subnet_type == "private" ? join("", aws_security_group.managed_service_access.*.id) : var.service_access_security_group
395-
instance_profile = join("", aws_iam_instance_profile.ec2.*.arn)
395+
instance_profile = var.ec2_role_enabled ? join("", aws_iam_instance_profile.ec2.*.arn) : var.existing_ec2_instance_profile_arn
396396
additional_master_security_groups = var.use_existing_additional_master_security_group == false ? join("", aws_security_group.master.*.id) : var.additional_master_security_group
397397
additional_slave_security_groups = var.use_existing_additional_slave_security_group == false ? join("", aws_security_group.slave.*.id) : var.additional_slave_security_group
398398
}
@@ -479,8 +479,8 @@ resource "aws_emr_cluster" "default" {
479479

480480
log_uri = var.log_uri
481481

482-
service_role = join("", aws_iam_role.emr.*.arn)
483-
autoscaling_role = join("", aws_iam_role.ec2_autoscaling.*.arn)
482+
service_role = var.service_role_enabled ? join("", aws_iam_role.emr.*.arn) : var.existing_service_role_arn
483+
autoscaling_role = var.ec2_autoscaling_role_enabled ? join("", aws_iam_role.ec2_autoscaling.*.arn) : var.existing_ec2_autoscaling_role_arn
484484

485485
tags = module.this.tags
486486

outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ output "master_host" {
2929
}
3030

3131
output "ec2_role" {
32-
value = join("", aws_iam_role.ec2.*.name)
32+
value = var.ec2_role_enabled ? join("", aws_iam_role.ec2.*.name) : null
3333
description = "Role name of EMR EC2 instances so users can attach more policies"
3434
}

0 commit comments

Comments
 (0)