AWS API Gateway Stage

This page shows how to write Terraform and CloudFormation for API Gateway Stage and write them securely.

aws_api_gateway_stage (Terraform)

The Stage in API Gateway can be configured in Terraform with the resource name aws_api_gateway_stage. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

missing-api-gateway-cache-cluster.tf#L16
resource "aws_api_gateway_stage" "pass" {
  name                  = "example"
  cache_cluster_enabled = true
}

# ruleid: missing-api-gateway-cache-cluster
api_gateway_stage_test.tf#L12
resource "aws_api_gateway_stage" "cache_1" {
  rest_api_id           = "api-id-1"
  stage_name            = "cache-stage-1"
  deployment_id         = "deployment-id-1"
  cache_cluster_enabled = true
  cache_cluster_size    = 0.5
main.tf#L41
resource "aws_api_gateway_stage" "no_assoc" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.no_assoc.id
  stage_name    = "example"
}

aws_api_gateway_stage_xray_denied.tf#L1
resource "aws_api_gateway_stage" "denied" {
}

resource "aws_api_gateway_stage" "denied_2" {
  xray_tracing_enabled = false
}
api_gateway_stage_test.tf#L12
resource "aws_api_gateway_stage" "cache_1" {
  rest_api_id           = "api-id-1"
  stage_name            = "cache-stage-1"
  deployment_id         = "deployment-id-1"
  cache_cluster_enabled = true
  cache_cluster_size    = 0.5

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

Security Best Practices for aws_api_gateway_stage

There are 2 settings in aws_api_gateway_stage that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

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

Ensure to enable API Gateway X-Ray tracing

It is better to enable API Gateway X-Ray tracing. It may help incident responses by providing plenty of request logs.

Review your AWS API Gateway settings

You can check if the aws_api_gateway_stage setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

Explanation in Terraform Registry

Manages an API Gateway Stage. A stage is a named reference to a deployment, which can be done via the aws_api_gateway_deployment resource. Stages can be optionally managed further with the aws_api_gateway_base_path_mapping resource, aws_api_gateway_domain_name resource, and aws_api_method_settings resource. For more information, see the API Gateway Developer Guide.

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_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::Stage (CloudFormation)

The Stage in ApiGateway can be configured in CloudFormation with the resource name AWS::ApiGateway::Stage. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

tree.json#L338
                      "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
                      "aws:cdk:cloudformation:props": {
                        "restApiId": {
                          "Ref": "helloworldapi61D4D3D5"
                        },
                        "deploymentId": {
tree.json#L338
                      "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
                      "aws:cdk:cloudformation:props": {
                        "restApiId": {
                          "Ref": "helloworldapi61D4D3D5"
                        },
                        "deploymentId": {
tree.json#L268
                      "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
                      "aws:cdk:cloudformation:props": {
                        "restApiId": {
                          "Ref": "helloworld60C9E878ApiEventSourceA7A86A4F19AEBC58"
                        },
                        "deploymentId": {
tree.json#L268
                      "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
                      "aws:cdk:cloudformation:props": {
                        "restApiId": {
                          "Ref": "helloworld60C9E878ApiEventSourceA7A86A4F19AEBC58"
                        },
                        "deploymentId": {
AWS_ApiGateway_StageResource.schema.json#L15
      "default" : "AWS::ApiGateway::Stage"
    },
    "Properties" : {
      "$ref" : "AWS_ApiGateway_Stage.schema.json"
    }
  }

Parameters

Explanation in CloudFormation Registry

The AWS::ApiGateway::Stage resource creates a stage for a deployment.

Frequently asked questions

What is AWS API Gateway Stage?

AWS API Gateway Stage 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 Stage?

For Terraform, the returntocorp/semgrep-rules, infracost/infracost and bridgecrewio/checkov source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the mouimet-infinisoft/samtest, mouimet-infinisoft/samtest and johanbjorn/helloworld source code examples are useful. See the CloudFormation Example section for further details.