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
-
arnoptional computed - string -
cache_cluster_enabledoptional - bool -
cache_cluster_sizeoptional - string -
client_certificate_idoptional - string -
deployment_idrequired - string -
descriptionoptional - string -
documentation_versionoptional - string -
execution_arnoptional computed - string -
idoptional computed - string -
invoke_urloptional computed - string -
rest_api_idrequired - string -
stage_namerequired - string -
tagsoptional - map from string to string -
variablesoptional - map from string to string -
xray_tracing_enabledoptional - bool -
access_log_settingslist block-
destination_arnrequired - string -
formatrequired - 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_deploymentresource. Stages can be optionally managed further with theaws_api_gateway_base_path_mappingresource,aws_api_gateway_domain_nameresource, andaws_api_method_settingsresource. 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
-
AccessLogSettingoptional - AccessLogSetting -
CacheClusterEnabledoptional - Boolean -
CacheClusterSizeoptional - String -
CanarySettingoptional - CanarySetting -
ClientCertificateIdoptional - String -
DeploymentIdoptional - String -
Descriptionoptional - String -
DocumentationVersionoptional - String -
MethodSettingsoptional - List of MethodSetting -
RestApiIdrequired - String -
StageNameoptional - String -
Tagsoptional - List of Tag -
TracingEnabledoptional - Boolean -
Variablesoptional - Map
Explanation in CloudFormation Registry
The
AWS::ApiGateway::Stageresource 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.