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!

posta 9 mesi fa248 visualizzazioni
2 Risposte
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
con risposta 9 mesi fa
profile pictureAWS
ESPERTO
verificato 9 mesi fa
profile pictureAWS
ESPERTO
verificato 9 mesi fa
  • 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
ESPERTO
con risposta 9 mesi fa
  • 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!

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande