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_sourceoptional computed - string -
arnoptional computed - string -
binary_media_typesoptional computed - list of string -
bodyoptional - string -
created_dateoptional computed - string -
descriptionoptional computed - string -
disable_execute_api_endpointoptional computed - bool -
execution_arnoptional computed - string -
idoptional computed - string -
minimum_compression_sizeoptional - number -
namerequired - string -
parametersoptional - map from string to string -
policyoptional computed - string -
root_resource_idoptional computed - string -
tagsoptional - map from string to string -
endpoint_configurationlist block-
typesrequired - list of string -
vpc_endpoint_idsoptional 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
bodyargument (with other arguments serving as overrides) or via other Terraform resources to manage the resources (aws_api_gateway_resourceresource), methods (aws_api_gateway_methodresource), integrations (aws_api_gateway_integrationresource), etc. of the REST API. Once the REST API is configured, theaws_api_gateway_deploymentresource can be used along with theaws_api_gateway_stageresource 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
-
ApiKeySourceTypeoptional - String -
BinaryMediaTypesoptional - List -
Bodyoptional - Json -
BodyS3Locationoptional - S3Location -
CloneFromoptional - String -
Descriptionoptional - String -
DisableExecuteApiEndpointoptional - Boolean -
EndpointConfigurationoptional - EndpointConfiguration -
FailOnWarningsoptional - Boolean -
MinimumCompressionSizeoptional - Integer -
Modeoptional - String -
Nameoptional - String -
Parametersoptional - Map -
Policyoptional - Json -
Tagsoptional - List of Tag
Explanation in CloudFormation Registry
The
AWS::ApiGateway::RestApiresource 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.