A construct that creates an API Gateway HTTP API and integrates it with an existing AWS Lambda function.

The Endpoint construct simplifies the process of exposing a Lambda function as an HTTP API endpoint. It allows you to configure the API Gateway with a custom name, path, and HTTP method.

import { Stack, Duration, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { lambda } from '@reuters-graphics/structs';
import * as apigateway from 'aws-cdk-lib/aws-apigatewayv2';

export class MyLambdaStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);

// Create a Lambda function
const myLambda = new lambda.NodeJsLambda(this, 'my-lambda', {
entry: './src/index.ts',
handler: 'handle',
});

// Create an API Gateway endpoint for the Lambda function
const myApiEndpoint = new lambda.api.Endpoint(this, 'my-api', {
lambdaFunction: myLambda.lambda,
apiPath: '/my-endpoint',
apiMethod: apigateway.HttpMethod.GET,
});

// Access new API's URL and add it to CDK outputs
new CfnOutput(this, 'ApiUrl', {
value: myApiEndpoint.api.url!,
});

// Create another Lambda function
const myOtherLambda = new lambda.NodeJsLambda(/* ... */);

// Add a new lambda integration as another route to the endpoint
myApiEndpoint.addLambdaRoute({
lambdaFunction: myOtherLambda.lambda,
apiPath: '/my-other-endpoint',
apiMethod: apigateway.HttpMethod.GET,
});
}
}

Hierarchy

  • Construct
    • Endpoint

Constructors

Properties

Methods

Constructors

  • Creates a new APIEndpoint construct.

    Parameters

    • scope: Construct

      The scope in which this construct is defined.

    • id: string

      The scoped construct ID.

    • props: EndpointOptions

      Configuration options for the API Gateway and Lambda function integration.

    Returns Endpoint

Properties

api: HttpApi

The underlying API Gateway HTTP API created by this construct.

node: Node

The tree node.

Methods

  • Checks if x is a construct.

    Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

    Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

    Parameters

    • x: any

      Any object

    Returns x is Construct

    true if x is an object created from a class which extends Construct.

  • Add another route to this API integrated with a Lambda function.

    Parameters

    Returns void

  • Returns a string representation of this construct.

    Returns string