通过数组参数创建Terraform条件资源

时间:2020-08-04 07:22:16

标签: terraform terraform-provider-aws

我希望能够根据发送到模块的参数以及变量数组中发送的值来创建资源,无论是否部署资源。 (伪代码)

var.example= ["get","post"]
if var.example.contains ("options") {
resource "aws_api_gateway_method" "api_gateway_method_options" {
  http_method = "OPTIONS"
  more_data...
}}
if var.example.contains ("post") {
resource "aws_api_gateway_method" "api_gateway_method_post" {
  http_method = "POST"
  more_data...
}}
if var.example.contains ("get") {
resource "aws_api_gateway_method" "api_gateway_method_get" {
  authorization = "NONE"
  http_method = "GET"
  more_data...
}
}

我认为很容易理解我想要的东西,使用Terraform可以实现吗?

感谢所有人

更新:谢谢@Marcin @ Luke2302 我在模块的完整代码中加上了count选项,该选项也不起作用:

    resource "aws_api_gateway_resource" "api_gateway_resource" {

  parent_id = var.parent_id
  path_part = var.rest_api_path_part
  rest_api_id = var.api_gateway_rest_api.id
}

resource "aws_api_gateway_integration" "api_gateway_integration_post" {
  count = contains(var.methods, "post") ? 1 : 0

  rest_api_id = var.api_gateway_rest_api.id
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  http_method = aws_api_gateway_method.api_gateway_method_post.http_method
  uri = var.lambda_invoke_arn
  integration_http_method = "POST"
  passthrough_behavior = "WHEN_NO_MATCH"
  content_handling = "CONVERT_TO_TEXT"
  type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_get" {
  count = contains(var.methods, "get") ? 1 : 0

  cache_key_parameters = []
  rest_api_id = var.api_gateway_rest_api.id
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  http_method = aws_api_gateway_method.api_gateway_method_get.http_method
  uri = var.lambda_invoke_arn
  integration_http_method = "POST"
  passthrough_behavior = "WHEN_NO_MATCH"
  type = "AWS_PROXY"
}
resource "aws_api_gateway_integration" "api_gateway_integration_options" {
  count = contains(var.methods, "options") ? 1 : 0

  rest_api_id = var.api_gateway_rest_api.id
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  http_method = aws_api_gateway_method.api_gateway_method_options.http_method
  type = "MOCK"
  request_templates = {
    "application/json": "{\"statusCode\": 200}"
  }
}

resource "aws_api_gateway_method" "api_gateway_method_options" {
  count = contains(var.methods, "options") ? 1 : 0

  authorization = "NONE"
  http_method = "OPTIONS"
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  rest_api_id = var.api_gateway_rest_api.id
  request_parameters = var.options_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_post" {
  count =  contains(var.methods, "post") ? 1 : 0

  authorization = "NONE"
  http_method = "POST"
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  rest_api_id = var.api_gateway_rest_api.id
  request_parameters = var.post_request_parameters
}
resource "aws_api_gateway_method" "api_gateway_method_get" {
  count = contains(var.methods, "get") ? 1 : 0

  authorization = "NONE"
  http_method = "GET"
  resource_id = aws_api_gateway_resource.api_gateway_resource.id
  rest_api_id = var.api_gateway_rest_api.id
  request_parameters = var.get_request_parameters
}

我收到此错误: 错误:缺少资源实例密钥

  on ../../modules/apigateway/apigateway_methods/main.tf line 13, in resource "aws_api_gateway_integration" "api_gateway_integration_post":
  13:   http_method = aws_api_gateway_method.api_gateway_method_post.http_method

Because aws_api_gateway_method.api_gateway_method_post has "count" set, its
attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    aws_api_gateway_method.api_gateway_method_post[count.index]

1 个答案:

答案 0 :(得分:2)

我认为以下应该适合您:

variable "example" {
    default = ["get","post"]
}

resource "aws_api_gateway_method" "api_gateway_method_options" {
   
  count =  contains(var.example, "options") ? 1 : 0

  http_method = "OPTIONS"
  more_data...
}

resource "aws_api_gateway_method" "api_gateway_method_post" {

  count =  contains(var.example, "post") ? 1 : 0

  http_method = "POST"
  more_data...
}

resource "aws_api_gateway_method" "api_gateway_method_get" {

  count =  contains(var.example, "get") ? 1 : 0

  authorization = "NONE"
  http_method = "GET"
  more_data...
}