Skip to content

Instantly share code, notes, and snippets.

@distractdiverge
Last active March 3, 2020 23:11
Show Gist options
  • Save distractdiverge/04a01bb8e6c99bbfbf67720acbd347fa to your computer and use it in GitHub Desktop.
Save distractdiverge/04a01bb8e6c99bbfbf67720acbd347fa to your computer and use it in GitHub Desktop.
Terraform Params and Iteration
provider "aws" {
region = "us-east-1"
version = "~> 2.0"
}
data "aws_region" "current" {}
data "aws_caller_identity" "caller" {}
locals {
module_name = "api_gateway"
region_name = data.aws_region.current.name
account_id = data.aws_caller_identity.caller.account_id
}
resource "aws_api_gateway_rest_api" "rest_api" {
name = "Test API Gateway"
endpoint_configuration {
types = ["REGIONAL"]
}
tags = {
Project = "Testing"
}
}
locals {
EMPTY_JSON_RESPONSE = {
"application/json" = "Empty"
}
}
locals {
paths = {
path_endpoint = {
path = "path"
method = "POST"
authorization = "NONE"
responses = [
{
status_code = "200"
response_models = local.EMPTY_JSON_RESPONSE
},
{
status_code = "500"
response_models = local.EMPTY_JSON_RESPONSE
}
]
}
query_endpoint = {
path = "query"
method = "GET"
authorization = "NONE"
responses = [
{
status_code = "200"
response_models = local.EMPTY_JSON_RESPONSE
},
{
status_code = "500"
response_models = local.EMPTY_JSON_RESPONSE
}
]
}
callback_endpoint = {
path = "callback"
method = "GET"
authorization = "NONE"
responses = [
{
status_code = "200"
response_models = local.EMPTY_JSON_RESPONSE
},
{
status_code = "500"
response_models = local.EMPTY_JSON_RESPONSE
}
]
}
}
}
#
# Create one resource for each path entry in 'local.paths'
#
resource "aws_api_gateway_resource" "resource" {
for_each = local.paths
rest_api_id = aws_api_gateway_rest_api.rest_api.id
parent_id = aws_api_gateway_rest_api.rest_api.root_resource_id
path_part = each.value.path
}
#
# Create one corresponding method for each path entry in 'local.paths'
#
resource "aws_api_gateway_method" "method" {
for_each = local.paths
rest_api_id = aws_api_gateway_rest_api.rest_api.id
resource_id = aws_api_gateway_resource.resource[each.key].id
http_method = each.value.method
authorization = each.value.authorization
}
# Explode the nested lists within each path
locals {
method_responses = flatten([
for path_name, path in local.paths : [
for index, response in local.paths[path_name].responses :
{
path_key = path_name
status_code = response.status_code
response_models = response.response_models
}
]
])
}
#
# Create a response for each of the responses within each of the paths
#
resource "aws_api_gateway_method_response" "response" {
# Make a unique key for each resource
for_each = { for value in local.method_responses : "${value.path_key}_${value.status_code}" => value }
http_method = aws_api_gateway_method.method[each.value.path_key].http_method
resource_id = aws_api_gateway_resource.resource[each.value.path_key].id
rest_api_id = aws_api_gateway_rest_api.rest_api.id
status_code = each.value.status_code
response_models = each.value.response_models
}

Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.

data.aws_region.current: Refreshing state... data.aws_caller_identity.caller: Refreshing state...


An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:

  • create

Terraform will perform the following actions:

aws_api_gateway_method.method["callback_endpoint"] will be created

  • resource "aws_api_gateway_method" "method" {
    • api_key_required = false
    • authorization = "NONE"
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • rest_api_id = (known after apply) }

aws_api_gateway_method.method["path_endpoint"] will be created

  • resource "aws_api_gateway_method" "method" {
    • api_key_required = false
    • authorization = "NONE"
    • http_method = "POST"
    • id = (known after apply)
    • resource_id = (known after apply)
    • rest_api_id = (known after apply) }

aws_api_gateway_method.method["query_endpoint"] will be created

  • resource "aws_api_gateway_method" "method" {
    • api_key_required = false
    • authorization = "NONE"
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • rest_api_id = (known after apply) }

aws_api_gateway_method_response.response["callback_endpoint_200"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "200" }

aws_api_gateway_method_response.response["callback_endpoint_500"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "500" }

aws_api_gateway_method_response.response["path_endpoint_200"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "POST"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "200" }

aws_api_gateway_method_response.response["path_endpoint_500"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "POST"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "500" }

aws_api_gateway_method_response.response["query_endpoint_200"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "200" }

aws_api_gateway_method_response.response["query_endpoint_500"] will be created

  • resource "aws_api_gateway_method_response" "response" {
    • http_method = "GET"
    • id = (known after apply)
    • resource_id = (known after apply)
    • response_models = {
      • "application/json" = "Empty" }
    • rest_api_id = (known after apply)
    • status_code = "500" }

aws_api_gateway_resource.resource["callback_endpoint"] will be created

  • resource "aws_api_gateway_resource" "resource" {
    • id = (known after apply)
    • parent_id = (known after apply)
    • path = (known after apply)
    • path_part = "callback"
    • rest_api_id = (known after apply) }

aws_api_gateway_resource.resource["path_endpoint"] will be created

  • resource "aws_api_gateway_resource" "resource" {
    • id = (known after apply)
    • parent_id = (known after apply)
    • path = (known after apply)
    • path_part = "path"
    • rest_api_id = (known after apply) }

aws_api_gateway_resource.resource["query_endpoint"] will be created

  • resource "aws_api_gateway_resource" "resource" {
    • id = (known after apply)
    • parent_id = (known after apply)
    • path = (known after apply)
    • path_part = "query"
    • rest_api_id = (known after apply) }

aws_api_gateway_rest_api.rest_api will be created

  • resource "aws_api_gateway_rest_api" "rest_api" {
    • api_key_source = "HEADER"

    • arn = (known after apply)

    • created_date = (known after apply)

    • execution_arn = (known after apply)

    • id = (known after apply)

    • minimum_compression_size = -1

    • name = "Test API Gateway"

    • root_resource_id = (known after apply)

    • tags = {

      • "Project" = "Testing" }
    • endpoint_configuration {

      • types = [
        • "REGIONAL", ] } }

Plan: 13 to add, 0 to change, 0 to destroy.


Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment