11data "aws_partition" "current" {}
22
33locals {
4- create = var. create
4+ create = var. create && var . putin_khuylo
55
6- is_t_instance_type = can ( regex ( " t[0-9]+ \\ .[a-z0-9]+ " , var . instance_type ))
6+ is_t_instance_type = replace (var . instance_type , " /^t(2|3|3a|4g){1} \\ ..*$/ " , " 1 " ) == " 1 " ? true : false
77
8- ami = try (coalesce (data . aws_ami . ubuntu_amd64 . image_id , try (nonsensitive (data. aws_ssm_parameter . this [0 ]. value ), null )), null )
8+ ami = try (coalesce (var . ami , try (nonsensitive (data. aws_ssm_parameter . this [0 ]. value ), null )), null )
99}
1010
1111data "aws_ssm_parameter" "this" {
@@ -14,15 +14,18 @@ data "aws_ssm_parameter" "this" {
1414 name = var. ami_ssm_parameter
1515}
1616
17- # #####################################
17+ # ###############################################################################
1818# Instance
19- # #####################################
19+ # ###############################################################################
2020
2121resource "aws_instance" "this" {
22- count = local. create && var. ignore_ami_changes && ! var. create_spot_instance ? 1 : 0
22+ count = local. create && ! var. ignore_ami_changes && ! var. create_spot_instance ? 1 : 0
2323
2424 ami = local. ami
2525 instance_type = var. instance_type
26+ cpu_core_count = var. cpu_core_count
27+ cpu_threads_per_core = var. cpu_threads_per_core
28+ hibernation = var. hibernation
2629
2730 user_data = var. user_data
2831 user_data_base64 = var. user_data_base64
@@ -187,24 +190,204 @@ resource "aws_instance" "this" {
187190
188191 tags = merge ({ " Name" = var.name }, var. instance_tags , var. tags )
189192 volume_tags = var. enable_volume_tags ? merge ({ " Name" = var.name }, var. volume_tags ) : null
193+ }
190194
191- lifecycle {
195+ # ###############################################################################
196+ # Instance - Ignore AMI Changes
197+ # ###############################################################################
198+
199+ resource "aws_instance" "ignore_ami" {
200+ count = local. create && var. ignore_ami_changes && ! var. create_spot_instance ? 1 : 0
201+
202+ ami = local. ami
203+ instance_type = var. instance_type
204+ cpu_core_count = var. cpu_core_count
205+ cpu_threads_per_core = var. cpu_threads_per_core
206+ hibernation = var. hibernation
192207
193- ignore_changes = [ami , ipv6_address_count ]
208+ user_data = var. user_data
209+ user_data_base64 = var. user_data_base64
210+ user_data_replace_on_change = var. user_data_replace_on_change
194211
212+ availability_zone = var. availability_zone
213+ subnet_id = var. subnet_id
214+ vpc_security_group_ids = var. vpc_security_group_ids
215+
216+ key_name = var. key_name
217+ monitoring = var. monitoring
218+ get_password_data = var. get_password_data
219+ iam_instance_profile = var. create_iam_instance_profile ? aws_iam_instance_profile. this [0 ]. name : var. iam_instance_profile
220+
221+ associate_public_ip_address = var. associate_public_ip_address
222+ private_ip = var. private_ip
223+ secondary_private_ips = var. secondary_private_ips
224+ ipv6_address_count = var. ipv6_address_count
225+ ipv6_addresses = var. ipv6_addresses
226+
227+ ebs_optimized = var. ebs_optimized
228+
229+ dynamic "cpu_options" {
230+ for_each = length (var. cpu_options ) > 0 ? [var . cpu_options ] : []
231+
232+ content {
233+ core_count = try (cpu_options. value . core_count , null )
234+ threads_per_core = try (cpu_options. value . threads_per_core , null )
235+ amd_sev_snp = try (cpu_options. value . amd_sev_snp , null )
236+ }
195237 }
196- }
197238
239+ dynamic "capacity_reservation_specification" {
240+ for_each = length (var. capacity_reservation_specification ) > 0 ? [var . capacity_reservation_specification ] : []
241+
242+ content {
243+ capacity_reservation_preference = try (capacity_reservation_specification. value . capacity_reservation_preference , null )
244+
245+ dynamic "capacity_reservation_target" {
246+ for_each = try ([capacity_reservation_specification . value . capacity_reservation_target ], [])
247+
248+ content {
249+ capacity_reservation_id = try (capacity_reservation_target. value . capacity_reservation_id , null )
250+ capacity_reservation_resource_group_arn = try (capacity_reservation_target. value . capacity_reservation_resource_group_arn , null )
251+ }
252+ }
253+ }
254+ }
255+
256+ dynamic "root_block_device" {
257+ for_each = var. root_block_device
258+
259+ content {
260+ delete_on_termination = try (root_block_device. value . delete_on_termination , null )
261+ encrypted = try (root_block_device. value . encrypted , null )
262+ iops = try (root_block_device. value . iops , null )
263+ kms_key_id = lookup (root_block_device. value , " kms_key_id" , null )
264+ volume_size = try (root_block_device. value . volume_size , null )
265+ volume_type = try (root_block_device. value . volume_type , null )
266+ throughput = try (root_block_device. value . throughput , null )
267+ tags = try (root_block_device. value . tags , null )
268+ }
269+ }
270+
271+ dynamic "ebs_block_device" {
272+ for_each = var. ebs_block_device
273+
274+ content {
275+ delete_on_termination = try (ebs_block_device. value . delete_on_termination , null )
276+ device_name = ebs_block_device. value . device_name
277+ encrypted = try (ebs_block_device. value . encrypted , null )
278+ iops = try (ebs_block_device. value . iops , null )
279+ kms_key_id = lookup (ebs_block_device. value , " kms_key_id" , null )
280+ snapshot_id = lookup (ebs_block_device. value , " snapshot_id" , null )
281+ volume_size = try (ebs_block_device. value . volume_size , null )
282+ volume_type = try (ebs_block_device. value . volume_type , null )
283+ throughput = try (ebs_block_device. value . throughput , null )
284+ tags = try (ebs_block_device. value . tags , null )
285+ }
286+ }
287+
288+ dynamic "ephemeral_block_device" {
289+ for_each = var. ephemeral_block_device
290+
291+ content {
292+ device_name = ephemeral_block_device. value . device_name
293+ no_device = try (ephemeral_block_device. value . no_device , null )
294+ virtual_name = try (ephemeral_block_device. value . virtual_name , null )
295+ }
296+ }
297+
298+ dynamic "metadata_options" {
299+ for_each = length (var. metadata_options ) > 0 ? [var . metadata_options ] : []
300+
301+ content {
302+ http_endpoint = try (metadata_options. value . http_endpoint , " enabled" )
303+ http_tokens = try (metadata_options. value . http_tokens , " optional" )
304+ http_put_response_hop_limit = try (metadata_options. value . http_put_response_hop_limit , 1 )
305+ instance_metadata_tags = try (metadata_options. value . instance_metadata_tags , null )
306+ }
307+ }
308+
309+ dynamic "network_interface" {
310+ for_each = var. network_interface
311+
312+ content {
313+ device_index = network_interface. value . device_index
314+ network_interface_id = lookup (network_interface. value , " network_interface_id" , null )
315+ delete_on_termination = try (network_interface. value . delete_on_termination , false )
316+ }
317+ }
318+
319+ dynamic "private_dns_name_options" {
320+ for_each = length (var. private_dns_name_options ) > 0 ? [var . private_dns_name_options ] : []
321+
322+ content {
323+ hostname_type = try (private_dns_name_options. value . hostname_type , null )
324+ enable_resource_name_dns_a_record = try (private_dns_name_options. value . enable_resource_name_dns_a_record , null )
325+ enable_resource_name_dns_aaaa_record = try (private_dns_name_options. value . enable_resource_name_dns_aaaa_record , null )
326+ }
327+ }
328+
329+ dynamic "launch_template" {
330+ for_each = length (var. launch_template ) > 0 ? [var . launch_template ] : []
331+
332+ content {
333+ id = lookup (var. launch_template , " id" , null )
334+ name = lookup (var. launch_template , " name" , null )
335+ version = lookup (var. launch_template , " version" , null )
336+ }
337+ }
338+
339+ dynamic "maintenance_options" {
340+ for_each = length (var. maintenance_options ) > 0 ? [var . maintenance_options ] : []
341+
342+ content {
343+ auto_recovery = try (maintenance_options. value . auto_recovery , null )
344+ }
345+ }
198346
199- # #####################################
347+ enclave_options {
348+ enabled = var. enclave_options_enabled
349+ }
350+
351+ source_dest_check = length (var. network_interface ) > 0 ? null : var. source_dest_check
352+ disable_api_termination = var. disable_api_termination
353+ disable_api_stop = var. disable_api_stop
354+ instance_initiated_shutdown_behavior = var. instance_initiated_shutdown_behavior
355+ placement_group = var. placement_group
356+ tenancy = var. tenancy
357+ host_id = var. host_id
358+
359+ credit_specification {
360+ cpu_credits = local. is_t_instance_type ? var. cpu_credits : null
361+ }
362+
363+ timeouts {
364+ create = try (var. timeouts . create , null )
365+ update = try (var. timeouts . update , null )
366+ delete = try (var. timeouts . delete , null )
367+ }
368+
369+ tags = merge ({ " Name" = var.name }, var. instance_tags , var. tags )
370+ volume_tags = var. enable_volume_tags ? merge ({ " Name" = var.name }, var. volume_tags ) : null
371+
372+ lifecycle {
373+ ignore_changes = [
374+ ami
375+ ]
376+ }
377+ }
378+
379+ # ###############################################################################
200380# Spot Instance
201- # #####################################
381+ # ###############################################################################
202382
203383resource "aws_spot_instance_request" "this" {
204384 count = local. create && var. create_spot_instance ? 1 : 0
205385
206- ami = data . aws_ami . ubuntu_amd64 . image_id
386+ ami = local . ami
207387 instance_type = var. instance_type
388+ cpu_core_count = var. cpu_core_count
389+ cpu_threads_per_core = var. cpu_threads_per_core
390+ hibernation = var. hibernation
208391
209392 user_data = var. user_data
210393 user_data_base64 = var. user_data_base64
@@ -361,9 +544,9 @@ resource "aws_spot_instance_request" "this" {
361544 volume_tags = var. enable_volume_tags ? merge ({ " Name" = var.name }, var. volume_tags ) : null
362545}
363546
364- # #####################################
547+ # ###############################################################################
365548# IAM Role / Instance Profile
366- # #####################################
549+ # ###############################################################################
367550
368551locals {
369552 iam_role_name = try (coalesce (var. iam_role_name , var. name ), " " )
@@ -420,3 +603,20 @@ resource "aws_iam_instance_profile" "this" {
420603 create_before_destroy = true
421604 }
422605}
606+
607+ # ###############################################################################
608+ # Elastic IP
609+ # ###############################################################################
610+
611+ resource "aws_eip" "this" {
612+ count = local. create && var. create_eip && ! var. create_spot_instance ? 1 : 0
613+
614+ instance = try (
615+ aws_instance. this [0 ]. id ,
616+ aws_instance. ignore_ami [0 ]. id ,
617+ )
618+
619+ domain = var. eip_domain
620+
621+ tags = merge (var. tags , var. eip_tags )
622+ }
0 commit comments