- Newest
- Most votes
- Most comments
Hello Confidential,
To reference the SSM Parameter value with the stack name in your AWS CloudFormation template, you can use the Fn::Sub
function to create the association document dynamically. Here's how you can achieve this:
Resources: CustomParameter: Type: 'AWS::SSM::Parameter' Properties: Name: Fn::Sub: '/${AWS::StackName}/name' Type: String Value: !Ref Name RunCommandAssociation1: Type: 'AWS::SSM::Association' Properties: Name: 'AWS-RunShellScript' Targets: - Key: InstanceIds Values: - !Ref Instance1 Parameters: commands: - Fn::Sub: - availability_zone=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone) region=${availability_zone::-1} name=$(aws ssm get-parameter --name /${StackName}/name --query 'Parameter.Value' --output text --region "${region}") - StackName: !Ref "AWS::StackName"
In this updated template:
-
We use
Fn::Sub
to create a string where${StackName}
is substituted with the value ofAWS::StackName
. This dynamically generates the/stack-name/name
parameter name. -
Inside the
commands
, you can access this dynamic parameter name and fetch the value accordingly.
This way, you can reference the SSM Parameter value that includes the stack name in your AWS::SSM::Association without hardcoding the stack name.
Please give a thumbs up if my suggestion helps
Hi,
On the proposal, you should replace !Ref "AWS::StackName"
with ${AWS::StackName"}
See section "Fn::Sub without a mapping" of https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html for more details
Best,
Didier
hi, If I want to use an Input parameter like the Stack name in my command then, How shall I do it? Thanks in advance!
Relevant content
- asked 2 years ago
- asked 8 months ago
- asked a year ago
- asked 10 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
Hi Gabriel, I was trying your suggestion but getting the following error:- Template contains errors.: Template error: variable names in Fn::Sub syntax must contain only alphanumeric characters, underscores, periods, and colons. on the following line "region=${availability_zone::-1}" and following error: Template format error: Unresolved resource dependencies [StackName] in the Resources block of the template on line name=$(aws ssm get-parameter --name /${StackName}/name --query 'Parameter.Value' --output text --region "${region}").
This is what i am doing:-
RunCommandAssociation1: DependsOn: - Instance1 Type: 'AWS::SSM::Association' Properties: Name: 'AWS-RunShellScript' Targets: - Key: InstanceIds Values: - !Ref Instance1 Parameters: commands: - Fn::Sub: sleep 20 availability_zone=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone) aws ssm get-parameter --name /${StackName}/es-parameter --query 'Parameter.Value' --output text --region "$region" >> /home/ec2-user/ip.txt name=$(aws ssm get-parameter --name /${StackName}/es-name --query 'Parameter.Value' --output text --region "$region")