We’ve got some Windows EC2 Instances deployed using Cloudformation that are configured using cfn-init scripts.
In order to update components on the instances later on, we need to re-run some of the Cloudformation::Init configsets using the cfn-init helper scripts and one of the required parameters, –stack, is the stack name. I was expecting this value would be dynamically available via the EC2 Instance Metadata as a single call but I have not been able to find it.
I am posting a workaround script but I’m still wondering if there is a simpler, robust way that I am missing.
2
Answers
Since the initial cfn-init command included in the User Data script includes the ${AWS::StackName} Psuedo parameter we can parse this script to extract it. The following PowerShell script illustrates the technique and a similar bash script could be used on Linux instances:
Note I considered using the aws cloudformation describe-stack-resources call providing the ec2 instance-id from metadata as the
--physical-resource-id
parmeter. This does work but is an extra dependency on an external service acall and would require the IAM Instance Profile permissions to be extended.You probably want to define cfn-hup in your cloudformation template so that it lays down the files with variables you need:
from docs
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-hup.html