The AWS CDK construct library for AWS Serverless Application Model (SAM) resources provides L1 (CloudFormation level) constructs for deploying serverless applications using SAM syntax within CDK stacks. This library automatically generates TypeScript constructs from CloudFormation AWS::Serverless resource specifications, enabling developers to define serverless functions, APIs, and applications using familiar SAM patterns.
npm install @aws-cdk/aws-samimport * as sam from '@aws-cdk/aws-sam';For specific constructs:
import { CfnFunction, CfnApi, CfnApplication } from '@aws-cdk/aws-sam';import * as cdk from '@aws-cdk/core';
import * as sam from '@aws-cdk/aws-sam';
const stack = new cdk.Stack();
// Create a serverless function
new sam.CfnFunction(stack, 'MyFunction', {
codeUri: {
bucket: 'my-deployment-bucket',
key: 'function.zip',
},
runtime: 'nodejs14.x',
handler: 'index.handler',
events: {
Api: {
type: 'Api',
properties: {
path: '/hello',
method: 'get',
},
},
},
});This library follows AWS CDK's standard L1 construct pattern:
Core Lambda function construct with SAM enhancements including automatic event source mapping, IAM role creation, and deployment preferences.
class CfnFunction extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnFunctionProps
);
}
interface CfnFunctionProps {
codeUri?: any;
runtime?: string;
handler?: string;
events?: { [key: string]: any };
policies?: any;
environment?: any;
// ... additional properties
}REST and HTTP API Gateway constructs with SAM-specific configuration for CORS, authentication, and custom domains.
class CfnApi extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnApiProps
);
}
class CfnHttpApi extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnHttpApiProps
);
}Deploy pre-built serverless applications from the AWS Serverless Application Repository.
class CfnApplication extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnApplicationProps
);
}
interface CfnApplicationProps {
location: any;
parameters?: { [key: string]: string };
tags?: { [key: string]: string };
}DynamoDB table and Step Functions state machine constructs for serverless data storage and workflow orchestration.
class CfnSimpleTable extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnSimpleTableProps
);
}
class CfnStateMachine extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnStateMachineProps
);
}Lambda layer version construct for sharing code and dependencies across functions.
class CfnLayerVersion extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnLayerVersionProps
);
}
interface CfnLayerVersionProps {
contentUri?: any;
compatibleRuntimes?: string[];
description?: string;
}// Base CDK construct and properties
import * as cdk from '@aws-cdk/core';
// Common property patterns used across constructs
interface TagsProperty {
[key: string]: string;
}
interface ParametersProperty {
[key: string]: string;
}
// Location specification for applications and code
interface LocationProperty {
applicationId?: string;
semanticVersion?: string;
bucket?: string;
key?: string;
version?: string;
}