[英]How to create AWS SSM Parameter from Terraform
I am trying to copy AWS SSM parameter(for cloudwatch) from one region to another.我正在尝试将 AWS SSM 参数(用于 cloudwatch)从一个区域复制到另一个区域。 I have the json which is created as a String in one region.
我有一个在一个区域中作为字符串创建的 json。
I am trying to write a terraform script to create this ssm parameter in another region.我正在尝试编写一个 terraform 脚本来在另一个区域创建这个 ssm 参数。
According to the terraform documentation, I need to do this根据 terraform 文档,我需要这样做
resource "aws_ssm_parameter" "foo" {
name = "foo"
type = "String"
value = "bar"
}
In my case value is a json.在我的情况下,值是一个 json。 Is there a way to store the json in a file and pass this file as value to the above resource?
有没有办法将 json 存储在文件中并将此文件作为值传递给上述资源? I tried using jsonencode,
我尝试使用 jsonencode,
resource "aws_ssm_parameter" "my-cloudwatch" {
name = "my-cloudwatch"
type = "String"
value = jsonencode({my-json})
that did not work either.那也没有用。 I am getting this error Extra characters after interpolation expression I believe this is because the json has characters like quotes and colon.
我收到此错误插值表达式后的额外字符我相信这是因为 json 包含引号和冒号等字符。
Any idea?任何的想法?
I tested the following & this worked for me:我测试了以下内容,这对我有用:
resource "aws_ssm_parameter" "my-cloudwatch" {
name = "my-cloudwatch"
type = "String"
#value = file("${path.module}/ssm-param.json")
value = jsonencode(file("${path.module}/files/ssm-param.json"))
./files/ssm-param.json content: ./files/ssm-param.json 内容:
{
"Value": "Something"
}
and the parameter store value looks like this:参数存储值如下所示:
"{\n \"Value\": \"Something\"\n}"
I just faced this issue the $ in the CW config is causing the problem.我刚刚遇到了这个问题,CW 配置中的 $ 导致了这个问题。 Use $$
使用 $$
"Note: If you specify the template as a literal string instead of loading a file, the inline template must use double dollar signs (like $${hello}) to prevent Terraform from interpolating values from the configuration into the string. " “注意:如果您将模板指定为文字字符串而不是加载文件,则内联模板必须使用双美元符号(如 $${hello})以防止 Terraform 将配置中的值插入到字符串中。”
https://www.terraform.io/docs/configuration-0-11/interpolation.html https://www.terraform.io/docs/configuration-0-11/interpolation.html
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "$${aws:AutoScalingGroupName}",
"ImageId": "$${aws:ImageId}",
"InstanceId": "$${aws:InstanceId}",
"InstanceType": "$${aws:InstanceType}"
},
I prefer Pauls aproach though.不过我更喜欢保罗的方法。
You need to insert your json with escaped quotas , is a little trick in AWS, and you need to parse this when retrieve:您需要插入带有转义配额的 json,这是 AWS 中的一个小技巧,您需要在检索时解析它:
const value = JSON.parse(Value)
Example of insert:插入示例:
"Value": "\"{\"flag\":\"market_store\",\"app\":\"ios\",\"enabled\":\"false\"}\"",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.