AWS API Gateway Rest API
This page shows how to write Terraform and CloudFormation for API Gateway Rest API and write them securely.
aws_api_gateway_rest_api (Terraform)
The Rest API in API Gateway can be configured in Terraform with the resource name aws_api_gateway_rest_api
. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_api_gateway_rest_api" "Api GW" {
name = "My Api Gateway"
description = "This is my API"
}
resource "aws_api_gateway_rest_api" "example" {
resource "aws_api_gateway_rest_api" "pass" {
name = var.name
policy = ""
endpoint_configuration {
resource "aws_api_gateway_rest_api" "positive1" {
name = "regional-example"
endpoint_configuration {
types = ["REGIONAL"]
}
resource "aws_api_gateway_rest_api" "api" {
name = "rest-api-gateway"
description = "Rest API Gateway"
}
resource "aws_api_gateway_rest_api" "my_rest_api" {
Parameters
-
api_key_source
optional computed - string -
arn
optional computed - string -
binary_media_types
optional computed - list of string -
body
optional - string -
created_date
optional computed - string -
description
optional computed - string -
disable_execute_api_endpoint
optional computed - bool -
execution_arn
optional computed - string -
id
optional computed - string -
minimum_compression_size
optional - number -
name
required - string -
parameters
optional - map from string to string -
policy
optional computed - string -
root_resource_id
optional computed - string -
tags
optional - map from string to string -
endpoint_configuration
list block-
types
required - list of string -
vpc_endpoint_ids
optional computed - set of string
-
Explanation in Terraform Registry
Manages an API Gateway REST API. The REST API can be configured via importing an OpenAPI specification in the
body
argument (with other arguments serving as overrides) or via other Terraform resources to manage the resources (aws_api_gateway_resource
resource), methods (aws_api_gateway_method
resource), integrations (aws_api_gateway_integration
resource), etc. of the REST API. Once the REST API is configured, theaws_api_gateway_deployment
resource can be used along with theaws_api_gateway_stage
resource to publish the REST API. -> Note: Amazon API Gateway Version 1 resources are used for creating and deploying REST APIs. To create and deploy WebSocket and HTTP APIs, use Amazon API Gateway Version 2 resources.
Tips: Best Practices for The Other AWS API Gateway Resources
In addition to the aws_api_gateway_method_settings, AWS API Gateway has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_api_gateway_method_settings
Ensure that API Gateway stage-level cache is encrypted
It is better to enable the stage-level cache encryption which reduces the risk of data leakage.
aws_api_gateway_domain_name
Ensure to use modern TLS protocols
It is better to adopt TLS v1.2+.
aws_api_gateway_stage
Ensure to enable access logging of your API Gateway stage (v1)
It is better to enable the access logging of your API Gateway stage (v1).
aws_api_gateway_method
Ensure that your API Gateway method blocks unwanted access
It is better that the API Gateway method does not allow public access.
AWS::ApiGateway::RestApi (CloudFormation)
The RestApi in ApiGateway can be configured in CloudFormation with the resource name AWS::ApiGateway::RestApi
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"description": "Observability Entry Point",
"name": "observeAPI"
}
},
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"name": "crypto-api"
}
},
"constructInfo": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"name": "crypto-api"
}
},
"constructInfo": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"name": "helloworld60C9E878:ApiEventSourceA7A86A4F"
}
},
"constructInfo": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"name": "helloworld60C9E878:ApiEventSourceA7A86A4F"
}
},
"constructInfo": {
Parameters
-
ApiKeySourceType
optional - String -
BinaryMediaTypes
optional - List -
Body
optional - Json -
BodyS3Location
optional - S3Location -
CloneFrom
optional - String -
Description
optional - String -
DisableExecuteApiEndpoint
optional - Boolean -
EndpointConfiguration
optional - EndpointConfiguration -
FailOnWarnings
optional - Boolean -
MinimumCompressionSize
optional - Integer -
Mode
optional - String -
Name
optional - String -
Parameters
optional - Map -
Policy
optional - Json -
Tags
optional - List of Tag
Explanation in CloudFormation Registry
The
AWS::ApiGateway::RestApi
resource creates a REST API. For more information, see restapi:create in the Amazon API Gateway REST API Reference.Note On January 1, 2016, the Swagger Specification was donated to the OpenAPI initiative, becoming the foundation of the OpenAPI Specification.
Frequently asked questions
What is AWS API Gateway Rest API?
AWS API Gateway Rest API is a resource for API Gateway of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS API Gateway Rest API?
For Terraform, the Danialjim/trambo_terraform, bridgecrewio/checkov and Checkmarx/kics source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the cdugga/go-observability, lvthillo/sam-cdk-demo and lvthillo/sam-cdk-demo source code examples are useful. See the CloudFormation Example section for further details.