0

I've noticed that scripts run with CloudFormation's UserData attribute don't recognize the EC2 instance's shell variables. For example, the template section below doesn't print any values when provisioning. Is there any way to get around this?

UserData:
  Fn::Base64: !Sub |
    #!/bin/bash
    echo HOME: $HOME
    echo USER: $USER
    echo PATH: $PATH
1
  • Can you look at your instances's system log (from the AWS console: select you EC2 instance -> Actions -> Instance Settings -> Get System Log) to see if there's any errors? Commented Feb 6, 2017 at 23:19

1 Answer 1

2

Note that the environment where cloud-init's User-Data Script gets executed doesn't usually contain HOME and USER variables, since the script is executed as root in a non-login shell.

Try the env command in your UserData to see the full list of environment variables available:

Description: Output shell variables.
Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-9be6f38c # amzn-ami-hvm-2016.09.1.20161221-x86_64-gp2
      InstanceType: m3.medium
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          env

On an Amazon Linux AMI (note that the result will depend on the AMI you're running!), I get the following output in the Console Output:

TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
RUNLEVEL=3
runlevel=3
PWD=/
LANGSH_SOURCED=1
LANG=en_US.UTF-8
PREVLEVEL=N
previous=N
CONSOLETYPE=serial
SHLVL=4
UPSTART_INSTANCE=
UPSTART_EVENTS=runlevel
UPSTART_JOB=rc
_=/bin/env
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.