【发布时间】:2021-09-13 21:30:21
【问题描述】:
我正在使用 Terraform 构建 AWS 基础设施,需要将包含变量的 shell 脚本传递到启动模板的用户数据中。这就是我引用变量中脚本的方式:
**user_data_base64 = filebase64(var.aws_userdata)**
这是我遇到的错误之一:
**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.**
在 Terraform 中是否可以做到这一点?这是脚本的变量:
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
}
【问题讨论】:
-
这表示
s1_aws_userdata存在错误,但您没有显示使用具有该名称的变量的代码。您尝试做的事情当然是可能的,但您需要提供更多信息,以便我们准确了解问题所在。 -
嗨@MarkB,谢谢你的回复。我刚刚在脚本中添加了变量。
标签: bash amazon-web-services terraform