如何将服务器ID元组展平为字符串? [英] How to flatten a tuple of server ids to a string?

查看:10
本文介绍了如何将服务器ID元组展平为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个文件,其中包含使用 count 属性生成的多个服务器主机的 ID:

I'm trying to create a file that includes the ID's of multiple server hosts that were generated with the count attribute:

resource "aws_instance" "workers" {
  count                  = "${var.worker_count}"
  ...
}

resource "local_file" "stop_instances" {
  filename = "${path.module}/generated/stop_instances.py"
  content =<<EOF
import boto3

# Boto Connection
ec2 = boto3.resource('ec2', '${var.region}')

def lambda_handler(event, context):
  # Retrieve instance IDs
  instance_ids = ["${aws_instance.controller.id}", "${aws_instance.gateway.id}", "${aws_instance.workers.*.id}"]

  # stopping instances
  stopping_instances = ec2.instances.filter(InstanceIds=instance_ids).stop()
EOF
}

但是,我收到以下错误:

However, I'm getting the following error:

 447:   instance_ids = ["${aws_instance.controller.id}", 
              "${aws_instance.gateway.id}", "${aws_instance.workers.*.id}"]
 449: 
 450: 
 451: 
    |----------------
    | aws_instance.workers is tuple with 3 elements

Cannot include the given value in a string template: string required.

有没有办法可以将元组展平为字符串?

Is there a way that I can flatten the tuple to a string?

我已经尝试过 tostring() 方法,但是只接受原始类型.

I've tried the tostring() method, but that only accepts primitive types.

推荐答案

加入 是我的解决方案:

instance_ids = ["${aws_instance.controller.id}","${aws_instance.gateway.id}","${join("","", aws_instance.workers.*.id)}"]

这篇关于如何将服务器ID元组展平为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆