Guel135
Guel135

Reputation: 788

Terraform conditional resource creation by array parameters

I want to be able to create a resource depending on what parameter is sent to the module and depends on the values sent in the variable array I will deploy the resource or not. (pseudo code)

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

I think it is quite easy to understand what I want, is that possible with terraform?

Thanks to all

UPDATE: Thanks @Marcin @Luke2302 I put the full code of the module with the count option that is also not working:

    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
}

I got this error: Error: Missing resource instance key

  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]

Upvotes: 0

Views: 1864

Answers (1)

Marcin
Marcin

Reputation: 238957

I think the following should work for you:

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

Upvotes: 3

Related Questions