Override API headers in AWS with Terraform using Mapping Template

Introduction :

Applications might require Header can be required to be passed from the API Gateway to the integration platforms like Lambda or any VPC link backend.

Solution :

AWS API gateway parameters and response code mapping template allows you to override any type of request parameter, response header, or response status code in the request.In this blog we are going to override the request headers of the API gateway using Terraform.

The request headers are associated with HTTP methods therefore mapping templates are created at the method level of API gateway.This provides flexibility to have different mapping templates for different HTTP methods like GET, POST, PUT, PATCH, etc.

In the example, we will override the headers of GET method using Pet store API as a sample.

The Terraform folder structure would look like below :

STEP 1 : Create provider.tf file

Terraform configurations must declare which providers they require.It is good practice to restrict Terraform provider version and define AWS Region Example : AWS

# Set up Terraform provider version 
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}  

STEP 2  : Create variable.tf file :

The file defines the custom headers added to the mapping template in the request.The example makes use of two headers : header1 and header2

variable "headers" {
  type = map
  description = "Header for template mapping in the request"
  default = {
    "method.request.header.header1" = true,
    "method.request.header.header2" = true
  } 
}

STEP 3  : Create header-template file:

To create a mapping template override, use the following $context variables in a mapping template named header-template

$input.json("$")
set($content.requestOverride.header.header1 = ''example1"
set($content.requestOverride.header.header1 = ''example2"
set($content.requestOverride.header.header1 = ''example3"
set($content.requestOverride.header.header1 = ''example4" 

STEP 4  : Create main.tf file:

Create main.tf file to write all the main set of configuration of your terraform module.In this example, I am creating a sample API gateway and attaching the above mapping template to override the headers.

resource "aws_api_gateway_rest_api" "SampleAPI" {
  name        = "SampleAPI"
  description = "Sample API for the example"
}

resource "aws_api_gateway_resource" "SampleResource" {
  rest_api_id = aws_api_gateway_rest_api.SampleAPI.id
  parent_id   = aws_api_gateway_rest_api.SampleResource.root_resource_id
  path_part   = "sample"
}

resource "aws_api_gateway_method" "SampleMethod" {
  rest_api_id   = aws_api_gateway_rest_api.SampleAPI.id
  resource_id   = aws_api_gateway_resource.SampleResource.id
  http_method   = "GET"
  authorization = "NONE"
  requested_parameters = var.headers
}

resource "aws_api_gateway_integration" "SampleIntegration" {
  rest_api_id          = aws_api_gateway_rest_api.SampleAPI.id
  resource_id          = aws_api_gateway_resource.SampleMethod.id
  http_method          = aws_api_gateway_method.SampleIntegration.http_method
  type = "HTTP"
  integration_http_method = "GET"
  request_templates = {
    "application/json" = ${file("header_template")}
  }
}

Step 5 : Testing the API Gateway

Go to the AWS management console and select the AWS API Gateway service.

Summary :

Terraform is a robust tool that can be used to manage your AWS infrastructure and serverless platforms like Lambda and API gateways. In this blog post, we’ve covered how to use Terraform to create and deploy a simple AWS Lambda function.


Leave a Comment

Your email address will not be published.