AWS CDK API Gateway Construct Library (v1)
The AWS Cloud Development Kit (CDK) Construct Library for AWS::ApiGateway provides high-level constructs to define API Gateway resources programmatically. This entry focuses on version 1.204.0, part of the CDK v1 series, which receives regular updates, but has a successor in CDK v2 with different packaging.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_apigateway'
cause The `aws-cdk.aws-apigateway` library is not installed or the Python environment is incorrect.fixEnsure the correct CDK v1 package is installed: `pip install aws-cdk.aws-apigateway==1.204.0 aws-cdk.core==1.204.0`. If using CDK v2, the import path and package name are different. -
A Construct with scope 'MyApiGatewayStack' and id 'MySimpleApi' already exists.
cause You are attempting to create two Constructs with the same ID within the same parent scope (e.g., the same Stack).fixEnsure all Construct IDs (`id` argument in `Construct(scope, id, ...)`) are unique within their immediate parent's scope. For example, change one of the `apigw.RestApi(self, 'MySimpleApi', ...)` to `apigw.RestApi(self, 'MyOtherApi', ...)`. -
Error: Stack 'MyApiGatewayStack' failed to deploy: User: arn:aws:iam::xxxxxxxxxxxx:user/YourUser is not authorized to perform: apigateway:CREATE_REST_API on resource: arn:aws:apigateway:...
cause The AWS IAM user or role used for deploying the CDK stack does not have sufficient permissions to create or manage API Gateway resources.fixGrant the necessary IAM permissions to the deploying identity. For full CDK deployment, a policy like `arn:aws:iam::aws:policy/AdministratorAccess` or a more granular custom policy including `apigateway:*`, `lambda:*`, `iam:*`, etc., is required. -
TypeError: Expected scope to be an instance of Construct, got <class 'aws_cdk.core.App'>
cause You are attempting to instantiate a Construct (e.g., `apigw.RestApi`) directly under `core.App` instead of within a `core.Stack` or another `core.Construct`.fixConstructs like `apigw.RestApi` must be scoped to a `core.Stack` or another Composite Construct. For example, use `apigw.RestApi(self, "MyApi")` inside a `core.Stack` class, where `self` refers to the stack instance.
Warnings
- breaking This library (`aws-cdk.aws-apigateway`) is part of AWS CDK v1. AWS CDK v2 is a major rewrite with different package structures, module names (e.g., `aws_cdk.aws_apigateway` is now `aws_cdk_lib.aws_apigateway`), and updated constructs. Migrating from v1 to v2 requires significant code changes.
- gotcha All Constructs within a given scope (e.g., a Stack or another Construct) must have a unique ID. Reusing IDs will lead to synthesis or deployment errors.
- gotcha API Gateway requires explicit deployment and stage creation to make the API accessible. While `deploy=True` on `apigw.RestApi` creates a default deployment and stage, complex scenarios with multiple stages, custom stage variables, or external deployment triggers require manual `apigw.Deployment` and `apigw.Stage` constructs.
- gotcha When integrating API Gateway with other AWS services (e.g., Lambda, SQS, Kinesis), ensure that the API Gateway execution role or the integrated resource itself has the necessary IAM permissions. While Lambda integrations often handle this automatically, other integrations might require manual IAM policy attachments.
Install
-
pip install aws-cdk.aws-apigateway==1.204.0 aws-cdk.core==1.204.0
Imports
- aws_apigateway
from aws_cdk.aws_apigateway import RestApi
from aws_cdk import aws_apigateway as apigw
- RestApi
RestApi(...)
apigw.RestApi(...)
Quickstart
import os
from aws_cdk import (
core,
aws_apigateway as apigw,
aws_lambda as lambda_,
)
class MyApiStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Define a Lambda function to integrate with API Gateway
hello_function = lambda_.Function(
self, "HelloFunction",
runtime=lambda_.Runtime.PYTHON_3_9,
handler="index.handler",
code=lambda_.Code.from_inline("def handler(event, context): return {'statusCode': 200, 'body': 'Hello from Lambda!'}")
)
# Create a REST API
api = apigw.RestApi(
self, "MySimpleApi",
rest_api_name="MySimpleApi",
description="A simple API Gateway example backed by Lambda.",
deploy=True, # Automatically creates a default deployment and stage
deploy_options=apigw.StageOptions(
stage_name="dev"
)
)
# Add a GET method to the root path, integrating with the Lambda function
api.root.add_method(
"GET",
apigw.LambdaIntegration(hello_function)
)
app = core.App()
MyApiStack(app, "MyApiGatewayStack")
app.synth()