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
754 stars 1.87k forks source link

Fails to create EC2 when ami variable is defined #352

Closed Zonny closed 1 year ago

Zonny commented 1 year ago

Hello,

I am recently trying to use this module and ran into this issue

 Error: Missing required argument

   with module.mattermost_standalone_ecs_instance.aws_instance.this[0],
   on .terraform/modules/mattermost_standalone_ecs_instance/main.tf line 22, in resource "aws_instance" "this":
   22:   ami                  = try(coalesce(var.ami, nonsensitive(data.aws_ssm_parameter.this[0].value)), null)

 "ami": one of `ami,launch_template` must be specified

It seems like it might be due to merging this PR. The reason I believe is that the ami variable is always getting set to null because data.aws_ssm_parameter.this[0].value cannot be referenced. I removed the try block locally and get this error message

 Error: Invalid index

   on .terraform/modules/mattermost_standalone_ecs_instance/main.tf line 22, in resource "aws_instance" "this":
   22:   ami                  = coalesce(var.ami, nonsensitive(data.aws_ssm_parameter.this[0].value))
     ├────────────────
     │ data.aws_ssm_parameter.this is empty tuple

 The given key does not identify an element in this collection value: the collection has no elements.

I opened up a PR to address the issue. Let me know if it is acceptable or if changes will need to be required

Originally posted by @Zonny in https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/issues/351#issuecomment-1681153268

JoaoJanini commented 1 year ago

Same problem here. Had to pass the ami using ami_ssm_parameter instead of ami as a workaround.

antonbabenko commented 1 year ago

This issue has been resolved in version 5.3.1 :tada:

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.