Skill 119 · Creating API Gateway Stage
Subchapter 119.1
references/create-api-gateway-stage.mdMarkdown14 KBView on GitHub
This SOP creates an API Gateway stage with comprehensive logging configuration, stage variables, and necessary IAM roles. It follows AWS best practices for API Gateway stage setup including CloudWatch logging, X-Ray tracing, and proper access logging.
Check for required tools and warn the user if any are missing.
Constraints:
call_awsVerify that the specified REST API and deployment exist.
Constraints:
call_aws to describe the REST API using: aws apigateway get-rest-api --rest-api-id {rest_api_id}call_aws to verify the deployment exists using: aws apigateway get-deployment --rest-api-id {rest_api_id} --deployment-id {deployment_id}Verify if API Gateway has the necessary IAM role for CloudWatch logging.
Constraints:
call_aws to check the account settings: aws apigateway get-accountcloudwatchRoleArn field in the responseCreate the IAM role for API Gateway CloudWatch logging if it doesn’t exist.
Constraints:
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"apigateway.amazonaws.com"},"Action":"sts:AssumeRole"}]}call_aws with: aws iam create-role --role-name APIGatewayCloudWatchLogsRole --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"apigateway.amazonaws.com"},"Action":"sts:AssumeRole"}]}'aws iam attach-role-policy --role-name APIGatewayCloudWatchLogsRole --policy-arn arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogsConfigure the API Gateway account to use the CloudWatch logs role.
Constraints:
call_aws to update account settings: aws apigateway update-account --patch-operationsCreate a dedicated log group for the API Gateway stage.
Constraints:
/aws/apigateway/{rest_api_id}/{stage_name}call_aws with: aws logs create-log-group --log-group-nameSet the retention policy for the CloudWatch log group to manage log lifecycle.
Constraints:
aws logs put-retention-policy --log-group-name --retention-in-days 14Create the API Gateway stage with data tracing configuration.
Constraints:
call_aws to create the stage with all configurations: aws apigateway create-stage --rest-api-id {rest_api_id} --stage-name {stage_name} --deployment-id {deployment_id} --description {stage_description} --variables {environment-variables} --tracing-enabled--data-trace-enabled {data_trace_enabled}Update the API Gateway stage with comprehensive configuration.
Constraints:
call_aws to configure API Gateway Stage aws apigateway update-stage --rest-api-id {api_gateway_id} --stage-name {stage_name} --patch-operations op=replace,path=/*/*/throttling/rateLimit,value={throttling_rate_limit} op=replace,path=/*/*/throttling/burstLimit,value={throttling_burst_limit}Set up method-level access and execution logging configuration for better granularity.
Constraints:
call_aws to update method settings for access logging using aws apigateway update-stage --rest-api-id {api_gateway_id} --stage-name {stage_name} --patch-operations '[{"op":"replace","path":"/accessLogSettings/destinationArn","value":{log_group_arn}},{"op":"replace","path":"/accessLogSettings/format","value":"$context.requestId $context.ip $context.caller $context.user [$context.requestTime] \"$context.httpMethod $context.resourcePath $context.protocol\" $context.status $context.error.message $context.responseLength $context.requestTime $context.xrayTraceId"}]'call_aws to update method settings for execution logging using aws apigateway update-stage --rest-api-id {api_gateway_id} --stage-name {stage_name} --patch-operations '[{"op":"replace","path":"/*/*/logging/loglevel","value":"INFO"},{"op":"replace","path":"/*/*/metrics/enabled","value":"true"}]'Create a WAF web ACL with basic security rules and associate it with the API Gateway stage for defense in depth.
Constraints:
aws wafv2 create-web-acl --name {api_name}-{stage_name}-waf --scope REGIONAL --default-action Allow={} --rulesaws wafv2 create-web-acl with rate-based ruleaws wafv2 associate-web-acl --web-acl-arn {web_acl_arn} --resource-arn arn:aws:apigateway:{region}::/restapis/{rest_api_id}/stages/{stage_name}Ask the user about authorization requirements and provide configuration guidance.
Constraints:
Validate that the stage has been created successfully with proper configuration.
Constraints:
call_aws to get stage details: aws apigateway get-stage --rest-api-id {rest_api_id} --stage-name {stage_name}aws wafv2 get-web-acl-for-resource --resource-arn arn:aws:apigateway:{region}::/restapis/{rest_api_id}/stages/{stage_name}Provide comprehensive examples for testing of new API Gateway stage.
Constraints:
# Test the API Gateway stage
aws apigateway test-invoke-method --rest-api-id {rest_api_id} --stage-name {stage_name} --method GET --path-with-query-string /
# Get stage information
aws apigateway get-stage --rest-api-id {rest_api_id} --stage-name {stage_name}
# View CloudWatch logs
aws logs describe-log-streams --log-group-name /aws/apigateway/{rest_api_id}/{stage_name}
# Get stage metrics
aws cloudwatch get-metric-statistics --namespace AWS/ApiGateway --metric-name Count --dimensions Name=ApiName,Value={api_name} Name=Stage,Value={stage_name} --start-time 2024-01-01T00:00:00Z --end-time 2024-01-01T23:59:59Z --period 3600 --statistics Sum
# Check WAF web ACL association (if WAF is enabled)
aws wafv2 get-web-acl-for-resource --resource-arn arn:aws:apigateway:{region}::/restapis/{rest_api_id}/stages/{stage_name}
# Monitor WAF metrics
aws cloudwatch get-metric-statistics --namespace AWS/WAFV2 --metric-name AllowedRequests --dimensions Name=WebACL,Value={web_acl_name} Name=Region,Value={region} --start-time 2024-01-01T00:00:00Z --end-time 2024-01-01T23:59:59Z --period 3600 --statistics SumIf logs are not appearing in CloudWatch:
If stage creation fails:
If experiencing performance issues:
If WAF is blocking legitimate requests:
If experiencing authorization problems: