@@ -59,8 +59,8 @@ module "ec2_complete" {
5959 threads_per_core = 1
6060 }
6161 enable_volume_tags = false
62- root_block_device = [
63- {
62+ root_block_device = {
63+ main = {
6464 encrypted = true
6565 volume_type = " gp3"
6666 throughput = 200
@@ -69,11 +69,10 @@ module "ec2_complete" {
6969 Name = " my-root-block"
7070 }
7171 },
72- ]
72+ }
7373
74- ebs_block_device = [
75- {
76- device_name = " /dev/sdf"
74+ ebs_volumes = {
75+ " /dev/sdf" = {
7776 volume_type = " gp3"
7877 volume_size = 5
7978 throughput = 200
@@ -83,7 +82,7 @@ module "ec2_complete" {
8382 MountPoint = " /mnt/data"
8483 }
8584 }
86- ]
85+ }
8786
8887 tags = local. tags
8988}
@@ -93,13 +92,12 @@ module "ec2_network_interface" {
9392
9493 name = " ${ local . name } -network-interface"
9594
96- network_interface = [
97- {
98- device_index = 0
95+ network_interface = {
96+ 0 = {
9997 network_interface_id = aws_network_interface.this.id
10098 delete_on_termination = false
10199 }
102- ]
100+ }
103101
104102 tags = local. tags
105103}
@@ -109,8 +107,7 @@ module "ec2_metadata_options" {
109107
110108 name = " ${ local . name } -metadata-options"
111109
112- subnet_id = element (module. vpc . private_subnets , 0 )
113- vpc_security_group_ids = [module . security_group . security_group_id ]
110+ subnet_id = element (module. vpc . private_subnets , 0 )
114111
115112 metadata_options = {
116113 http_endpoint = " enabled"
@@ -130,7 +127,6 @@ module "ec2_t2_unlimited" {
130127 instance_type = " t2.micro"
131128 cpu_credits = " unlimited"
132129 subnet_id = element (module. vpc . private_subnets , 0 )
133- vpc_security_group_ids = [module . security_group . security_group_id ]
134130 associate_public_ip_address = true
135131
136132 maintenance_options = {
@@ -148,7 +144,6 @@ module "ec2_t3_unlimited" {
148144 instance_type = " t3.micro"
149145 cpu_credits = " unlimited"
150146 subnet_id = element (module. vpc . private_subnets , 0 )
151- vpc_security_group_ids = [module . security_group . security_group_id ]
152147 associate_public_ip_address = true
153148
154149 tags = local. tags
@@ -171,11 +166,10 @@ module "ec2_ignore_ami_changes" {
171166
172167 ignore_ami_changes = true
173168
174- ami = data. aws_ami . amazon_linux . id
175- instance_type = " t2.micro"
176- availability_zone = element (module. vpc . azs , 0 )
177- subnet_id = element (module. vpc . private_subnets , 0 )
178- vpc_security_group_ids = [module . security_group . security_group_id ]
169+ ami = data. aws_ami . amazon_linux . id
170+ instance_type = " t2.micro"
171+ availability_zone = element (module. vpc . azs , 0 )
172+ subnet_id = element (module. vpc . private_subnets , 0 )
179173
180174 tags = local. tags
181175}
@@ -190,8 +184,8 @@ locals {
190184 instance_type = " t3.micro"
191185 availability_zone = element (module. vpc . azs , 0 )
192186 subnet_id = element (module. vpc . private_subnets , 0 )
193- root_block_device = [
194- {
187+ root_block_device = {
188+ main = {
195189 encrypted = true
196190 volume_type = " gp3"
197191 throughput = 200
@@ -200,19 +194,19 @@ locals {
200194 Name = " my-root-block"
201195 }
202196 }
203- ]
197+ }
204198 }
205199 two = {
206200 instance_type = " t3.small"
207201 availability_zone = element (module. vpc . azs , 1 )
208202 subnet_id = element (module. vpc . private_subnets , 1 )
209- root_block_device = [
210- {
203+ root_block_device = {
204+ main = {
211205 encrypted = true
212206 volume_type = " gp2"
213207 volume_size = 50
214208 }
215- ]
209+ }
216210 }
217211 three = {
218212 instance_type = " t3.medium"
@@ -229,13 +223,12 @@ module "ec2_multiple" {
229223
230224 name = " ${ local . name } -multi-${ each . key } "
231225
232- instance_type = each. value . instance_type
233- availability_zone = each. value . availability_zone
234- subnet_id = each. value . subnet_id
235- vpc_security_group_ids = [module . security_group . security_group_id ]
226+ instance_type = each. value . instance_type
227+ availability_zone = each. value . availability_zone
228+ subnet_id = each. value . subnet_id
236229
237230 enable_volume_tags = false
238- root_block_device = lookup (each. value , " root_block_device" , [] )
231+ root_block_device = try (each. value . root_block_device , null )
239232
240233 tags = local. tags
241234}
@@ -256,10 +249,9 @@ module "ec2_spot_instance" {
256249 associate_public_ip_address = true
257250
258251 # Spot request specific attributes
259- spot_price = " 0.1"
260- spot_wait_for_fulfillment = true
261- spot_type = " persistent"
262- spot_instance_interruption_behavior = " terminate"
252+ spot_price = " 0.1"
253+ spot_wait_for_fulfillment = true
254+ spot_type = " persistent"
263255 # End spot request specific attributes
264256
265257 user_data_base64 = base64encode (local. user_data )
@@ -270,28 +262,27 @@ module "ec2_spot_instance" {
270262 }
271263
272264 enable_volume_tags = false
273- root_block_device = [
274- {
265+ root_block_device = {
266+ main = {
275267 encrypted = true
276268 volume_type = " gp3"
277269 throughput = 200
278270 volume_size = 50
279271 tags = {
280272 Name = " my-root-block"
281273 }
282- },
283- ]
274+ }
275+ }
284276
285- ebs_block_device = [
286- {
287- device_name = " /dev/sdf"
277+ ebs_volumes = {
278+ " /dev/sdf" = {
288279 volume_type = " gp3"
289280 volume_size = 5
290281 throughput = 200
291282 encrypted = true
292283 # kms_key_id = aws_kms_key.this.arn # you must grant the AWSServiceRoleForEC2Spot service-linked role access to any custom KMS keys
293284 }
294- ]
285+ }
295286
296287 tags = local. tags
297288}
@@ -305,10 +296,8 @@ module "ec2_open_capacity_reservation" {
305296
306297 name = " ${ local . name } -open-capacity-reservation"
307298
308- ami = data. aws_ami . amazon_linux . id
309299 instance_type = " t3.micro"
310300 subnet_id = element (module. vpc . private_subnets , 0 )
311- vpc_security_group_ids = [module . security_group . security_group_id ]
312301 associate_public_ip_address = false
313302
314303 capacity_reservation_specification = {
@@ -325,10 +314,8 @@ module "ec2_targeted_capacity_reservation" {
325314
326315 name = " ${ local . name } -targeted-capacity-reservation"
327316
328- ami = data. aws_ami . amazon_linux . id
329317 instance_type = " t3.micro"
330318 subnet_id = element (module. vpc . private_subnets , 0 )
331- vpc_security_group_ids = [module . security_group . security_group_id ]
332319 associate_public_ip_address = false
333320
334321 capacity_reservation_specification = {
@@ -365,11 +352,9 @@ module "ec2_cpu_options" {
365352
366353 name = " ${ local . name } -cpu-options"
367354
368- ami = data. aws_ami . amazon_linux_23 . id
369355 instance_type = " c6a.xlarge" # used to set core count below and test amd_sev_snp attribute
370356 availability_zone = element (module. vpc . azs , 0 )
371357 subnet_id = element (module. vpc . private_subnets , 0 )
372- vpc_security_group_ids = [module . security_group . security_group_id ]
373358 placement_group = aws_placement_group. web . id
374359 associate_public_ip_address = true
375360 disable_api_stop = false
@@ -389,22 +374,20 @@ module "ec2_cpu_options" {
389374 amd_sev_snp = " enabled"
390375 }
391376 enable_volume_tags = false
392- root_block_device = [
393- {
377+ root_block_device = {
378+ main = {
394379 encrypted = true
395380 volume_type = " gp3"
396381 throughput = 200
397382 volume_size = 50
398383 tags = {
399384 Name = " my-root-block"
400385 }
401- },
402- ]
386+ }
387+ }
403388
404- ebs_block_device = [
405- {
406- device_name = " /dev/sdf"
407- volume_type = " gp3"
389+ ebs_volumes = {
390+ " /dev/sdf" = {
408391 volume_size = 5
409392 throughput = 200
410393 encrypted = true
@@ -413,7 +396,7 @@ module "ec2_cpu_options" {
413396 MountPoint = " /mnt/data"
414397 }
415398 }
416- ]
399+ }
417400
418401 instance_tags = { Persistence = " 09:00-18:00" }
419402
@@ -426,7 +409,7 @@ module "ec2_cpu_options" {
426409
427410module "vpc" {
428411 source = " terraform-aws-modules/vpc/aws"
429- version = " ~> 5 .0"
412+ version = " ~> 6 .0"
430413
431414 name = local. name
432415 cidr = local. vpc_cidr
@@ -441,34 +424,19 @@ module "vpc" {
441424data "aws_ami" "amazon_linux" {
442425 most_recent = true
443426 owners = [" amazon" ]
444-
445- filter {
446- name = " name"
447- values = [" amzn-ami-hvm-*-x86_64-gp2" ]
448- }
449- }
450-
451- data "aws_ami" "amazon_linux_23" {
452- most_recent = true
453- owners = [" amazon" ]
454-
455- filter {
456- name = " name"
457- values = [" al2023-ami-2023*-x86_64" ]
458- }
427+ name_regex = " ^al2023-ami-2023.*-x86_64"
459428}
460429
461430module "security_group" {
462431 source = " terraform-aws-modules/security-group/aws"
463- version = " ~> 4 .0"
432+ version = " ~> 5 .0"
464433
465434 name = local. name
466435 description = " Security group for example usage with EC2 instance"
467436 vpc_id = module. vpc . vpc_id
468437
469438 ingress_cidr_blocks = [" 0.0.0.0/0" ]
470439 ingress_rules = [" http-80-tcp" , " all-icmp" ]
471- egress_rules = [" all-all" ]
472440
473441 tags = local. tags
474442}
0 commit comments