Query related to Cloudformation with SSM

0

Hi there, I am trying to reference the following parameters in Cloudformation template under AWS::SSM::Association when trying to run commands.

  • aws ssm get-parameter --name /my-stack/my-parameter --query 'Parameter.Value' --output text --region !Ref MyRegion In above command I am not able to get the value of the region with ** !Ref MyRegion** which am passing as input parameter.
  • echo ${Instance1.PrivateIp} >> /home/ec2-user/ip.txt. Here also, I am not able to get the PrivateIp of the Instance1 Ec2.

This is what i am doing:-

RunCommandAssociation2:
    Type: 'AWS::SSM::Association'
    Properties:
      Name: 'AWS-RunShellScript'
      Targets:
        - Key: InstanceIds
          Values:
            - !Ref Instance2
      Parameters:
        commands:
          - echo $(curl http://169.254.169.254/latest/meta-data/local-ipv4) >> /home/ec2-user/ip.txt
          - value=$(aws ssm get-parameter --name /my-stack/my-parameter --query 'Parameter.Value' --output text --region '!Ref MyRegion')
          - echo "$value" >> /home/ec2-user/ip.txt
          - echo ${Instance1.PrivateIp} >> /home/ec2-user/ip.txt

Can anyone please help me here. I want to pass these values in my SSM commands, just like we pass in user data. Thanks in advance!

  • Are quotes in !Ref MyRegiin necessary?

  • hi @Antonio_Lagrotteria, I tried without quotes as well but no luck.

asked 8 months ago248 views
1 Answer
0

Hi,

As said in my answer to your initiai question, you must use Fn:Sub if you want to have ${Instance1.PrivateIp} properly substituted with its real value.

See examples on https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html

The following is closest to your use case is this one if you replace UserData with your SSM doc

UserData:
  Fn::Base64:
    !Sub |
      #!/bin/bash -xe
      yum update -y aws-cfn-bootstrap
      /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --configsets wordpress_install --region ${AWS::Region}
      /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}

If you don't use !Sub, you will remain with your variable name

Best,

Didier

profile pictureAWS
EXPERT
answered 8 months ago
profile pictureAWS
EXPERT
reviewed 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions