AWS CDK API Gateway Construct Library (v1)

1.204.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart defines a simple AWS CDK v1 stack that creates an API Gateway REST API with a single GET endpoint. This endpoint is integrated with a Python Lambda function that returns a 'Hello from Lambda!' message. It demonstrates basic API Gateway and Lambda integration within the CDK v1 framework. Remember to run `cdk deploy` after `app.synth()` to provision resources.

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

view raw JSON →