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
resource "aws_api_gateway_stage" "pass" {
name = "example"
cache_cluster_enabled = true
}
# ruleid: missing-api-gateway-cache-cluster
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
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"
}
resource "aws_api_gateway_stage" "denied" {
}
resource "aws_api_gateway_stage" "denied_2" {
xray_tracing_enabled = false
}
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
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.
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).
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.
Parameters
-
arn
optional computed - string -
cache_cluster_enabled
optional - bool -
cache_cluster_size
optional - string -
client_certificate_id
optional - string -
deployment_id
required - string -
description
optional - string -
documentation_version
optional - string -
execution_arn
optional computed - string -
id
optional computed - string -
invoke_url
optional computed - string -
rest_api_id
required - string -
stage_name
required - string -
tags
optional - map from string to string -
variables
optional - map from string to string -
xray_tracing_enabled
optional - bool -
access_log_settings
list block-
destination_arn
required - string -
format
required - string
-
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 theaws_api_gateway_base_path_mapping
resource,aws_api_gateway_domain_name
resource, andaws_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.
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_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::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
"aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
"aws:cdk:cloudformation:props": {
"restApiId": {
"Ref": "helloworldapi61D4D3D5"
},
"deploymentId": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
"aws:cdk:cloudformation:props": {
"restApiId": {
"Ref": "helloworldapi61D4D3D5"
},
"deploymentId": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
"aws:cdk:cloudformation:props": {
"restApiId": {
"Ref": "helloworld60C9E878ApiEventSourceA7A86A4F19AEBC58"
},
"deploymentId": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage",
"aws:cdk:cloudformation:props": {
"restApiId": {
"Ref": "helloworld60C9E878ApiEventSourceA7A86A4F19AEBC58"
},
"deploymentId": {
"default" : "AWS::ApiGateway::Stage"
},
"Properties" : {
"$ref" : "AWS_ApiGateway_Stage.schema.json"
}
}
Parameters
-
AccessLogSetting
optional - AccessLogSetting -
CacheClusterEnabled
optional - Boolean -
CacheClusterSize
optional - String -
CanarySetting
optional - CanarySetting -
ClientCertificateId
optional - String -
DeploymentId
optional - String -
Description
optional - String -
DocumentationVersion
optional - String -
MethodSettings
optional - List of MethodSetting -
RestApiId
required - String -
StageName
optional - String -
Tags
optional - List of Tag -
TracingEnabled
optional - Boolean -
Variables
optional - Map
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.