How to refer SSMParameter value in AWS::SSM::Association?

0

Hi there, I am stuck with the following problem:- I am creating an SSMParameter like this:-

CustomParameter:
    Type: 'AWS::SSM::Parameter'
    Properties:
      Name:
        Fn::Sub: '/${AWS::StackName}/name'
      Type: String
      Value: !Ref Name

Now, I want to get the value of this parameter in my AWS::SSM::Association resource:-

RunCommandAssociation1:
    Type: 'AWS::SSM::Association'
    Properties:
      Name: 'AWS-RunShellScript'
      Targets:
        - Key: InstanceIds
          Values:
            - !Ref Instance1
      Parameters:
        commands:
          - 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 /${AWS::StackName}/name --query 'Parameter.Value' --output text --region "$region")

I believe we cannot directly reference ${AWS::StackName} in above commands. Can anyone help me how can I get this work done. I don't want to use ec2 userdata here. I only need the solution using 'AWS::SSM::Association'.

Thanks in advance!

gefragt vor 9 Monaten248 Aufrufe
2 Antworten
0

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:

  1. We use Fn::Sub to create a string where ${StackName} is substituted with the value of AWS::StackName. This dynamically generates the /stack-name/name parameter name.

  2. 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

profile picture
beantwortet vor 9 Monaten
profile pictureAWS
EXPERTE
überprüft vor 9 Monaten
profile pictureAWS
EXPERTE
überprüft vor 9 Monaten
  • 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")

0

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

profile pictureAWS
EXPERTE
beantwortet vor 9 Monaten
  • 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!

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen