Skip to content

Commit 9260216

Browse files
committed
feat: Update EMR virtual cluster module to use latest functionality offered by provider
1 parent f1967fc commit 9260216

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

examples/virtual-cluster/main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ locals {
4343
module "complete" {
4444
source = "../../modules/virtual-cluster"
4545

46-
eks_cluster_id = module.eks.cluster_name
47-
oidc_provider_arn = module.eks.oidc_provider_arn
46+
eks_cluster_name = module.eks.cluster_name
47+
eks_oidc_provider_arn = module.eks.oidc_provider_arn
4848

4949
name = "emr-custom"
5050
create_namespace = true
@@ -67,8 +67,8 @@ module "complete" {
6767
module "default" {
6868
source = "../../modules/virtual-cluster"
6969

70-
eks_cluster_id = module.eks.cluster_name
71-
oidc_provider_arn = module.eks.oidc_provider_arn
70+
eks_cluster_name = module.eks.cluster_name
71+
eks_oidc_provider_arn = module.eks.oidc_provider_arn
7272

7373
s3_bucket_arns = [
7474
module.s3_bucket.s3_bucket_arn,
@@ -160,6 +160,9 @@ module "eks" {
160160

161161
enable_cluster_creator_admin_permissions = true
162162

163+
# Required for now until https://github.com/aws/containers-roadmap/issues/2397
164+
enable_irsa = true
165+
163166
compute_config = {
164167
enabled = true
165168
node_pools = ["general-purpose", "system"]

modules/virtual-cluster/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ See [`examples`](https://github.com/terraform-aws-modules/terraform-aws-emr/tree
1212
module "emr_virtual_cluster" {
1313
source = "terraform-aws-modules/emr/aws//modules/virtual-cluster"
1414
15+
eks_cluster_name = "example"
16+
eks_oidc_provider_arn = "arn:aws:iam::0123456789:oidc-provider/eks-example"
17+
1518
name = "emr-custom"
1619
create_namespace = true
1720
namespace = "emr-custom"
@@ -41,6 +44,9 @@ module "emr_virtual_cluster" {
4144
module "emr_virtual_cluster" {
4245
source = "terraform-aws-modules/emr/aws//modules/virtual-cluster"
4346
47+
eks_cluster_name = "example"
48+
eks_oidc_provider_arn = "arn:aws:iam::0123456789:oidc-provider/eks-example"
49+
4450
name = "emr-default"
4551
namespace = "emr-default"
4652
@@ -104,6 +110,7 @@ No modules.
104110
|------|-------------|------|---------|:--------:|
105111
| <a name="input_annotations"></a> [annotations](#input\_annotations) | A map of annotations to add to all Kubernetes resources | `map(string)` | `{}` | no |
106112
| <a name="input_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#input\_cloudwatch\_log\_group\_arn) | ARN of the log group to use for the cluster logs | `string` | `"arn:aws:logs:*:*:*"` | no |
113+
| <a name="input_cloudwatch_log_group_class"></a> [cloudwatch\_log\_group\_class](#input\_cloudwatch\_log\_group\_class) | Specified the log class of the log group. Possible values are: `STANDARD` or `INFREQUENT_ACCESS` | `string` | `null` | no |
107114
| <a name="input_cloudwatch_log_group_kms_key_id"></a> [cloudwatch\_log\_group\_kms\_key\_id](#input\_cloudwatch\_log\_group\_kms\_key\_id) | If a KMS Key ARN is set, this key will be used to encrypt the corresponding log group. Please be sure that the KMS Key has an appropriate key policy (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html) | `string` | `null` | no |
108115
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | The name of the log group. If a name is not provided, the default name format used is: `/emr-on-eks-logs/emr-workload/<NAMESPACE>` | `string` | `null` | no |
109116
| <a name="input_cloudwatch_log_group_retention_in_days"></a> [cloudwatch\_log\_group\_retention\_in\_days](#input\_cloudwatch\_log\_group\_retention\_in\_days) | Number of days to retain log events. Default retention - 7 days | `number` | `7` | no |
@@ -114,7 +121,8 @@ No modules.
114121
| <a name="input_create_iam_role"></a> [create\_iam\_role](#input\_create\_iam\_role) | Determines whether an IAM role is created for EMR on EKS job execution role | `bool` | `true` | no |
115122
| <a name="input_create_kubernetes_role"></a> [create\_kubernetes\_role](#input\_create\_kubernetes\_role) | Determines whether a Kubernetes role is created for EMR on EKS | `bool` | `true` | no |
116123
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Determines whether a Kubernetes namespace is created for EMR on EKS | `bool` | `true` | no |
117-
| <a name="input_eks_cluster_id"></a> [eks\_cluster\_id](#input\_eks\_cluster\_id) | EKS cluster ID | `string` | `""` | no |
124+
| <a name="input_eks_cluster_name"></a> [eks\_cluster\_name](#input\_eks\_cluster\_name) | EKS cluster name | `string` | `""` | no |
125+
| <a name="input_eks_oidc_provider_arn"></a> [eks\_oidc\_provider\_arn](#input\_eks\_oidc\_provider\_arn) | OIDC provider ARN for the EKS cluster | `string` | `""` | no |
118126
| <a name="input_iam_role_additional_policies"></a> [iam\_role\_additional\_policies](#input\_iam\_role\_additional\_policies) | Additional policies to be added to the job execution IAM role | `any` | `{}` | no |
119127
| <a name="input_iam_role_description"></a> [iam\_role\_description](#input\_iam\_role\_description) | Description of the job execution role | `string` | `null` | no |
120128
| <a name="input_iam_role_path"></a> [iam\_role\_path](#input\_iam\_role\_path) | Job execution IAM role path | `string` | `null` | no |
@@ -123,7 +131,7 @@ No modules.
123131
| <a name="input_labels"></a> [labels](#input\_labels) | A map of labels to add to all Kubernetes resources | `map(string)` | `{}` | no |
124132
| <a name="input_name"></a> [name](#input\_name) | Name of the EMR on EKS virtual cluster | `string` | `""` | no |
125133
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Kubernetes namespace for EMR on EKS | `string` | `"emr-containers"` | no |
126-
| <a name="input_oidc_provider_arn"></a> [oidc\_provider\_arn](#input\_oidc\_provider\_arn) | OIDC provider ARN for the EKS cluster | `string` | `""` | no |
134+
| <a name="input_region"></a> [region](#input\_region) | Region where the resource(s) will be managed. Defaults to the Region set in the provider configuration | `string` | `null` | no |
127135
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | Name to use on IAM role created for EMR on EKS job execution role as well as Kubernetes RBAC role | `string` | `null` | no |
128136
| <a name="input_s3_bucket_arns"></a> [s3\_bucket\_arns](#input\_s3\_bucket\_arns) | S3 bucket ARNs for EMR on EKS job execution role to list, get objects, and put objects | `list(string)` | `[]` | no |
129137
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |

modules/virtual-cluster/main.tf

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
data "aws_caller_identity" "current" {}
1+
data "aws_caller_identity" "current" {
2+
count = var.create ? 1 : 0
3+
}
24

35
locals {
4-
account_id = data.aws_caller_identity.current.account_id
6+
account_id = try(data.aws_caller_identity.current[0].account_id, "")
57

68
internal_role_name = try(coalesce(var.role_name, var.name), "")
79

@@ -19,10 +21,12 @@ locals {
1921
resource "aws_emrcontainers_virtual_cluster" "this" {
2022
count = var.create ? 1 : 0
2123

24+
region = var.region
25+
2226
name = var.name
2327

2428
container_provider {
25-
id = var.eks_cluster_id
29+
id = var.eks_cluster_name
2630
type = "EKS"
2731

2832
info {
@@ -145,27 +149,30 @@ locals {
145149
data "aws_iam_policy_document" "assume" {
146150
count = local.create_iam_role ? 1 : 0
147151

152+
153+
# IRSA is default and only supported authentication method for now until wildcard support is added
154+
# to EKS pod identity https://github.com/aws/containers-roadmap/issues/2397
148155
statement {
149156
sid = "IRSA"
150157
effect = "Allow"
151158
actions = ["sts:AssumeRoleWithWebIdentity"]
152159

153160
principals {
154161
type = "Federated"
155-
identifiers = [var.oidc_provider_arn]
162+
identifiers = [var.eks_oidc_provider_arn]
156163
}
157164

158165
condition {
159166
test = "StringLike"
160-
variable = "${replace(var.oidc_provider_arn, "/^(.*provider/)/", "")}:sub"
167+
variable = "${replace(var.eks_oidc_provider_arn, "/^(.*provider/)/", "")}:sub"
161168
# Terraform lacks support for a base32 function and role names with prefixes are unknown so a wildcard is used
162169
values = ["system:serviceaccount:${local.namespace}:emr-containers-sa-*-*-${local.account_id}-*"]
163170
}
164171

165172
# https://aws.amazon.com/premiumsupport/knowledge-center/eks-troubleshoot-oidc-and-irsa/?nc1=h_ls
166173
condition {
167174
test = "StringEquals"
168-
variable = "${replace(var.oidc_provider_arn, "/^(.*provider/)/", "")}:aud"
175+
variable = "${replace(var.eks_oidc_provider_arn, "/^(.*provider/)/", "")}:aud"
169176
values = ["sts.amazonaws.com"]
170177
}
171178
}
@@ -258,10 +265,13 @@ resource "aws_iam_role_policy_attachment" "additional" {
258265
resource "aws_cloudwatch_log_group" "this" {
259266
count = var.create && var.create_cloudwatch_log_group ? 1 : 0
260267

268+
region = var.region
269+
261270
name = var.cloudwatch_log_group_use_name_prefix ? null : local.cloudwatch_log_group_name
262271
name_prefix = var.cloudwatch_log_group_use_name_prefix ? "${local.cloudwatch_log_group_name}-" : null
263272
retention_in_days = var.cloudwatch_log_group_retention_in_days
264273
kms_key_id = var.cloudwatch_log_group_kms_key_id
274+
log_group_class = var.cloudwatch_log_group_class
265275
skip_destroy = var.cloudwatch_log_group_skip_destroy
266276

267277
tags = local.tags

modules/virtual-cluster/variables.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ variable "create" {
44
default = true
55
}
66

7+
variable "region" {
8+
description = "Region where the resource(s) will be managed. Defaults to the Region set in the provider configuration"
9+
type = string
10+
default = null
11+
}
12+
713
variable "tags" {
814
description = "A map of tags to add to all resources"
915
type = map(string)
@@ -32,8 +38,8 @@ variable "name" {
3238
default = ""
3339
}
3440

35-
variable "eks_cluster_id" {
36-
description = "EKS cluster ID"
41+
variable "eks_cluster_name" {
42+
description = "EKS cluster name"
3743
type = string
3844
default = ""
3945
}
@@ -70,7 +76,7 @@ variable "create_iam_role" {
7076
default = true
7177
}
7278

73-
variable "oidc_provider_arn" {
79+
variable "eks_oidc_provider_arn" {
7480
description = "OIDC provider ARN for the EKS cluster"
7581
type = string
7682
default = ""
@@ -114,7 +120,7 @@ variable "iam_role_permissions_boundary" {
114120

115121
variable "iam_role_additional_policies" {
116122
description = "Additional policies to be added to the job execution IAM role"
117-
type = any
123+
type = map(string)
118124
default = {}
119125
}
120126

@@ -146,6 +152,12 @@ variable "cloudwatch_log_group_kms_key_id" {
146152
default = null
147153
}
148154

155+
variable "cloudwatch_log_group_class" {
156+
description = "Specified the log class of the log group. Possible values are: `STANDARD` or `INFREQUENT_ACCESS`"
157+
type = string
158+
default = null
159+
}
160+
149161
variable "cloudwatch_log_group_name" {
150162
description = "The name of the log group. If a name is not provided, the default name format used is: `/emr-on-eks-logs/emr-workload/<NAMESPACE>`"
151163
type = string

0 commit comments

Comments
 (0)