terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
758 stars 1.88k forks source link

Missing output for Block Devices when ami change is ignored #342

Closed sim1e097cd closed 1 year ago

sim1e097cd commented 1 year ago

Description

If ignore_ami_changes is set to true the resource inside the module changes its name from module.ec2_instance.aws_instance.this[0] to module.ec2_instance.aws_instance.ignore_ami[0]. This breaks the output generation that does not consider a different name for the resource.

The solution consists in adding a multiple choice in the try(...)

Versions

Reproduction Code [Required]

output "root_block_device" {
  description = "Root block device information"
  value       = try(aws_instance.ignore_ami[0].root_block_device, null)
}

output "ebs_block_device" {
  description = "EBS block device information"
  value       = try(aws_instance.this[0].ebs_block_device, null)
}

output "ephemeral_block_device" {
  description = "Ephemeral block device information"
  value       = try(aws_instance.this[0].ephemeral_block_device, null)
}

Steps to reproduce the behavior:

Execute a plan setting ignore_ami_changes to true with this local

root_volumes = sum([for i in module.ec2_instance.root_block_device : try(i.volume_size, 0)])

Expected behavior

A valid value for the output

Actual behavior

A non existing value for the output

Terminal Output Screenshot(s)

https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/assets/72647533/0c519276-5ee3-4f49-b295-ad4e3a5cd810

Possible solution

output "root_block_device" {
  description = "Root block device information"
  value = try(
    aws_instance.this[0].root_block_device,
    aws_instance.ignore_ami[0].root_block_device,
    aws_spot_instance_request.this[0].root_block_device,
    null
  )
}

output "ebs_block_device" {
  description = "EBS block device information"
  value = try(
    aws_instance.this[0].ebs_block_device,
    aws_instance.ignore_ami[0].ebs_block_device,
    aws_spot_instance_request.this[0].ebs_block_device,
    null
  )
}

output "ephemeral_block_device" {
  description = "Ephemeral block device information"
  value = try(
    aws_instance.this[0].ephemeral_block_device,
    aws_instance.ignore_ami[0].ephemeral_block_device,
    aws_spot_instance_request.this[0].ephemeral_block_device,
    null
  )
}
github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.