Terraform 没有名为“的变量”;地区“;

Terraform 没有名为“的变量”;地区“;,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,使用模板提供程序的ec2 launch tempate userdata定义即将替换为templatefile函数,因为它已被弃用 定义本身可以毫无问题地完成,但是在规划过程中出现了一个错误,即用于获取实例元数据等的处理变量未定义,是否有办法避免这种情况 $terraform版本 地形v0.13.5 以下定义适用于处理 export LOCAL_IP=$(ec2metadata-LOCAL-ipv4) 导出全局_IP=$(ec2metadata--public-ipv4) 导出区域=$(cur

使用模板提供程序的ec2 launch tempate userdata定义即将替换为templatefile函数,因为它已被弃用

定义本身可以毫无问题地完成,但是在规划过程中出现了一个错误,即用于获取实例元数据等的处理变量未定义,是否有办法避免这种情况

$terraform版本
地形v0.13.5
以下定义适用于处理

export LOCAL_IP=$(ec2metadata-LOCAL-ipv4)
导出全局_IP=$(ec2metadata--public-ipv4)
导出区域=$(curl-s)http://169.254.169.254/latest/meta-data/placement/availability-zone |sed's/[a-z]$/')
导出AWS_DEFAULT_REGION=“$${REGION}”
$terraform平面图
错误:未能呈现::13,30-36:未知变量;没有名为“REGION”的变量,以及4个其他诊断变量

谢谢

模板文件
数据源和
模板文件
函数一起使用是没有意义的,因为它们都做相同的事情:渲染模板

在当前配置中,您使用
templatefile
函数呈现模板以生成字符串,然后使用
template\u file
函数将结果解释为模板,因此将对模板进行两次评估

第一次求值将按预期将
$${REGION}
转换为
${REGION}
,然后第二次求值将尝试将
${REGION}
作为模板插值处理

相反,完全删除
template_文件
数据源(已弃用),只需使用
templatefile
功能:

   user_data = templatefile("${path.module}/userdata.sh.tpl, {
     # ...
   })

(我在这里删除了
base64encode
函数,因为
user\u data
需要一个未编码的字符串值,而提供者本身将进行base64编码。如果您自己在这里调用
base64encode
,那么您将得到两个级别的base64编码,我想这不是您想要的。)

什么是
ec2metadata
?默认情况下没有这样的包。您在哪里运行Terraform?@Marcin,
ec2metadata
是启动的EC2实例的元数据。元数据是通过在启动时在本地运行shell@JohnHanley Turn plan获得的,意思是程序
ec2metadata
?是你自己安装的吗?例如,AmazonLinux2上就没有这样的东西。
$ terraform plan
Error: failed to render : <template_file>:13,30-36: Unknown variable; There is no variable named "REGION"., and 4 other diagnostic(s)
   user_data = templatefile("${path.module}/userdata.sh.tpl, {
     # ...
   })