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

feat: Remove call data ssm parameter when ami id is specified #351

Closed shawnsavour closed 1 year ago

shawnsavour commented 1 year ago

350 Description:

Description

Create an ec2 instance without ssm permission with cause error.

This account or role can still create an instance in console.

The root cause is because this line of code in main.tf in line 9:

data "aws_ssm_parameter" "this" {
  count = local.create ? 1 : 0

  name = var.ami_ssm_parameter
}

Request to make the call data aws_ssm_parameter optional, something like when ami is specified, then no need to call ssm parameter to get the ami id:

#main.tf
  ami                  = try(coalesce(var.ami, nonsensitive(data.aws_ssm_parameter.this[0].value)), null)

Because it coalesce the var.ami and the nonsensitive(data.aws_ssm_parameter.this[0].value)), so we only need it when var.ami is not specified. So it should be check for null when use data to call to ssm.

data "aws_ssm_parameter" "this" {
  count = local.create && var.ami == null ? 1 : 0

  name = var.ami_ssm_parameter
}

⚠️ Note

Versions

Reproduction Code [Required]

Steps to reproduce the behavior:

Expected behavior

Create an instance without ssm parameter permission

Actual behavior

Cannot create instance because missing permission

antonbabenko commented 1 year ago

This PR is included in version 5.3.0 :tada:

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.
github-actions[bot] commented 1 year ago

I'm going to lock this pull request 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 related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.