简体   繁体   English

如何从 Terraform 创建 AWS SSM 参数

[英]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.

相关问题 如何将变量的 terraform 输出导出/发布到 AWS SSM 参数存储 - how to export / post terraform output of variables to AWS SSM parameter store 如何隐藏 terraform aws_ssm_parameter 值 - How to hide terraform aws_ssm_parameter values 如何使用 Terraform 创建 AWS SSM 文档 Package - How to create an AWS SSM Document Package using Terraform 是否可以从 SSM 命令更新 AWS SSM 参数存储中的参数? - Is it possible to update a parameter in AWS SSM parameter store from an SSM Command? 如何使用 Terraform 在 AWS SSM 参数中存储三元素元组? - How can I store a three element tuple in AWS SSM parameter with Terraform? 如何在Terraform中使用aws_ssm_parameter在'values'参数中输入字典结构? - How to input the dictionary structure in 'values' argument using the aws_ssm_parameter in terraform? 将 AWS SSM 参数存储中的加密数据提取到 terraform var 文件中并将其加密传递,直到在 terraform 代码中调用它 - Pull encrypted data from AWS SSM Parameter store into terraform var file and pass it encrypted till it is called inside the terraform code 替换 AWS SSM 参数存储中的值 - Substitute a value from AWS SSM parameter store Terraform - 复制 AWS SSM 参数 - Terraform - Copy AWS SSM Parameters Terraform - 可选 SSM 参数查找 - Terraform - Optional SSM parameter lookup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM