AWS SAM Function construct for creating Lambda functions with serverless-specific features including automatic event source mapping, IAM policies, and deployment configurations.
Creates a Lambda function with SAM enhancements.
/**
* AWS::Serverless::Function - Lambda function with SAM enhancements
*/
class CfnFunction extends cdk.CfnResource {
constructor(
scope: cdk.Construct,
id: string,
props: CfnFunctionProps
);
}
interface CfnFunctionProps {
/** Function code location */
codeUri?: any;
/** Runtime environment for the function */
runtime?: string;
/** Entry point handler for the function */
handler?: string;
/** Function description */
description?: string;
/** Memory allocation in MB (128-10240) */
memorySize?: number;
/** Function timeout in seconds (1-900) */
timeout?: number;
/** IAM role ARN for the function */
role?: string;
/** Managed policy ARNs or policy documents */
policies?: any;
/** Environment variables */
environment?: CfnFunction.FunctionEnvironmentProperty;
/** Event sources that trigger the function */
events?: { [key: string]: any };
/** VPC configuration */
vpcConfig?: CfnFunction.VpcConfigProperty;
/** Dead letter queue configuration */
deadLetterQueue?: CfnFunction.DeadLetterQueueProperty;
/** Deployment preferences for gradual deployments */
deploymentPreference?: CfnFunction.DeploymentPreferenceProperty;
/** Lambda layers */
layers?: string[];
/** X-Ray tracing configuration */
tracing?: string;
/** Function name override */
functionName?: string;
/** IAM permissions boundary */
permissionsBoundary?: string;
/** Reserved concurrency limit */
reservedConcurrencyLimit?: number;
/** Provisioned concurrency configuration */
provisionedConcurrencyConfig?: CfnFunction.ProvisionedConcurrencyConfigProperty;
/** IAM assume role policy document */
assumeRolePolicyDocument?: any;
/** Event invoke configuration */
eventInvokeConfig?: CfnFunction.EventInvokeConfigProperty;
/** File system configurations */
fileSystemConfigs?: CfnFunction.FileSystemConfigProperty[];
/** Code signing configuration ARN */
codeSigningConfigArn?: string;
/** Processor architectures */
architectures?: string[];
/** Ephemeral storage configuration */
ephemeralStorage?: CfnFunction.EphemeralStorageProperty;
/** SnapStart configuration */
snapStart?: CfnFunction.SnapStartProperty;
/** Runtime management configuration */
runtimeManagementConfig?: CfnFunction.RuntimeManagementConfigProperty;
/** Logging configuration */
loggingConfig?: CfnFunction.LoggingConfigProperty;
}Usage Examples:
import * as cdk from '@aws-cdk/core';
import * as sam from '@aws-cdk/aws-sam';
const stack = new cdk.Stack();
// Basic function
new sam.CfnFunction(stack, 'BasicFunction', {
codeUri: './src',
runtime: 'nodejs14.x',
handler: 'index.handler',
});
// Function with API event
new sam.CfnFunction(stack, 'ApiFunction', {
codeUri: {
bucket: 'my-bucket',
key: 'function.zip',
},
runtime: 'python3.9',
handler: 'app.lambda_handler',
events: {
HelloWorld: {
type: 'Api',
properties: {
path: '/hello',
method: 'get',
},
},
},
});
// Function with policies and environment
new sam.CfnFunction(stack, 'EnhancedFunction', {
codeUri: './dist',
runtime: 'nodejs14.x',
handler: 'index.handler',
policies: ['AWSLambdaExecute'],
environment: {
variables: {
TABLE_NAME: 'MyTable',
REGION: 'us-east-1',
},
},
timeout: 30,
memorySize: 512,
});interface FunctionEnvironmentProperty {
/** Environment variables */
variables?: { [key: string]: string };
}interface VpcConfigProperty {
/** Security group IDs */
securityGroupIds?: string[];
/** Subnet IDs */
subnetIds?: string[];
}interface DeadLetterQueueProperty {
/** Target ARN for failed invocations */
targetArn?: string;
/** Type of dead letter queue (SNS or SQS) */
type?: string;
}interface DeploymentPreferenceProperty {
/** Enable deployment preferences */
enabled?: boolean;
/** Deployment type (Canary10Percent30Minutes, Linear10PercentEvery10Minutes, etc.) */
type?: string;
/** CloudWatch alarms for monitoring */
alarms?: string[];
/** Pre-traffic and post-traffic hooks */
hooks?: DeploymentPreferenceHooksProperty;
/** Rollback trigger configuration */
triggerConfigurations?: TriggerConfigurationProperty[];
}
interface DeploymentPreferenceHooksProperty {
/** Pre-traffic hook function ARN */
preTraffic?: string;
/** Post-traffic hook function ARN */
postTraffic?: string;
}
interface TriggerConfigurationProperty {
/** Trigger events */
triggerEvents?: string[];
/** Trigger name */
triggerName?: string;
/** Trigger target ARN */
triggerTargetArn?: string;
}interface ProvisionedConcurrencyConfigProperty {
/** Provisioned concurrency capacity */
provisionedConcurrencyCapacity?: number;
}interface EventInvokeConfigProperty {
/** Destination configuration */
destinationConfig?: DestinationConfigProperty;
/** Maximum event age in seconds */
maximumEventAgeInSeconds?: number;
/** Maximum retry attempts */
maximumRetryAttempts?: number;
}
interface DestinationConfigProperty {
/** On failure destination */
onFailure?: DestinationProperty;
/** On success destination */
onSuccess?: DestinationProperty;
}
interface DestinationProperty {
/** Destination ARN */
destination: string;
/** Destination type */
type?: string;
}interface FileSystemConfigProperty {
/** EFS access point ARN */
arn?: string;
/** Local mount path */
localMountPath?: string;
}interface EphemeralStorageProperty {
/** Size in MB (512-10240) */
size?: number;
}interface SnapStartProperty {
/** Apply SnapStart optimization */
applyOn?: string;
}interface RuntimeManagementConfigProperty {
/** Runtime update mode */
updateRuntimeOn?: string;
/** Runtime version ARN */
runtimeVersionArn?: string;
}interface LoggingConfigProperty {
/** Application log level */
applicationLogLevel?: string;
/** Log format */
logFormat?: string;
/** Log group name */
logGroup?: string;
/** System log level */
systemLogLevel?: string;
}interface ApiEventProperty {
/** API path */
path?: string;
/** HTTP method */
method?: string;
/** API Gateway REST API reference */
restApiId?: string;
/** Request parameters */
requestParameters?: any;
}interface ScheduleEventProperty {
/** Schedule expression (rate or cron) */
schedule?: string;
/** Event name */
name?: string;
/** Event description */
description?: string;
/** Input data for the event */
input?: string;
/** Enable/disable the event */
enabled?: boolean;
}interface S3EventProperty {
/** S3 bucket name */
bucket?: string;
/** S3 event types */
events?: string[];
/** Object key filter */
filter?: S3NotificationFilterProperty;
}
interface S3NotificationFilterProperty {
/** S3 key filter */
s3Key?: S3KeyFilterProperty;
}
interface S3KeyFilterProperty {
/** Filter rules */
rules?: S3KeyFilterRuleProperty[];
}
interface S3KeyFilterRuleProperty {
/** Rule name (prefix or suffix) */
name?: string;
/** Rule value */
value?: string;
}interface DynamoDBEventProperty {
/** DynamoDB stream ARN */
stream?: string;
/** Starting position (TRIM_HORIZON or LATEST) */
startingPosition?: string;
/** Batch size */
batchSize?: number;
/** Maximum batching window in seconds */
maximumBatchingWindowInSeconds?: number;
/** Event source mapping enabled */
enabled?: boolean;
}interface KinesisEventProperty {
/** Kinesis stream ARN */
stream?: string;
/** Starting position */
startingPosition?: string;
/** Batch size */
batchSize?: number;
/** Maximum batching window in seconds */
maximumBatchingWindowInSeconds?: number;
/** Parallelization factor */
parallelizationFactor?: number;
}interface SqsEventProperty {
/** SQS queue ARN */
queue?: string;
/** Batch size */
batchSize?: number;
/** Maximum batching window in seconds */
maximumBatchingWindowInSeconds?: number;
/** Event source mapping enabled */
enabled?: boolean;
}interface SnsEventProperty {
/** SNS topic ARN */
topic?: string;
/** Subscription region */
region?: string;
/** Subscription filter policy */
filterPolicy?: any;
}interface CloudWatchEventProperty {
/** Event pattern */
pattern?: any;
/** Input data for the event */
input?: string;
/** Input path for transforming event data */
inputPath?: string;
}interface CloudWatchLogsEventProperty {
/** Log group name */
logGroup?: string;
/** Filter name */
filterName?: string;
/** Filter pattern */
filterPattern?: string;
}interface EventBridgeRuleEventProperty {
/** Event pattern */
pattern?: any;
/** Event bus name */
eventBusName?: string;
/** Input data for the event */
input?: string;
/** Input path */
inputPath?: string;
/** Input transformer */
inputTransformer?: EventBridgeRuleEventInputTransformerProperty;
}
interface EventBridgeRuleEventInputTransformerProperty {
/** Input paths map */
inputPathsMap?: { [key: string]: string };
/** Input template */
inputTemplate?: string;
}