Quantcast
Channel: Active questions tagged amazon-ec2 - Stack Overflow
Viewing all articles
Browse latest Browse all 29541

Setting SSM parameter as an Environment variable for EC2 - Does not work

$
0
0

I am trying to get and export an SSM parameter as an environment variable to an EC2 using the UserData section of Cloudformation.

The script is trying to append for e.g export WHATS_HER_NAME=Sherlyn to the /etc/profile file. But all i see in the /etc/profile is export WHATS_HER_NAME=. The value is not present. I use amazon linux 2 ami.

here is my cloudformation template.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Ec2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "IamInstanceProfile": {
          "Ref": "Ec2instanceProfileTest"
        },
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "\n",
              [
                "#!/bin/bash -xe",
                "yum update -y aws-cfn-bootstrap",
                {
                  "Fn::Sub": "/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource Ec2Instance --configsets default --region ${AWS::Region}"
                },
                {
                  "Fn::Sub": "/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}"
                },
                {
                  "Fn::Sub": "echo \"export WHATS_HER_NAME=$(aws ssm get-parameter --name WhatsHerName --region ${AWS::Region} --query 'Parameter.Value')\">> /etc/profile"
                }
              ]
            ]
          }
        }
      }
    },
    "GetSSMParameterPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "GetSsmProperty",
        "PolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Resource": "arn:aws:ssm:ap-southeast-2:012345678901:parameter/WhatsHerName",
              "Action": [
                "ssm:GetParameters",
                "ssm:GetParameter"
              ]
            },
            {
              "Effect": "Allow",
              "Resource": "*",
              "Action": [
                "ssm:DescribeParameters"
              ]
            }
          ]
        },
        "Roles": [
          {
            "Ref": "InstanceRole"
          }
        ]
      }
    },
    "InstanceRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/"
      }
    },
    "BasicParameter": {
      "Type": "AWS::SSM::Parameter",
      "Properties": {
        "Name": "WhatsHerName",
        "Type": "String",
        "Value": "Sherlyn"
      }
    }
  }
}

any help would be highly appreciated.


Viewing all articles
Browse latest Browse all 29541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>