Skip to content

feat: Allow passing in custom instance profile role #30

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

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ Examples codified under the [`examples`](https://github.com/terraform-aws-module
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down Expand Up @@ -402,6 +402,7 @@ No modules.
| <a name="input_iam_instance_profile_description"></a> [iam\_instance\_profile\_description](#input\_iam\_instance\_profile\_description) | Description of the EC2 IAM role/instance profile | `string` | `null` | no |
| <a name="input_iam_instance_profile_name"></a> [iam\_instance\_profile\_name](#input\_iam\_instance\_profile\_name) | Name to use on EC2 IAM role/instance profile created | `string` | `null` | no |
| <a name="input_iam_instance_profile_policies"></a> [iam\_instance\_profile\_policies](#input\_iam\_instance\_profile\_policies) | Map of IAM policies to attach to the EC2 IAM role/instance profile | `map(string)` | <pre>{<br> "AmazonElasticMapReduceforEC2Role": "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"<br>}</pre> | no |
| <a name="input_iam_instance_profile_role_arn"></a> [iam\_instance\_profile\_role\_arn](#input\_iam\_instance\_profile\_role\_arn) | The ARN of an existing IAM role to use if passing in a custom instance profile and creating a service role | `string` | `null` | no |
| <a name="input_iam_role_path"></a> [iam\_role\_path](#input\_iam\_role\_path) | IAM role path | `string` | `null` | no |
| <a name="input_iam_role_permissions_boundary"></a> [iam\_role\_permissions\_boundary](#input\_iam\_role\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the IAM role | `string` | `null` | no |
| <a name="input_iam_role_tags"></a> [iam\_role\_tags](#input\_iam\_role\_tags) | A map of additional tags to add to the IAM role created | `map(string)` | `{}` | no |
Expand Down
14 changes: 12 additions & 2 deletions examples/private-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Note that this example may create resources which will incur monetary charges on
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand All @@ -52,7 +52,17 @@ Note that this example may create resources which will incur monetary charges on

| Name | Type |
|------|------|
| [aws_iam_instance_profile.custom_instance_profile](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
| [aws_iam_role.autoscaling](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role.custom_instance_profile](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.autoscaling](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.emr_for_ec2](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.assume](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.autoscaling](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

Expand Down
83 changes: 81 additions & 2 deletions examples/private-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ provider "aws" {

data "aws_availability_zones" "available" {}

data "aws_partition" "current" {}

data "aws_caller_identity" "current" {}

data "aws_region" "current" {}

locals {
name = replace(basename(path.cwd), "-cluster", "")
region = "eu-west-1"
Expand Down Expand Up @@ -158,7 +164,9 @@ module "emr_instance_fleet" {
module "emr_instance_group" {
source = "../.."

name = "${local.name}-instance-group"
name = "${local.name}-instance-group"
create_iam_instance_profile = false
create_autoscaling_iam_role = false

release_label_filters = {
emr6 = {
Expand Down Expand Up @@ -230,8 +238,12 @@ module "emr_instance_group" {
ebs_root_volume_size = 64
ec2_attributes = {
# Instance groups only support one Subnet/AZ
subnet_id = element(module.vpc.private_subnets, 0)
subnet_id = element(module.vpc.private_subnets, 0)
instance_profile = aws_iam_instance_profile.custom_instance_profile.arn
}
iam_instance_profile_role_arn = aws_iam_role.custom_instance_profile.arn
autoscaling_iam_role_arn = aws_iam_role.autoscaling.arn

vpc_id = module.vpc.vpc_id

keep_job_flow_alive_when_no_steps = true
Expand Down Expand Up @@ -357,3 +369,70 @@ module "s3_bucket" {

tags = local.tags
}

resource "aws_iam_role" "custom_instance_profile" {
name = "custom-instance-profile"
assume_role_policy = data.aws_iam_policy_document.assume.json
}

data "aws_iam_policy_document" "assume" {
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
}
}

resource "aws_iam_role_policy_attachment" "emr_for_ec2" {
role = aws_iam_role.custom_instance_profile.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"
}

resource "aws_iam_instance_profile" "custom_instance_profile" {
role = aws_iam_role.custom_instance_profile.name

name = "custom-instance-profile"

depends_on = [
aws_iam_role_policy_attachment.emr_for_ec2,
]
}

resource "aws_iam_role" "autoscaling" {
name = "custom-autoscaling-role"
assume_role_policy = data.aws_iam_policy_document.autoscaling.json
}

data "aws_iam_policy_document" "autoscaling" {
statement {
sid = "EMRAssumeRole"
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = [
"elasticmapreduce.${data.aws_partition.current.dns_suffix}",
"application-autoscaling.${data.aws_partition.current.dns_suffix}"
]
}

condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [data.aws_caller_identity.current.account_id]
}

condition {
test = "ArnLike"
variable = "aws:SourceArn"
values = ["arn:aws:elasticmapreduce:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:*"]
}
}
}

resource "aws_iam_role_policy_attachment" "autoscaling" {
role = aws_iam_role.autoscaling.name
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonElasticMapReduceforAutoScalingRole"
}
2 changes: 1 addition & 1 deletion examples/private-cluster/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions examples/public-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Note that this example may create resources which will incur monetary charges on
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/public-cluster/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions examples/serverless-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Note that this example may create resources which will incur monetary charges on
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/serverless-cluster/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions examples/studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ $ terraform apply
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion examples/studio/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions examples/virtual-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ aws emr-containers list-virtual-clusters --region us-west-2 --states ARRESTED \
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.17 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.0 |
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.7 |
Expand All @@ -54,7 +54,7 @@ aws emr-containers list-virtual-clusters --region us-west-2 --states ARRESTED \

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |
| <a name="provider_null"></a> [null](#provider\_null) | >= 3.0 |
| <a name="provider_time"></a> [time](#provider\_time) | >= 0.7 |

Expand Down
2 changes: 1 addition & 1 deletion examples/virtual-cluster/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ data "aws_iam_policy_document" "service_pass_role" {
resources = compact([
try(aws_iam_role.autoscaling[0].arn, ""),
try(aws_iam_role.instance_profile[0].arn, ""),
var.autoscaling_iam_role_arn,
var.iam_instance_profile_role_arn
])

condition {
Expand Down
4 changes: 2 additions & 2 deletions modules/serverless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ Examples codified under the [`examples`](https://github.com/terraform-aws-module
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion modules/serverless/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions modules/studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ Examples codified under the [`examples`](https://github.com/terraform-aws-module
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion modules/studio/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}
4 changes: 2 additions & 2 deletions modules/virtual-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ Examples codified under the [`examples`](https://github.com/terraform-aws-module
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.59 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.65 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.10 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.59 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.65 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | >= 2.10 |

## Modules
Expand Down
2 changes: 1 addition & 1 deletion modules/virtual-cluster/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
kubernetes = {
source = "hashicorp/kubernetes"
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ variable "iam_instance_profile_policies" {
default = { AmazonElasticMapReduceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role" }
}

variable "iam_instance_profile_role_arn" {
description = "The ARN of an existing IAM role to use if passing in a custom instance profile and creating a service role"
type = string
default = null
}

################################################################################
# Managed Security Group
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.59"
version = ">= 5.65"
}
}
}