How to pass in UserData as a parameter in CloudFormation

Constantin

I have a CloudFormation template that creates an EC2 instance with an UserData script that should be assigned with the LaunchUserData parameter:

AWSTemplateFormatVersion: '2010-09-09'
Description: Create an AMI from an EC2 instance.
Parameters:
  LaunchUserData:
    Description: Base64-encoded user data to launch EC2 instances.
    Type: String
  GitSSHKey:
    Description: Git SSH key
      Type: String
Resources:
  LaunchEc2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      UserData: ...

LaunchUserData is a bash script that will reference some of the stack's parameters and resources:

#!/bin/bash


GIT_SSH_KEY=${GitSshKey};

echo "$GIT_SSH_KEY" | base64 -d > $HOME/.ssh/id_rsa;

echo ${AWS:Region};

I want to create the stack via CLI as follows:

GIT_SSH_KEY="somesshkey"
LAUNCH_USERDATA=$(jq -Rs . < $PWD/templates/launch)
STACK_ID=$(aws cloudformation create-stack \
--stack-name test \
--template-body file://$CLOUD_FORMATION_FILE \
--parameters \
    ParameterKey=GitSSHKey,ParameterValue="$GIT_SSH_KEY" \
    ParameterKey=LaunchUserData,ParameterValue="$LAUNCH_USERDATA" \
--output text \
--query 'StackId');

The LaunchUserData script, once interpreted, must reference and substitute its ${} variables with parameters and resources of the current stack, hence, when the user-data script is retrieved (and executed) within the EC2 instance, it should look like this:

$ curl http://169.254.169.254/latest/user-data

#!/bin/bash


GIT_SSH_KEY=somesshkey;

echo "$GIT_SSH_KEY" | base64 -d > $HOME/.ssh/id_rsa;
echo us-east-1;

Note how the script might also reference other CloudFormation resources.

How can I correctly pass LaunchUserData parameter to UserData without hard-coding it into the template?

Paolo

You could do something like this:

AWSTemplateFormatVersion: '2010-09-09'
Description: Create an AMI from an EC2 instance.
Parameters:
  LaunchUserData:
    Description: Base64-encoded user data to launch EC2 instances.
    Type: String
  GitSSHKey:
    Description: Git SSH key
      Type: String
Resources:
  LaunchEc2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          export GitSshKey="${GitSSHKey}"
          echo "${LaunchUserData}" | base64 -d | bash

so decode the parameter and pipe it to bash.

Do note however that http://169.254.169.254/latest/user-data will always return whatever is present in the template itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cloudformation: How to pass a string through cloudformation script into UserData?

CloudFormation: How to handle errors in UserData

Reference Parameter Value in UserData in AWS Cloudformation

How to import Resource ID into UserData in Cloudformation

How to wrtie the following command in userdata cloudformation?

How to pass a list to a nested stack parameter in AWS CloudFormation?

How to pass public key from parameter file in cloudformation template?

How do I pass a list of strings as a parameter in CloudFormation?

Crontab in AWS CloudFormation Userdata

Using userdata in Cloudformation

aws cloudformation userdata: how to use local variable in script

How to run ec2 UserData script on cloudformation update

How to add two variables in Cloudformation Fn::Sub in UserData

Can you pass a parameter into a bash script in Cloudformation?

Pass secure SSM parameter to a nested CloudFormation stack

How to pass list to aws CDK bootstrap `--cloudformation-execution-policies` parameter in powershell

How to pass a parameter from CloudFormation to an AWS Lambda function stored in an S3 bucket

How to pass username as parameter recursively from a file in SecretStringTemplate (rotation secret cloudformation template code)?

AWS Cloudformation: Add Paramater for Userdata

using Cloudformation ref with awscli in userData

UserData script with Resource Attribute CloudFormation

Reference ID of resource in Userdata Cloudformation

Ios how to pass google map userData to next view

How to pass userdata from one Lua chunk to another in C++

AWS: How to specify a boolean parameter in a CloudFormation template

How to pass in JSON type into yaml cloudformation template

aws: cloudformation: How to pass the role arn to instanceprofile

how to pass reference to existing VPC to a cloudformation template?

How To Pass CloudFormation Outputs To A CodeBuild Stage?