如何在Terraform外部数据源中使用AWS cli命令



如果我自己运行以下命令,我会得到预期的结果-

此:

aws cloudfront list-cloud-front-origin-access-identities | jq -r ' .CloudFrontOriginAccessIdentityList.Items[] | select(.Comment == "Created for Nackle Shared CF in pprd").Id'

返回此:

E1P6ZIBDB6I6FZ

如何使用Terraform外部数据源来获得相同的结果?

我试过这个:

data "external" "json" {
program = ["sh", "-c", "aws cloudfront list-cloud-front-origin-access-identities | jq -r ' .CloudFrontOriginAccessIdentityList.Items[] | select(.Comment == "Created for Nackle Shared CF in pprd").Id'"] 
}
output "map" {
value = ["${values(data.external.json.result)}"] 
}

但当我运行Terraform应用程序-时,它会返回此错误

Expected a comma to mark the beginning of the next item.

我认为当它被正确地书写时;值";会是E1P6ZIBDB6I6FZ吗?

如何在地形的另一部分中将该值用作变量?

有什么不同的方法可以解决这个问题吗?

我是Terraform的新手,从未使用过外部数据源。

外部数据源的json解析能力非常有限。应该是(转义引号并返回新json(:

data "external" "json" {
program = ["sh", "-c", "aws cloudfront list-cloud-front-origin-access-identities | jq -r ' .CloudFrontOriginAccessIdentityList.Items[] | select(.Comment == "Created for Nackle Shared CF in pprd") |  {id: .Id}'"] 
}

然后您访问Id为:

data.external.json.result["id"]

最新更新