Terraform - API Gateway
terraform plan
terraform apply
variable "environment" {
type = string
default = "dev"
description = "The environment to deploy to"
}
variable "application" {
type = string
default = "may"
description = "The application name"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
}
}
required_version = ">= 1.5.7"
}
provider "aws" {
region = "ap-southeast-2"
}
resource "aws_api_gateway_rest_api" "api_gateway_api" {
name = "${var.application}-api-gateway-${var.environment}"
description = "API Gateway for Google Merchant Center ${var.application}"
endpoint_configuration {
types = ["REGIONAL"]
}
}
locals {
api_resources = {
products = {
path = "products"
http_method = "GET"
},
orders = {
path = "orders"
http_method = "GET"
}
}
}
resource "aws_api_gateway_resource" "resources" {
for_each = local.api_resources
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
parent_id = aws_api_gateway_rest_api.api_gateway_api.root_resource_id
path_part = each.value.path
}
resource "aws_api_gateway_method" "methods" {
for_each = local.api_resources
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
resource_id = aws_api_gateway_resource.resources[each.key].id
http_method = each.value.http_method
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integrations" {
for_each = local.api_resources
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
resource_id = aws_api_gateway_resource.resources[each.key].id
http_method = aws_api_gateway_method.methods[each.key].http_method
type = "MOCK"
request_templates = {
"application/json" = jsonencode({
statusCode = 200
})
}
}
resource "aws_api_gateway_method_response" "method_response" {
for_each = local.api_resources
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
resource_id = aws_api_gateway_resource.resources[each.key].id
http_method = aws_api_gateway_method.methods[each.key].http_method
status_code = "200"
}
resource "aws_api_gateway_integration_response" "integration_response" {
for_each = local.api_resources
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
resource_id = aws_api_gateway_resource.resources[each.key].id
http_method = aws_api_gateway_method.methods[each.key].http_method
status_code = aws_api_gateway_method_response.method_response[each.key].status_code
response_templates = {
"application/json" = jsonencode({
message = "This is a mock response for ${each.key}"
data = {
key1 = "value1"
key2 = "value2"
}
})
}
depends_on = [
aws_api_gateway_method.methods,
aws_api_gateway_integration.integrations
]
}
resource "aws_api_gateway_deployment" "api_gateway_deployment" {
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
description = "Deployment of API Gateway for Google Merchant Center ${var.application}"
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_rest_api.api_gateway_api.body,
values(aws_api_gateway_resource.resources)[*].id,
values(aws_api_gateway_method.methods)[*].id,
values(aws_api_gateway_integration.integrations)[*].id,
]))
}
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_api_gateway_method.methods,
aws_api_gateway_integration.integrations,
]
}
resource "aws_api_gateway_stage" "api_gateway_stage" {
deployment_id = aws_api_gateway_deployment.api_gateway_deployment.id
rest_api_id = aws_api_gateway_rest_api.api_gateway_api.id
stage_name = var.environment
}