中使用动态资源名称

中使用动态资源名称

本文介绍了如何在 Terraform 中使用动态资源名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为多个开发和生产环境使用相同的 terraform 模板.

I would like to use the same terraform template for several dev and production environments.

我的方法:据我了解,资源名称必须是唯一的,并且 terraform 在内部存储资源的状态.因此,我尝试使用变量作为资源名称 - 但似乎不受支持.我收到一条错误消息:

My approach:As I understand it, the resource name needs to be unique, and terraform stores the state of the resource internally. I therefore tried to use variables for the resource names - but it seems to be not supported. I get an error message:

$ terraform plan
var.env1
  Enter a value: abc

Error asking for user input: Error parsing address 'aws_sqs_queue.SqsIntegrationOrderIn${var.env1}': invalid resource address "aws_sqs_queue.SqsIntegrationOrderIn${var.env1}"

我的地形模板:

variable "env1" {}

provider "aws" {
        region = "ap-southeast-2"
}

resource "aws_sqs_queue" "SqsIntegrationOrderIn${var.env1}" {
        name = "Integration_Order_In__${var.env1}"
        message_retention_seconds = 86400
        receive_wait_time_seconds = 5
}

我认为,要么我的方法错误,要么语法错误.有什么想法吗?

I think, either my approach is wrong, or the syntax. Any ideas?

推荐答案

我建议使用不同的 工作区为每个环境.这允许您像这样指定您的配置:

I recommend using a different workspace for each environment. This allows you to specify your configuration like this:

variable "env1" {}

provider "aws" {
        region = "ap-southeast-2"
}

resource "aws_sqs_queue" "SqsIntegrationOrderIn" {
        name = "Integration_Order_In__${var.env1}"
        message_retention_seconds = 86400
        receive_wait_time_seconds = 5
}

确保根据环境命名aws_sqs_queue"资源(例如,通过将其包含在名称中)以避免在 AWS 中发生名称冲突.

Make sure to make the name of the "aws_sqs_queue" resource depending on the environment (e.g. by including it in the name) to avoid name conflicts in AWS.

这篇关于如何在 Terraform 中使用动态资源名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:55