I am building AWS infrastructure with Terraform and need to pass a shell script that has variables into the user data of a launch template. This is how I am referencing the script which is in a variable:
**user_data_base64 = filebase64(var.aws_userdata)**
This is one of the errors I am getting:
**Error: Variables not allowed
│
│ on variables.tf line 345, in variable "s1_aws_userdata":
│ 345: source /tmp/${CURL_FILEPATH}
│
│ Variables may not be used here.**
Is it possible in Terraform to accomplish this? Here is the variable with the script:
variable "s1_aws_userdata" {
type = "string"
default = <<EOF
#!/usr/bin/env bash
function download_and_run_scripts {
echo
# dependency check
if [ ! -x /usr/bin/curl ]; then
echo -e "Error, curl is not installed."
exit 1
fi
# s1g_install scripts
local DOWNLOAD_SCRIPTS="toolkit.sh ${AMI_SCRIPTS}"
local CURL_FILEPATH=""
for CURL_FILEPATH in ${DOWNLOAD_SCRIPTS}; do
echo -e "\nGoing to download: ${CURL_FILEPATH}\n"
case ${CURL_FILEPATH} in
toolkit.sh)
local CURL_REPO="s1_tools"
;;
*)
local CURL_REPO="s1g_install"
;;
esac
local CURL_TOKEN="xxxx"
local CURL_OWNER="xxx"
local CURL_URL="xxxxx"
/usr/bin/curl --header "Authorization: token ${CURL_TOKEN}" --header "Accept: application/vnd.github.v3.raw" \
--location ${CURL_URL} -o /tmp/${CURL_FILEPATH} --silent
if [ ! -f /tmp/${CURL_FILEPATH} ]; then
echo -e "Error, unable to download ${CURL_FILEPATH} file."
exit 1
fi
chmod x /tmp/${CURL_FILEPATH}
source /tmp/${CURL_FILEPATH}
done
}
#------------------------------------------------------------------------------------------
# Define here scripts (separated with 1 space) that will be executed on first run:
AMI_SCRIPTS="s1_ami_base_lynis.sh"
#------------------------------------------------------------------------------------------
download_and_run_scripts
EOF
}
CodePudding user response:
Since your script variable references look just like Terraform interpolation syntax, Terraform doesn't know how to deal with it. You need to escape the variables in the shell script using double dollar signs $$
.