简体   繁体   English

Terraform - 如何将列表转换为 map(如何使用 terraform 获取 AMI 标签)

[英]Terraform - How to convert lists into map (How to fetch AMI tags using terraform)

I am trying to fetch the tags of AMI using AWS CLI and want to reuse the values from the output. I have a terraform code below which is returning outputs in string format(Maybe not sure of format) which I want to convert into a map object.我正在尝试使用 AWS CLI 获取 AMI 的标签,并希望重用 output 中的值。我下面有一个 terraform 代码,它以字符串格式(可能不确定格式)返回输出,我想将其转换为 map object。

variable "ami" {
  default = "ami-xxxx"
}

locals {
  tags = {
  "platform"    = lookup(data.local_file.read_tags.content, "platform", "") #Expecting to get platform from Map of read_tags
  }
}

data "template_file" "log_name" {
  template = "${path.module}/output.log"
}

resource "null_resource" "ami_tags" {
  provisioner "local-exec" {
    command = "aws ec2 describe-tags --filters Name=resource-id,Values=${var.ami} --query Tags[*].[Key,Value] > ${data.template_file.log_name.rendered}"
  }
}

data "local_file" "read_tags" {
  filename = "${data.template_file.log_name.rendered}"
  depends_on = ["null_resource.ami_tags"]
}

output "tags" {
  value = local.tags
}

output "cli-output-tags" {
  value = "${concat(data.local_file.read_tags.content)}"
}

output of cli-output-tags is below: cli-output-tags的 output 如下:

[
    [
        "ENV",
        "DEV"
    ],
    [
        "Name",
        "Base-AMI"
    ],
    [
        "platform",
        "Linux"
    ]
]

How can I convert this output into Map as below using terraform/(jq command), or is there any other way to fetch required values directly from cli-output-tags output:我如何使用 terraform/(jq 命令)将此 output 转换为 Map,或者是否有任何其他方法直接从cli-output-tags output 获取所需的值:

{
ENV  = "DEV",
Name = "Base-AMI",
platform = "Linux"
}

I have also tried changing the CLI command a bit like below but still not able to fetch values as expected:我也试过像下面那样更改 CLI 命令,但仍然无法按预期获取值:

'Tags[].{Key:Key,Value:Value}'

Resulted below output:结果如下 output:

[
    {
        "Key": "ENV",
        "Value": "DEV"
    },
    {
        "Key": "Name",
        "Value": "Base-AMI"
    },
    {
        "Key": "platform",
        "Value": "Linux"
    }
]

You could use zipmap :你可以使用zipmap

output "cli-output-tags" {
  value = zipmap(
        jsondecode(data.local_file.read_tags.content)[*][0],
        jsondecode(data.local_file.read_tags.content)[*][1]
      )         
}

The code first changes string data from your file to json, then gets all first elements [*][0] (same for second elements [*][1] ), and zips them into map.该代码首先将文件中的字符串数据更改为 json,然后获取所有第一个元素[*][0] (第二个元素[*][1]相同),并将它们压缩到 map。

How can I convert this output into Map as below我怎样才能将这个 output 转换成 Map 如下

One way would be to use jq as follows (assuming cli-output-tags is the name of the file holding the JSON array of arrays):一种方法是按如下方式使用 jq(假设cli-output-tags是保存 JSON 数组的文件的名称):

jq -r -f '"{", (.[] | "\(.[0]) = \"\(.[1])\""), "}"' cli-output-tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 terraform (0.11.13) 中将 aws secret manager 字符串转换为 map - How to convert the aws secret manager string to map in terraform (0.11.13) 如何使用 terraform 从 Ami(具有多个卷 -3+)旋转 ec2 - How to spin the ec2 from Ami(with multiple volumes -3+) using terraform 如何将标签添加到 ECS 现有服务,使用 Terraform 定义任务 - How to add Tags to ECS existing Service, Task definition using Terraform 如何在 Terraform 中 DRY 重复块列表 - How to DRY repeating block lists in Terraform 如何使用 terraform 将 EIP 附加到现有的 ENI(在 terraform 之外创建) - How to attach EIP to a existing ENI (Created outside of terraform) using terraform 我如何自动将 output ami id 链接到 terraform 变量? - How can I chain packer output ami id to terraform variables automatically? 如何使用 CDKTF 创建的 terraform 部署 Lambda? - How to deploy Lambda using terraform created by CDKTF? 如何使用 Terraform 将 LogAnalyticsWorkSpace 与数据工厂集成? - How to integrate LogAnalyticsWorkSpace with Data Factory using Terraform? 如何在 Terraform 中为 AWS EC2 应用不同的 TAG - How to apply different TAGs for AWS EC2 in Terraform 如何在terraform应用中移动terraform资源 - How to move terraform resource in terraform apply
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM