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