Step-by-Step To Create AWS API Gateway With Terraform

Step 1: Launch An Instance

  • Launch EC2 instance with Amazon Linux2 Kernel 5.10(AMI) along with port numbers set SSH – 22, HTTP 80 and select storage t2.micro.

  • Now connect with git bash terminal or any terminal like powershell, putty, e.t.c. by using SSH Client

Step 2: Install Terraform

  • Now install terraform packages from official site of hashicorp or follow below commands
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo


  • Now install terraform by using following commands
sudo yum -y install terraform


Step 3: Create A File And Write Terraform Script For AWS Route 53 Using Terraform

  • Create a file with .tf extension in that file write a script by using following command
 vi <filename>.tf
  • .tf is a extension of terraform without this extension we cannot create a terraform file

Provider Configuration

  • This section specifies the AWS provider and sets the region to “us-east-1”. The provider block configures the authentication details and default settings for interacting with AWS.
provider "aws" {
region = "us-east-1"
}

Creating API Gateway Resource

  • This block defines an API Gateway REST API named “example-api” with a description “Example API Gateway”.
resource "aws_api_gateway_rest_api" "example_api" {
name = "example-api"
description = "Example API Gateway"
}
resource "aws_api_gateway_resource" "example_resource" {
rest_api_id = aws_api_gateway_rest_api.example_api.id
parent_id = aws_api_gateway_rest_api.example_api.root_resource_id
path_part = "example"
}


API Gateway Method

  • This block defines an HTTP GET method for the “/example” resource with no authorization required. It associates the method with the API Gateway REST API and resource using their IDs.
resource "aws_api_gateway_method" "example_method" {
rest_api_id = aws_api_gateway_rest_api.example_api.id
resource_id = aws_api_gateway_resource.example_resource.id
http_method = "GET"
authorization = "NONE"
}


API Gateway Integration

  • This block integrates the API Gateway method with an AWS Lambda function. It specifies that the integration uses the AWS_PROXY type, and the URI of the Lambda function is provided using an interpolated string that dynamically inserts the AWS account ID of the current caller.
resource "aws_api_gateway_integration" "example_integration" {
rest_api_id = aws_api_gateway_rest_api.example_api.id
resource_id = aws_api_gateway_resource.example_resource.id
http_method = aws_api_gateway_method.example_method.http_method
integration_http_method = "GET"
type = "AWS_PROXY"
uri = "arn:aws:lambda:us-east-1:${data.aws_caller_identity.current.account_id}:function:example-lambda" # here give your URI ID
}


API Gateway Deployment

This block deploys the API Gateway configuration to a stage named “dev”. It depends on the integration being created before deployment, and it associates the deployment with the API Gateway REST API using its ID.

resource "aws_api_gateway_deployment" "example_deployment" {
depends_on = [aws_api_gateway_integration.example_integration]
rest_api_id = aws_api_gateway_rest_api.example_api.id
stage_name = "dev"
}



AWS Caller Identity Data Source

This block retrieves information about the AWS account identity, such as the account ID. It is used to dynamically generate the URI for the Lambda function integration.

data "aws_caller_identity" "current" {}


Output

This block exposes the invoke URL of the deployed API Gateway as an output variable named “api_endpoint”. The invoke URL can be used to access the API.

output "api_endpoint" {
value = aws_api_gateway_deployment.example_deployment.invoke_url
}


Step 4: Now Initialize Terraform And Execute Terraform Commands

  • Now initialize terraform by using following command
terraform init

  • Now execute terraform execution commands by using following commands
terraform fmt 
terraform validate
terraform plan

Now execute terraform apply by using following command

terraform apply --auto-approve

The following screenshot shows that we successfully created AWS API Gateway with Terraform

Complete Guide To Create AWS API Gateway With Terraform

AWS Application Programming Interface (API) Gateway is a completely managed service presented by Amazon Web Services (AWS) that empowers developers to create, monitor, deploy, and secure APIs at any scale. It goes about as a gateway for managing and routing HTTP and WebSocket traffic to backend service, including AWS Lambda function, Amazon EC2 instance, and other HTTP endpoints.

Terraform, then again, is an open-source infrastructure as code (IaC) tool by HashiCorp, intended to automate the provisioning and the executives of cloud infrastructure. With Terraform, users can characterize their infrastructure resources utilizing declarative configuration files, ensuring consistency and reproducibility across various conditions.

By combining AWS Application Programming Interface (API) Gateway with Terraform, associations can streamline the most common way of provisioning and managing APIs in the AWS cloud environment. Terraforms infrastructure as code approach permits clients to define Application Programming interface gateway resources, including endpoints, techniques, integrations, and approvals, in a version-controlled and reusable way.

Similar Reads

What Is AWS API Gateway?

API Gateway is a completely managed service given by cloud platforms, outstandingly Amazon Web Services (AWS), that allows developers to create, distribute, maintain, monitor, and secure Application Programming Interfaces (APIs) at any scale. It goes about as a front door for applications to get to data, business logic, or usefulness from backend services, for example, Lambda functions, EC2 instances, or other AWS services, as well as on-premises resources....

Key Features Of Application Programming Interface (API) Gateway

The following are the key features of API Gateway:...

Step-by-Step To Create AWS API Gateway With Terraform

Step 1: Launch An Instance...

Conclusion

AWS API Gateway is a powerful service that enables developers to create, manage, and secure APIs at scale. All through this article, we’ve explored the primary elements and functionalities of API Gateway, including Application Programming interface (API) creation, mix with backend service, security mechanisms, monitoring, and adaptability....

AWS APIGateway And Terraform – FAQ’s

How Does AWS API Gateway Pricing Work?...