Skip to content

Commit 327207a

Browse files
authored
fix: Ensure conditional creation applies to data sources (#21)
1 parent c218e0d commit 327207a

File tree

5 files changed

+30
-123
lines changed

5 files changed

+30
-123
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.2
3+
rev: v1.83.4
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

main.tf

+12-7
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ resource "helm_release" "this" {
8787
# IAM Role for Service Account(s) (IRSA)
8888
################################################################################
8989

90-
data "aws_partition" "current" {}
91-
data "aws_caller_identity" "current" {}
90+
data "aws_partition" "current" {
91+
count = local.create_role ? 1 : 0
92+
}
93+
data "aws_caller_identity" "current" {
94+
count = local.create_role ? 1 : 0
95+
}
9296

9397
locals {
9498
create_role = var.create && var.create_role
9599

96-
account_id = data.aws_caller_identity.current.account_id
97-
partition = data.aws_partition.current.partition
100+
account_id = try(data.aws_caller_identity.current[0].account_id, "*")
101+
partition = try(data.aws_partition.current[0].partition, "*")
98102

99103
role_name = try(coalesce(var.role_name, var.name), "")
100104
role_name_condition = var.role_name_use_prefix ? "${local.role_name}-*" : local.role_name
@@ -184,10 +188,11 @@ locals {
184188
create_policy = local.create_role && var.create_policy
185189

186190
policy_name = try(coalesce(var.policy_name, local.role_name), "")
191+
perms = concat(var.source_policy_documents, var.override_policy_documents, var.policy_statements)
187192
}
188193

189194
data "aws_iam_policy_document" "this" {
190-
count = local.create_policy ? 1 : 0
195+
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
191196

192197
source_policy_documents = var.source_policy_documents
193198
override_policy_documents = var.override_policy_documents
@@ -235,7 +240,7 @@ data "aws_iam_policy_document" "this" {
235240
}
236241

237242
resource "aws_iam_policy" "this" {
238-
count = local.create_policy ? 1 : 0
243+
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
239244

240245
name = var.policy_name_use_prefix ? null : local.policy_name
241246
name_prefix = var.policy_name_use_prefix ? "${local.policy_name}-" : null
@@ -247,7 +252,7 @@ resource "aws_iam_policy" "this" {
247252
}
248253

249254
resource "aws_iam_role_policy_attachment" "this" {
250-
count = local.create_policy ? 1 : 0
255+
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
251256

252257
role = aws_iam_role.this[0].name
253258
policy_arn = aws_iam_policy.this[0].arn

tests/README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,32 @@ Note that this example may create resources which will incur monetary charges on
2626
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
2727
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.47 |
2828
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.9 |
29-
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | >= 1.14 |
3029

3130
## Providers
3231

3332
| Name | Version |
3433
|------|---------|
3534
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.47 |
36-
| <a name="provider_kubectl"></a> [kubectl](#provider\_kubectl) | >= 1.14 |
3735

3836
## Modules
3937

4038
| Name | Source | Version |
4139
|------|--------|---------|
4240
| <a name="module_disabled"></a> [disabled](#module\_disabled) | ../ | n/a |
43-
| <a name="module_eks"></a> [eks](#module\_eks) | terraform-aws-modules/eks/aws | ~> 19.10 |
41+
| <a name="module_eks"></a> [eks](#module\_eks) | terraform-aws-modules/eks/aws | ~> 19.16 |
4442
| <a name="module_helm_release_irsa"></a> [helm\_release\_irsa](#module\_helm\_release\_irsa) | ../ | n/a |
4543
| <a name="module_helm_release_only"></a> [helm\_release\_only](#module\_helm\_release\_only) | ../ | n/a |
4644
| <a name="module_irsa_only"></a> [irsa\_only](#module\_irsa\_only) | ../ | n/a |
47-
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 4.0 |
45+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
4846

4947
## Resources
5048

5149
| Name | Type |
5250
|------|------|
5351
| [aws_iam_instance_profile.karpenter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
5452
| [aws_iam_policy.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
55-
| [kubectl_manifest.karpenter_example_deployment](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
56-
| [kubectl_manifest.karpenter_node_template](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
57-
| [kubectl_manifest.karpenter_provisioner](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
5853
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
5954
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
60-
| [aws_eks_cluster_auth.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |
6155
| [aws_iam_policy_document.karpenter_controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
6256

6357
## Inputs

tests/main.tf

+15-103
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ provider "helm" {
66
kubernetes {
77
host = module.eks.cluster_endpoint
88
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
9-
token = data.aws_eks_cluster_auth.this.token
10-
}
11-
}
129

13-
provider "kubectl" {
14-
apply_retry_count = 30
15-
host = module.eks.cluster_endpoint
16-
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
17-
load_config_file = false
18-
token = data.aws_eks_cluster_auth.this.token
19-
}
20-
21-
data "aws_eks_cluster_auth" "this" {
22-
name = module.eks.cluster_name
10+
exec {
11+
api_version = "client.authentication.k8s.io/v1beta1"
12+
command = "aws"
13+
# This requires the awscli to be installed locally where Terraform is executed
14+
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
15+
}
16+
}
2317
}
2418

2519
data "aws_caller_identity" "current" {}
@@ -157,21 +151,22 @@ module "disabled" {
157151

158152
module "eks" {
159153
source = "terraform-aws-modules/eks/aws"
160-
version = "~> 19.10"
154+
version = "~> 19.16"
161155

162-
cluster_name = local.name
163-
cluster_version = "1.24"
156+
cluster_name = local.name
157+
cluster_version = "1.27"
158+
cluster_endpoint_public_access = true
164159

165160
vpc_id = module.vpc.vpc_id
166161
subnet_ids = module.vpc.private_subnets
167162

168163
eks_managed_node_groups = {
169164
initial = {
170-
instance_types = ["m5.xlarge"]
165+
instance_types = ["m5.large"]
171166

172167
min_size = 1
173-
max_size = 2
174-
desired_size = 1
168+
max_size = 3
169+
desired_size = 2
175170
}
176171
}
177172

@@ -185,7 +180,7 @@ module "eks" {
185180

186181
module "vpc" {
187182
source = "terraform-aws-modules/vpc/aws"
188-
version = "~> 4.0"
183+
version = "~> 5.0"
189184

190185
name = local.name
191186
cidr = local.vpc_cidr
@@ -218,7 +213,6 @@ resource "aws_iam_instance_profile" "karpenter" {
218213
}
219214

220215
data "aws_iam_policy_document" "karpenter_controller" {
221-
# # checkov:skip=CKV_AWS_111
222216
statement {
223217
actions = [
224218
"ec2:CreateLaunchTemplate",
@@ -298,85 +292,3 @@ resource "aws_iam_policy" "karpenter_controller" {
298292

299293
tags = local.tags
300294
}
301-
302-
################################################################################
303-
# Karpenter Provisioner
304-
################################################################################
305-
306-
# Workaround - https://github.com/hashicorp/terraform-provider-kubernetes/issues/1380#issuecomment-967022975
307-
resource "kubectl_manifest" "karpenter_provisioner" {
308-
yaml_body = <<-YAML
309-
---
310-
apiVersion: karpenter.sh/v1alpha5
311-
kind: Provisioner
312-
metadata:
313-
name: default
314-
spec:
315-
requirements:
316-
- key: karpenter.sh/capacity-type
317-
operator: In
318-
values: ["spot"]
319-
limits:
320-
resources:
321-
cpu: 1000
322-
providerRef:
323-
name: default
324-
ttlSecondsAfterEmpty: 30
325-
YAML
326-
327-
depends_on = [
328-
module.helm_release_irsa.helm_release
329-
]
330-
}
331-
332-
resource "kubectl_manifest" "karpenter_node_template" {
333-
yaml_body = <<-YAML
334-
apiVersion: karpenter.k8s.aws/v1alpha1
335-
kind: AWSNodeTemplate
336-
metadata:
337-
name: default
338-
spec:
339-
subnetSelector:
340-
${local.karpenter_tag_key}: ${module.eks.cluster_name}
341-
securityGroupSelector:
342-
${local.karpenter_tag_key}: ${module.eks.cluster_name}
343-
tags:
344-
${local.karpenter_tag_key}: ${module.eks.cluster_name}
345-
YAML
346-
347-
depends_on = [
348-
kubectl_manifest.karpenter_provisioner
349-
]
350-
}
351-
352-
# Example deployment using the [pause image](https://www.ianlewis.org/en/almighty-pause-container)
353-
# and starts with zero replicas
354-
resource "kubectl_manifest" "karpenter_example_deployment" {
355-
yaml_body = <<-YAML
356-
apiVersion: apps/v1
357-
kind: Deployment
358-
metadata:
359-
name: inflate
360-
spec:
361-
replicas: 0
362-
selector:
363-
matchLabels:
364-
app: inflate
365-
template:
366-
metadata:
367-
labels:
368-
app: inflate
369-
spec:
370-
terminationGracePeriodSeconds: 0
371-
containers:
372-
- name: inflate
373-
image: public.ecr.aws/eks-distro/kubernetes/pause:3.2
374-
resources:
375-
requests:
376-
cpu: 1
377-
YAML
378-
379-
depends_on = [
380-
kubectl_manifest.karpenter_node_template
381-
]
382-
}

tests/versions.tf

-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ terraform {
1010
source = "hashicorp/helm"
1111
version = ">= 2.9"
1212
}
13-
kubectl = {
14-
source = "gavinbunney/kubectl"
15-
version = ">= 1.14"
16-
}
1713
}
1814
}

0 commit comments

Comments
 (0)