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!

1개 답변
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
전문가
답변함 9달 전
profile pictureAWS
전문가
검토됨 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠