Skip to content

Commit 098ce2e

Browse files
authoredMar 7, 2025··
Merge pull request #41733 from hashicorp/security-terraform.lang.security.ec2-imdsv1-optional.ec2-imdsv1-optional
Fix `terraform.lang.security.ec2-imdsv1-optional.ec2-imdsv1-optional` security findings
2 parents e713509 + 3619335 commit 098ce2e

File tree

13 files changed

+58
-2
lines changed

13 files changed

+58
-2
lines changed
 

‎examples/count/main.tf

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ resource "aws_instance" "web" {
4646
instance_type = "t2.small"
4747
ami = data.aws_ami.ubuntu.id
4848

49+
# Force IMDSv2.
50+
metadata_options {
51+
http_tokens = "required"
52+
}
53+
4954
# This will create 4 instances
5055
count = 4
5156
}

‎examples/eip/main.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ resource "aws_instance" "web" {
6767
# this should be on port 80
6868
user_data = file("userdata.sh")
6969

70-
#Instance tags
70+
# Force IMDSv2.
71+
metadata_options {
72+
http_tokens = "required"
73+
}
74+
75+
# Instance tags.
7176
tags = {
7277
Name = "eip-example"
7378
}

‎examples/elb/main.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ resource "aws_instance" "web" {
172172
subnet_id = aws_subnet.tf_test_subnet.id
173173
user_data = file("userdata.sh")
174174

175-
#Instance tags
175+
# Force IMDSv2.
176+
metadata_options {
177+
http_tokens = "required"
178+
}
179+
180+
# Instance tags
176181

177182
tags = {
178183
Name = "elb-example"

‎examples/two-tier/main.tf

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ resource "aws_instance" "web" {
136136
# backend instances.
137137
subnet_id = aws_subnet.default.id
138138

139+
# Force IMDSv2.
140+
metadata_options {
141+
http_tokens = "required"
142+
}
143+
139144
# We run a remote provisioner on the instance after creating it.
140145
# In this case, we just install nginx and start it. By default,
141146
# this should be on port 80

‎internal/service/ec2/testdata/Instance/data.tags/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ resource "aws_instance" "test" {
1010
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
1111
instance_type = "t4g.nano"
1212

13+
metadata_options {
14+
http_tokens = "required"
15+
}
16+
1317
tags = var.resource_tags
1418
}
1519

‎internal/service/ec2/testdata/Instance/data.tags_defaults/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ resource "aws_instance" "test" {
1616
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
1717
instance_type = "t4g.nano"
1818

19+
metadata_options {
20+
http_tokens = "required"
21+
}
22+
1923
tags = var.resource_tags
2024
}
2125

‎internal/service/ec2/testdata/Instance/data.tags_ignore/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ resource "aws_instance" "test" {
1919
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
2020
instance_type = "t4g.nano"
2121

22+
metadata_options {
23+
http_tokens = "required"
24+
}
25+
2226
tags = var.resource_tags
2327
}
2428

‎internal/service/ec2/testdata/Instance/tags/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ resource "aws_instance" "test" {
55
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
66
instance_type = "t4g.nano"
77

8+
metadata_options {
9+
http_tokens = "required"
10+
}
11+
812
tags = var.resource_tags
913
}
1014

‎internal/service/ec2/testdata/Instance/tagsComputed1/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ resource "aws_instance" "test" {
77
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
88
instance_type = "t4g.nano"
99

10+
metadata_options {
11+
http_tokens = "required"
12+
}
13+
1014
tags = {
1115
(var.unknownTagKey) = null_resource.test.id
1216
}

‎internal/service/ec2/testdata/Instance/tagsComputed2/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ resource "aws_instance" "test" {
77
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
88
instance_type = "t4g.nano"
99

10+
metadata_options {
11+
http_tokens = "required"
12+
}
13+
1014
tags = {
1115
(var.unknownTagKey) = null_resource.test.id
1216
(var.knownTagKey) = var.knownTagValue

‎internal/service/ec2/testdata/Instance/tags_defaults/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ resource "aws_instance" "test" {
1111
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
1212
instance_type = "t4g.nano"
1313

14+
metadata_options {
15+
http_tokens = "required"
16+
}
17+
1418
tags = var.resource_tags
1519
}
1620

‎internal/service/ec2/testdata/Instance/tags_ignore/main_gen.tf

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ resource "aws_instance" "test" {
1414
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
1515
instance_type = "t4g.nano"
1616

17+
metadata_options {
18+
http_tokens = "required"
19+
}
20+
1721
tags = var.resource_tags
1822
}
1923

‎internal/service/ec2/testdata/tmpl/ec2_instance_tags.gtpl

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ resource "aws_instance" "test" {
22
ami = data.aws_ami.amzn2-ami-minimal-hvm-ebs-arm64.id
33
instance_type = "t4g.nano"
44

5+
metadata_options {
6+
http_tokens = "required"
7+
}
8+
59
{{- template "tags" . }}
610
}
711

0 commit comments

Comments
 (0)
Please sign in to comment.