如何在资源参数上创建if语句?

发布于 2025-02-05 00:48:33 字数 787 浏览 2 评论 0原文

我有循环在AWS上创建2个EC2。我想在其中唯一的一个上传递user_data参数,因此我的想法是创建IF语句以实现这一目标。 类似的内容:

实例EC2

resource "aws_instance" "web" {
  count                       = length(var.vms)
  ami                         = data.aws_ami.ubuntu.id
  instance_type               = var.instance_type
  key_name                    = var.key_name
  get_password_data           = false
  associate_public_ip_address = true
  vpc_security_group_ids      = [var.secgr_id]
  iam_instance_profile        = aws_iam_instance_profile.ec2_instance_profile.name
  user_data = var.vms[count.index] == "some-vm-name" ? "${file(var.file_name)}" : null
  tags = {
    Name = var.vms[count.index]
  }
  lifecycle {
    prevent_destroy = true
  }
}

它实际上选择了我想要的VM,但是我从未执行过通过文件函数执行脚本吗?

这可以做吗?

I have for loop that creates 2 ec2s on aws. I want to pass user_data argument on the only one of them, so my idea is to create if statement to accomplish this.
Something like this:

Instance EC2

resource "aws_instance" "web" {
  count                       = length(var.vms)
  ami                         = data.aws_ami.ubuntu.id
  instance_type               = var.instance_type
  key_name                    = var.key_name
  get_password_data           = false
  associate_public_ip_address = true
  vpc_security_group_ids      = [var.secgr_id]
  iam_instance_profile        = aws_iam_instance_profile.ec2_instance_profile.name
  user_data = var.vms[count.index] == "some-vm-name" ? "${file(var.file_name)}" : null
  tags = {
    Name = var.vms[count.index]
  }
  lifecycle {
    prevent_destroy = true
  }
}

It actually choses VM that i want, but the script is never executed that i pass through file function?

Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文