Locally testing AWS CDK applications - AWS Serverless Application Model

Locally testing AWS CDK applications

You can use the AWS SAM CLI to locally test your AWS CDK applications by running the following commands from the project root directory of your AWS CDK application:

Before you run any of the sam local commands with a AWS CDK application, you must run cdk synth.

When running sam local invoke you need the function construct identifier that you want to invoke, and the path to your synthesized AWS CloudFormation template. If your application uses nested stacks, to resolve naming conflicts, you also need the stack name where the function is defined.

Usage:

# Invoke the function FUNCTION_IDENTIFIER declared in the stack STACK_NAME sam local invoke [OPTIONS] [STACK_NAME/FUNCTION_IDENTIFIER] # Start all APIs declared in the AWS CDK application sam local start-api -t ./cdk.out/CdkSamExampleStack.template.json [OPTIONS] # Start a local endpoint that emulates AWS Lambda sam local start-lambda -t ./cdk.out/CdkSamExampleStack.template.json [OPTIONS]

Example

Consider stacks and functions that are declared with the following sample:

app = new HelloCdkStack(app, "HelloCdkStack", ... ) class HelloCdkStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { ... new lambda.Function(this, 'MyFunction', { ... }); new HelloCdkNestedStack(this, 'HelloNestedStack' ,{ ... }); } class HelloCdkNestedStack extends cdk.NestedStack { constructor(scope: Construct, id: string, props?: cdk.NestedStackProps) { ... new lambda.Function(this, 'MyFunction', { ... }); new lambda.Function(this, 'MyNestedFunction', { ... }); }

The following commands locally invokes the Lambda functions defined in example presented above:

# Invoke MyFunction from the HelloCdkStack sam local invoke -t ./cdk.out/HelloCdkStack.template.json MyFunction
# Invoke MyNestedFunction from the HelloCdkNestedStack sam local invoke -t ./cdk.out/HelloCdkStack.template.json MyNestedFunction
# Invoke MyFunction from the HelloCdkNestedStack sam local invoke -t ./cdk.out/HelloCdkStack.template.json HelloNestedStack/MyFunction