Lambda function with a function URL

Create Lambda function with a URL

AWS Lambda
import { CfnOutput, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Function, Runtime, Code, FunctionUrlAuthType } from "aws-cdk-lib/aws-lambda";

export class CdkStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
      super(scope, id, props);
  
      // lambda function
      const myFunction = new Function(this, "myFunction", {
        runtime: Runtime.NODEJS_14_X,
        code: Code.fromAsset("functions"),
        handler: "handler.app"
      });
  
      const myFunctionUrl = myFunction.addFunctionUrl({
        authType: FunctionUrlAuthType.NONE,
        cors: {
          allowedOrigins: ['*'],
        }
      });
  
      new CfnOutput(this, 'FunctionUrl', {
        value: myFunctionUrl.url,
      });
    }
  }

Download

git clone https://github.com/aws-samples/serverless-patterns
cd serverless-patterns/lambda-function-url-cdk

Pattern repository

View on GitHub

Last updated on 26 Dec 2024

Edit this page