gruntwork-io/bash-commons

If statement in IMDSv2 check needs to check for undeclared variable

Closed this issue · 1 comments

Describe the bug
The latest PR #60 to check for IMDSv2 has an if statement that uses -z which causes the script to fail with an unbound variable error.

With -z this happens if the variable is undeclared (because the scripts that call it usually have set -eu in them). This needs to be changed to -n to check for either undeclared or undefined.

Undeclared means the variable was never declared (which is the default if no one set the variable). Undefined means the variable was declared at some point, but it currently empty (i.e. GRUNTWORK_BASH_COMMONS_IMDS_VERSION="").

Terminal output:

2024-06-27T01:37:44Z: ==> amazon-ebs.centos: /opt/gruntwork/bash-commons/aws.sh: line 28: GRUNTWORK_BASH_COMMONS_IMDS_VERSION: unbound variable   

To Reproduce
Try to build an AMI using packer with the terraform-aws-monitoring modules to install cloudwatch agent.

Expected behavior
The if statement triggers if variable is undeclared or undefined, and it does not error if it is undeclared.

Nice to have

  • Terminal output
  • Screenshots

See PR #64 for the recommended fix.