use*_*406 2 amazon-web-services terraform
我在 Terraform 中有这个政策:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "${source_ip}"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有一个变量authorized_ip定义如下:
variable "authorized_ip" {
default = [
"x.x.x.x/x",
"x.x.x.x/x",
]
}
Run Code Online (Sandbox Code Playgroud)
现在我这样调用 Json 策略:
data "template_file" "apigw_policy" {
template = "${file("${path.module}/template/apigateway_policy.json.template")}"
vars = {
source_ip = "${var.authorized_ip}"
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
属性“vars”的值不合适:元素“source_ip”:需要字符串。
所以 Terraform 需要一个字符串而不是列表。如何将列表转换为字符串以获得这样的结果:
"IpAddress": {
"aws:SourceIp": [
"x.x.x.x/x",
"x.x.x.x/x"
]
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处使用该jsonencode功能:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "${jsonencode(source_ip)}"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1957 次 |
| 最近记录: |