Условное создание ресурса Terraform по параметрам массива - PullRequest
0 голосов
/ 04 августа 2020

Я хочу иметь возможность создавать ресурс в зависимости от того, какой параметр отправляется модулю и зависит от значений, отправленных в массиве переменных, я разверну ресурс или нет. (псевдокод)

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 Ответ

1 голос
/ 04 августа 2020

Думаю, у вас должно работать следующее:

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...
}
...