Skip to content

Commit d7377b6

Browse files
authored
Update Ubuntu AMI query (#215)
* Fix ami * Update atmos.yaml * Update main.tf
1 parent bf781a3 commit d7377b6

File tree

9 files changed

+96
-307
lines changed

9 files changed

+96
-307
lines changed

cloudwatch-alarm.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "null_resource" "check_alarm_action" {
44
count = var.disable_alarm_action ? 0 : local.instance_count
55

66
triggers = {
7-
action = "arn:${data.aws_partition.default.partition}:swf:${local.region}:${data.aws_caller_identity.default.account_id}:${var.default_alarm_action}"
7+
action = "arn:${try(data.aws_partition.default[0].partition, null)}:swf:${local.region}:${try(data.aws_caller_identity.default[0].account_id, null)}:${var.default_alarm_action}"
88
}
99
}
1010

examples/complete/fixtures.us-east-2.tfvars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
enabled = true
1+
# enabled = true
22

33
region = "us-east-2"
44

@@ -54,6 +54,6 @@ security_group_rules = [
5454
},
5555
]
5656

57-
ssh_public_key_path = "/secrets"
57+
ssh_public_key_path = "/tmp/secrets"
5858

5959
metric_treat_missing_data = "notBreaching"

examples/complete/main.tf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module "aws_key_pair" {
1111
attributes = module.this.attributes
1212
ssh_public_key_path = var.ssh_public_key_path
1313
generate_ssh_key = true
14+
15+
context = module.this.context
1416
}
1517

1618
module "vpc" {
@@ -24,7 +26,7 @@ module "vpc" {
2426

2527
module "subnets" {
2628
source = "cloudposse/dynamic-subnets/aws"
27-
version = "2.3.0"
29+
version = "2.4.2"
2830

2931
availability_zones = var.availability_zones
3032
vpc_id = module.vpc.vpc_id
@@ -46,6 +48,7 @@ module "instance_profile_label" {
4648
}
4749

4850
data "aws_iam_policy_document" "test" {
51+
count = module.this.enabled ? 1 : 0
4952
statement {
5053
effect = "Allow"
5154

@@ -61,29 +64,33 @@ data "aws_iam_policy_document" "test" {
6164
}
6265

6366
resource "aws_iam_role" "test" {
67+
count = module.this.enabled ? 1 : 0
68+
6469
name = module.instance_profile_label.id
65-
assume_role_policy = data.aws_iam_policy_document.test.json
70+
assume_role_policy = one(data.aws_iam_policy_document.test[*].json)
6671
tags = module.instance_profile_label.tags
6772
}
6873

6974
# https://github.com/hashicorp/terraform-guides/tree/master/infrastructure-as-code/terraform-0.13-examples/module-depends-on
7075
resource "aws_iam_instance_profile" "test" {
76+
count = module.this.enabled ? 1 : 0
77+
7178
name = module.instance_profile_label.id
72-
role = aws_iam_role.test.name
79+
role = aws_iam_role.test[0].name
7380
}
7481

7582
module "ec2_instance" {
7683
source = "../../"
7784

7885
ssh_key_pair = module.aws_key_pair.key_name
7986
vpc_id = module.vpc.vpc_id
80-
subnet = module.subnets.private_subnet_ids[0]
87+
subnet = module.this.enabled ? module.subnets.private_subnet_ids[0] : null
8188
security_groups = [module.vpc.vpc_default_security_group_id]
8289
assign_eip_address = var.assign_eip_address
8390
associate_public_ip_address = var.associate_public_ip_address
8491
instance_type = var.instance_type
8592
security_group_rules = var.security_group_rules
86-
instance_profile = aws_iam_instance_profile.test.name
93+
instance_profile = module.this.enabled ? aws_iam_instance_profile.test[0].name : null
8794
tenancy = var.tenancy
8895
metric_treat_missing_data = var.metric_treat_missing_data
8996

examples/external-eni/fixtures.us-east-2.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ security_group_rules = [
5454
},
5555
]
5656

57-
ssh_public_key_path = "/secrets"
57+
ssh_public_key_path = "/tmp/secrets"

main.tf

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,47 @@ locals {
33
instance_count = local.enabled ? 1 : 0
44
volume_count = var.ebs_volume_count > 0 && local.instance_count > 0 ? var.ebs_volume_count : 0
55
# create an instance profile if the instance is enabled and we aren't given one to use
6-
instance_profile_count = module.this.enabled && var.instance_profile_enabled && var.instance_profile == "" ? 1 : 0
6+
instance_profile_count = local.enabled && var.instance_profile_enabled && var.instance_profile == "" ? 1 : 0
77
instance_profile = var.instance_profile_enabled && var.instance_profile != "" ? var.instance_profile : (var.instance_profile_enabled ? one(aws_iam_instance_profile.default[*].name) : "")
8-
security_group_enabled = module.this.enabled && var.security_group_enabled
9-
region = var.region != "" ? var.region : data.aws_region.default.name
8+
security_group_enabled = local.enabled && var.security_group_enabled
9+
region = local.enabled ? coalesce(var.region, one(data.aws_region.default[*].name)) : ""
1010
root_iops = contains(["io1", "io2", "gp3"], var.root_volume_type) ? var.root_iops : null
1111
ebs_iops = contains(["io1", "io2", "gp3"], var.ebs_volume_type) ? var.ebs_iops : null
1212
root_throughput = var.root_volume_type == "gp3" ? var.root_throughput : null
1313
ebs_throughput = var.ebs_volume_type == "gp3" ? var.ebs_throughput : null
14-
availability_zone = var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone
14+
availability_zone = var.availability_zone != "" ? var.availability_zone : try(data.aws_subnet.default[0].availability_zone, null)
1515
ami = var.ami != "" ? var.ami : one(data.aws_ami.default[*].image_id)
1616
ami_owner = var.ami != "" ? var.ami_owner : one(data.aws_ami.default[*].owner_id)
1717
root_volume_type = var.root_volume_type != "" ? var.root_volume_type : one(data.aws_ami.info[*].root_device_type)
1818

1919
region_domain = local.region == "us-east-1" ? "compute-1.amazonaws.com" : "${local.region}.compute.amazonaws.com"
20-
eip_public_dns = var.associate_public_ip_address && var.assign_eip_address && module.this.enabled ? "ec2-${replace(one(aws_eip.default[*].public_ip), ".", "-")}.${local.region_domain}" : ""
20+
eip_public_dns = var.associate_public_ip_address && var.assign_eip_address && local.enabled ? "ec2-${replace(one(aws_eip.default[*].public_ip), ".", "-")}.${local.region_domain}" : ""
2121
public_dns = (
22-
var.associate_public_ip_address && var.assign_eip_address && module.this.enabled ?
22+
var.associate_public_ip_address && var.assign_eip_address && local.enabled ?
2323
local.eip_public_dns : one(aws_instance.default[*].public_dns)
2424
)
2525
}
2626

2727
data "aws_caller_identity" "default" {
28+
count = local.enabled ? 1 : 0
2829
}
2930

3031
data "aws_region" "default" {
32+
count = local.enabled ? 1 : 0
3133
}
3234

3335
data "aws_partition" "default" {
36+
count = local.enabled ? 1 : 0
3437
}
3538

3639
data "aws_subnet" "default" {
37-
id = var.subnet
40+
count = local.enabled ? 1 : 0
41+
id = var.subnet
3842
}
3943

4044
data "aws_iam_policy_document" "default" {
45+
count = local.enabled ? 1 : 0
46+
4147
statement {
4248
sid = ""
4349

@@ -54,12 +60,12 @@ data "aws_iam_policy_document" "default" {
5460
}
5561
}
5662
data "aws_ami" "default" {
57-
count = var.ami == "" ? 1 : 0
63+
count = local.enabled && var.ami == "" ? 1 : 0
5864
most_recent = "true"
5965

6066
filter {
6167
name = "name"
62-
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
68+
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
6369
}
6470

6571
filter {
@@ -71,7 +77,7 @@ data "aws_ami" "default" {
7177
}
7278

7379
data "aws_ami" "info" {
74-
count = var.root_volume_type != "" ? 0 : 1
80+
count = local.enabled && var.root_volume_type == "" ? 1 : 0
7581

7682
filter {
7783
name = "image-id"
@@ -95,10 +101,10 @@ resource "aws_iam_instance_profile" "default" {
95101
}
96102

97103
resource "aws_iam_role" "default" {
98-
count = var.instance_profile_enabled ? local.instance_profile_count : 0
104+
count = local.enabled && var.instance_profile_enabled ? local.instance_profile_count : 0
99105
name = module.this.id
100106
path = "/"
101-
assume_role_policy = data.aws_iam_policy_document.default.json
107+
assume_role_policy = one(data.aws_iam_policy_document.default[*].json)
102108
permissions_boundary = var.permissions_boundary_arn
103109
tags = module.this.tags
104110
}
@@ -175,7 +181,7 @@ resource "aws_instance" "default" {
175181

176182
resource "aws_eip" "default" {
177183
#bridgecrew:skip=BC_AWS_NETWORKING_48: Skiping `Ensure all EIP addresses allocated to a VPC are attached to EC2 instances` because it is incorrectly flagging that this instance does not belong to a VPC even though subnet_id is configured.
178-
count = var.associate_public_ip_address && var.assign_eip_address && module.this.enabled ? 1 : 0
184+
count = var.associate_public_ip_address && var.assign_eip_address && local.enabled ? 1 : 0
179185
instance = one(aws_instance.default[*].id)
180186
tags = module.this.tags
181187
}

ssm_patch.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
locals {
33
ssm_patch_log_bucket_enabled = local.ssm_enabled && var.ssm_patch_manager_s3_log_bucket != "" && var.ssm_patch_manager_s3_log_bucket != null
4-
ssm_policy_arn = var.ssm_patch_manager_iam_policy_arn == null || var.ssm_patch_manager_iam_policy_arn == "" ? "arn:${data.aws_partition.default.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore" : var.ssm_patch_manager_iam_policy_arn
4+
partition = module.this.enabled ? one(data.aws_partition.default[*].partition) : ""
5+
ssm_policy_arn = var.ssm_patch_manager_iam_policy_arn == null || var.ssm_patch_manager_iam_policy_arn == "" ? "arn:${local.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore" : var.ssm_patch_manager_iam_policy_arn
56
ssm_enabled = local.enabled && var.ssm_patch_manager_enabled
67
}
78

@@ -25,8 +26,8 @@ data "aws_iam_policy_document" "ssm_patch_s3_log_policy" {
2526
"s3:GetEncryptionConfiguration",
2627
]
2728
resources = [
28-
"arn:${data.aws_partition.default.partition}:s3:::${var.ssm_patch_manager_s3_log_bucket}/*",
29-
"arn:${data.aws_partition.default.partition}:s3:::${var.ssm_patch_manager_s3_log_bucket}",
29+
"arn:${try(data.aws_partition.default[0].partition, null)}:s3:::${var.ssm_patch_manager_s3_log_bucket}/*",
30+
"arn:${try(data.aws_partition.default[0].partition, null)}:s3:::${var.ssm_patch_manager_s3_log_bucket}",
3031
]
3132
}
3233
}

test/src/examples_complete_test.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test
33
import (
44
"math/rand"
55
"strconv"
6+
"strings"
67
"testing"
78
"time"
89

@@ -12,8 +13,6 @@ import (
1213

1314
// Test the Terraform module in examples/complete using Terratest.
1415
func TestExamplesComplete(t *testing.T) {
15-
t.Parallel()
16-
1716
rand.Seed(time.Now().UnixNano())
1817

1918
randId := strconv.Itoa(rand.Intn(100000))
@@ -27,6 +26,7 @@ func TestExamplesComplete(t *testing.T) {
2726
VarFiles: []string{"fixtures.us-east-2.tfvars"},
2827
Vars: map[string]interface{}{
2928
"attributes": attributes,
29+
"enabled": "true",
3030
},
3131
}
3232

@@ -84,8 +84,6 @@ func TestExamplesComplete(t *testing.T) {
8484
}
8585

8686
func TestExternalEniComplete(t *testing.T) {
87-
t.Parallel()
88-
8987
rand.Seed(time.Now().UnixNano())
9088

9189
randId := strconv.Itoa(rand.Intn(100000))
@@ -149,3 +147,35 @@ func TestExternalEniComplete(t *testing.T) {
149147
// Verify we're getting back the outputs we expect
150148
assert.Contains(t, securityGroupARN, "arn:aws:ec2", "SG ID should contains substring 'arn:aws:ec2'")
151149
}
150+
151+
// Test the Terraform module in examples/complete using Terratest.
152+
func TestDisabled(t *testing.T) {
153+
rand.Seed(time.Now().UnixNano())
154+
155+
randId := strconv.Itoa(rand.Intn(100000))
156+
attributes := []string{randId}
157+
158+
terraformOptions := &terraform.Options{
159+
// The path to where our Terraform code is located
160+
TerraformDir: "../../examples/complete",
161+
Upgrade: true,
162+
// Variables to pass to our Terraform code using -var-file options
163+
VarFiles: []string{"fixtures.us-east-2.tfvars"},
164+
Vars: map[string]interface{}{
165+
"attributes": attributes,
166+
"enabled": "false",
167+
},
168+
}
169+
170+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
171+
defer terraform.Destroy(t, terraformOptions)
172+
173+
terraform.Init(t, terraformOptions)
174+
plan := terraform.Plan(t, terraformOptions)
175+
planContainsNoChanges := strings.Contains(plan, "No changes.") || strings.Contains(plan, "0 to add, 0 to change, 0 to destroy.") || !strings.Contains(plan, "Plan")
176+
177+
assert.True(t, planContainsNoChanges)
178+
179+
planContainsNoAMIsearch := !strings.Contains(plan, "module.ec2_instance.data.aws_ami.default[0]: Reading...") && !strings.Contains(plan, "module.ec2_instance.data.aws_ami.info[0]: Reading...")
180+
assert.True(t, planContainsNoAMIsearch)
181+
}

test/src/go.mod

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
module github.com/cloudposse/terraform-aws-ec2-instance
22

3-
go 1.14
3+
go 1.23
4+
5+
toolchain go1.23.0
46

57
require (
6-
github.com/gruntwork-io/gruntwork-cli v0.7.0 // indirect
78
github.com/gruntwork-io/terratest v0.34.7
89
github.com/stretchr/testify v1.6.1
910
)
11+
12+
require (
13+
github.com/agext/levenshtein v1.2.1 // indirect
14+
github.com/apparentlymart/go-textseg v1.0.0 // indirect
15+
github.com/apparentlymart/go-textseg/v12 v12.0.0 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/hashicorp/errwrap v1.0.0 // indirect
18+
github.com/hashicorp/go-multierror v1.1.0 // indirect
19+
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
20+
github.com/hashicorp/terraform-json v0.9.0 // indirect
21+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
22+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
23+
github.com/pmezard/go-difflib v1.0.0 // indirect
24+
github.com/zclconf/go-cty v1.2.1 // indirect
25+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
26+
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
27+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect
28+
golang.org/x/text v0.3.3 // indirect
29+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
30+
)

0 commit comments

Comments
 (0)