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

main.tf#L1
resource "aws_api_gateway_rest_api" "Api GW" {
  name        = "My Api Gateway"
  description = "This is my API"
}

resource "aws_api_gateway_rest_api" "example" {
main.tf#L1
resource "aws_api_gateway_rest_api" "pass" {
  name = var.name

  policy = ""

  endpoint_configuration {
positive.tf#L1
resource "aws_api_gateway_rest_api" "positive1" {
  name = "regional-example"

  endpoint_configuration {
    types = ["REGIONAL"]
  }
api_gateway_rest_api_test.tf#L12
resource "aws_api_gateway_rest_api" "api" {
  name        = "rest-api-gateway"
  description = "Rest API Gateway"
}

resource "aws_api_gateway_rest_api" "my_rest_api" {

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

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, the aws_api_gateway_deployment resource can be used along with the aws_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.

risk-label

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.

risk-label

aws_api_gateway_domain_name

Ensure to use modern TLS protocols

It is better to adopt TLS v1.2+.

risk-label

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).

risk-label

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.

Review your AWS API Gateway settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

tree.json#L225
                  "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
                  "aws:cdk:cloudformation:props": {
                    "description": "Observability Entry Point",
                    "name": "observeAPI"
                  }
                },
tree.json#L216
                  "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
                  "aws:cdk:cloudformation:props": {
                    "name": "crypto-api"
                  }
                },
                "constructInfo": {
tree.json#L216
                  "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
                  "aws:cdk:cloudformation:props": {
                    "name": "crypto-api"
                  }
                },
                "constructInfo": {
tree.json#L155
                  "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
                  "aws:cdk:cloudformation:props": {
                    "name": "helloworld60C9E878:ApiEventSourceA7A86A4F"
                  }
                },
                "constructInfo": {
tree.json#L155
                  "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
                  "aws:cdk:cloudformation:props": {
                    "name": "helloworld60C9E878:ApiEventSourceA7A86A4F"
                  }
                },
                "constructInfo": {

Parameters

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.