0

I am trying to get ENVIRONMENT Variables into the EC2 instance (trying to run a django app on Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type ami-0ff8a91507f77f867 ). How do you get them in the newest version of amazon's linux, or get the logging so it can be traced.

user-data text (modified from here):

#!/bin/bash
#trying to get a file made
touch /tmp/testfile.txt
cat 'This and that' > /tmp/testfile.txt
#trying to log
echo 'Woot!' > /home/ec2-user/user-script-output.txt

#Trying to get the output logged to see what is going wrong
exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1

#trying to log
echo "XXXXXXXXXX  STARTING USER DATA SCRIPT    XXXXXXXXXXXXXX"

#trying to store the ENVIRONMENT VARIABLES
PARAMETER_PATH='/'
REGION='us-east-1'
# Functions
AWS="/usr/local/bin/aws"

get_parameter_store_tags() {
    echo $($AWS ssm get-parameters-by-path --with-decryption --path ${PARAMETER_PATH}  --region ${REGION})
}

params_to_env () {
    params=$1

    # If .Ta1gs does not exist we assume ssm Parameteres object.
    SELECTOR="Name"

    for key in $(echo $params | /usr/bin/jq -r ".[][].${SELECTOR}"); do
                value=$(echo $params | /usr/bin/jq -r ".[][] | select(.${SELECTOR}==\"$key\") | .Value")
                key=$(echo "${key##*/}" | /usr/bin/tr ':' '_' | /usr/bin/tr '-' '_' | /usr/bin/tr '[:lower:]' '[:upper:]')
                export $key="$value"
                echo "$key=$value"
    done
}


# Get TAGS
if [ -z "$PARAMETER_PATH" ]
      then
              echo "Please provide a parameter store path. -p option"
              exit 1
fi
TAGS=$(get_parameter_store_tags ${PARAMETER_PATH} ${REGION})
echo "Tags fetched via ssm from ${PARAMETER_PATH} ${REGION}"

echo "Adding new variables..."
params_to_env "$TAGS"

Notes -

  • What i think i know but am unsure
    • the user-data script is only loaded when it is created, not when I stop and then start mentioned here (although it also says [i think outdated] that the output is logged to /var/log/cloud-init-output.log )
    • I may not be starting the instance correctly
    • I don't know where to store the bash script so that it can be executed
  • What I have verified
    • the user-data text is on the instance by ssh-ing in and curl http://169.254.169.254/latest/user-data shows the current text (#!/bin/bash …)
  • What Ive tried
    • editing rc.local directly to export AWS_ACCESS_KEY_ID='JEFEJEFEJEFEJEFE' … and the like
    • putting them in the AWS Parameter Store (and can see them via the correct call, I just can't trace getting them into the EC2 instance without logs or confirming if the user-data is getting run)
    • putting ENV variables in Tags and importing them as mentioned here:
    • tried outputting the logs to other files as suggested here (Not seeing any log files in the ssh instance or on the system log)
    • viewing the System Log on the aws webpage to see any errors/logs via selecting the instance -> 'Actions' -> 'Instance Settings' -> 'Get System Log' (not seeing any commands run or log statements [only 1 unrelated word of user])
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • 1
    What is the current state - what is (or is not) actually happening? What is the target state? – getglad Sep 10 '18 at 18:02
  • I don't have any logs showing. It appears the user-data script isn't being executed. the instance starts. `printenv` prints other environment variables, but none of the ones I tried to add. – chris Frisina Sep 10 '18 at 18:20
  • Do you stop/start the instance or run it for the first time? userdata won't be run during next reboots – Putnik Sep 10 '18 at 18:56
  • 1
    See https://superuser.com/questions/1095113/how-to-configure-aws-ec2-user-data-values-and-bash-script – jarmod Sep 10 '18 at 19:27

0 Answers0