Locally testing Amazon CDK applications - Amazon Serverless Application Model
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Locally testing Amazon CDK applications

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

Before you run any of the sam local commands with a Amazon 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 Amazon 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 Amazon CDK application sam local start-api -t ./cdk.out/CdkSamExampleStack.template.json [OPTIONS] # Start a local endpoint that emulates Amazon 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