You need to account for special characters, like ', *, \, etc. This works for me, but I'm not sure it is complete to cover every case.
echo -n '"' ; sed ':again; N; $!b again; s/\\/\\\\/g; s/"/\\"/g; s/\n/\\n/g; s/$/\\n/;' $1 | tr -d '\n' ; echo '"'
This will produce one, long string that can be placed into the "UserData": { "Fn::Base64": field. Here's an example shell script:
#!/bin/bash
echo $(date "+%F %R:%S") ":: get metadata"
MD=/etc/profile.d/metadata.sh
echo "# AWS metadata" > $MD
metadata=$(curl -sf http://169.254.169.254/latest/dynamic/instance-identity/document)
echo declare -x metadata=\'$metadata\' >> $MD
echo $(date "+%F %R:%S") ":: yum update"
yum -y update
echo $(date "+%F %R:%S") ":: awscli update"
yum -y install python-pip
rm -rf /tmp/pip-build-root/ ; pip install awscli --upgrade
echo $(date "+%F %R:%S") ":: set a cronjob"
echo '*/15 * * * * ~/bin/cronjob.sh' > /var/spool/cron/root
echo $(date "+%F %R:%S") ":: userdata complete"
Converted to CloudFormation:
"#!/bin/bash\n\necho $(date \"+%F %R:%S\") \":: get metadata\"\nMD=/etc/profile.d/metadata.sh\necho \"# AWS metadata\" > $MD\nmetadata=$(curl -sf http://169.254.169.254/latest/dynamic/instance-identity/document)\necho declare -x metadata=\\'$metadata\\' >> $MD\n\necho $(date \"+%F %R:%S\") \":: yum update\"\nyum -y update\n\necho $(date \"+%F %R:%S\") \":: awscli update\"\nyum -y install python-pip\nrm -rf /tmp/pip-build-root/ ; pip install awscli --upgrade\n\necho $(date \"+%F %R:%S\") \":: set a cronjob\"\necho '*/15 * * * * ~/bin/cronjob.sh' > /var/spool/cron/root\n\necho $(date \"+%F %R:%S\") \":: userdata complete\"\n"