Terraform 配置编写与动态环境搭建指南
一、使用外部脚本动态配置资源组位置
1.1 操作步骤
- 在包含
main.tf
文件的目录中,创建一个名为 GetLocation.ps1
的 PowerShell 脚本,内容如下:
# Read the JSON payload from stdin
$jsonpayload = [Console]::In.ReadLine()
# Convert JSON to a string
$json = ConvertFrom-Json $jsonpayload
$environment = $json.environment
if($environment -eq "Production"){
$location="westeurope"
}else{
$location="westus"
}
# Write output to stdout
Write-Output "{ ""location"" : ""$location""}"
- 在
main.tf
文件中添加外部块:
data "external" "getlocation" {
program = ["Powershell.exe", "./GetLocation.ps1"]
query = {