AWS Fundamentals Logo
AWS Fundamentals
L2 Construct

SingletonFunction

A Lambda that will only ever be added to a stack once. This construct is a way to guarantee that the lambda function will be guaranteed to be part of the stack, once and only once, irrespective of how many times the construct is declared to be part of the stack. This is guaranteed as long as the `uuid` property and the optional `lambdaPurpose` property stay the same whenever they're declared into the stack.

Import

import { SingletonFunction } from 'aws-cdk-lib/aws-lambda';

Or use the module namespace:

import * as lambda from 'aws-cdk-lib/aws-lambda';
// lambda.SingletonFunction

Properties

Configuration passed to the constructor as SingletonFunctionProps.

uuidRequired
string

A unique identifier to identify this lambda. The identifier should be unique across all custom resource providers. We recommend generating a UUID per provider.

lambdaPurposeOptional
string

A descriptive name for the purpose of this Lambda. If the Lambda does not have a physical name, this string will be reflected its generated name. The combination of lambdaPurpose and uuid must be unique.

Default: SingletonLambda

3 properties inherited from FunctionProps
codeRequiredinherited from FunctionProps
Code

The source code of your Lambda function. You can point to a file in an Amazon Simple Storage Service (Amazon S3) bucket or specify your source code as inline text.

handlerRequiredinherited from FunctionProps
string

The name of the method within your code that Lambda calls to execute your function. The format includes the file name. It can also include namespaces and other qualifiers, depending on the runtime. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/foundation-progmodel.html. Use `Handler.FROM_IMAGE` when defining a function from a Docker image. NOTE: If you specify your source code as inline text by specifying the ZipFile property within the Code property, specify index.function_name as the handler.

runtimeRequiredinherited from FunctionProps
Runtime

The runtime environment for the Lambda function that you are uploading. For valid values, see the Runtime property in the AWS Lambda Developer Guide. Use `Runtime.FROM_IMAGE` when defining a function from a Docker image.

48 properties inherited from FunctionOptions
adotInstrumentationOptionalinherited from FunctionOptions
AdotInstrumentationConfig

Specify the configuration of AWS Distro for OpenTelemetry (ADOT) instrumentation.

Default: - No ADOT instrumentation

allowAllIpv6OutboundOptionalinherited from FunctionOptions
boolean

Whether to allow the Lambda to send all ipv6 network traffic. If set to true, there will only be a single egress rule which allows all outbound ipv6 traffic. If set to false, you must individually add traffic rules to allow the Lambda to connect to network targets using ipv6. Do not specify this property if the `securityGroups` or `securityGroup` property is set. Instead, configure `allowAllIpv6Outbound` directly on the security group.

Default: false

allowAllOutboundOptionalinherited from FunctionOptions
boolean

Whether to allow the Lambda to send all network traffic (except ipv6). If set to false, you must individually add traffic rules to allow the Lambda to connect to network targets. Do not specify this property if the `securityGroups` or `securityGroup` property is set. Instead, configure `allowAllOutbound` directly on the security group.

Default: true

allowPublicSubnetOptionalinherited from FunctionOptions
boolean

Lambda Functions in a public subnet can NOT access the internet. Use this property to acknowledge this limitation and still place the function in a public subnet.

Default: false

applicationLogLevelOptionalDeprecatedinherited from FunctionOptions
string

Sets the application log level for the function.

Default: "INFO"

Deprecated: Use `applicationLogLevelV2` as a property instead.

applicationLogLevelV2Optionalinherited from FunctionOptions
ApplicationLogLevel

Sets the application log level for the function.

Default: ApplicationLogLevel.INFO

architectureOptionalinherited from FunctionOptions
Architecture

The system architectures compatible with this lambda function.

Default: Architecture.X86_64

codeSigningConfigOptionalinherited from FunctionOptions
ICodeSigningConfigRef

Code signing config associated with this function.

Default: - Not Sign the Code

currentVersionOptionsOptionalinherited from FunctionOptions
VersionOptions

Options for the `lambda.Version` resource automatically created by the `fn.currentVersion` method.

Default: - default options as described in `VersionOptions`

deadLetterQueueOptionalinherited from FunctionOptions
IQueue

The SQS queue to use if DLQ is enabled. If SNS topic is desired, specify `deadLetterTopic` property instead.

Default: - SQS queue with 14 day retention period if `deadLetterQueueEnabled` is `true`

deadLetterQueueEnabledOptionalinherited from FunctionOptions
boolean

Enabled DLQ. If `deadLetterQueue` is undefined, an SQS queue with default options will be defined for your Function.

Default: - false unless `deadLetterQueue` is set, which implies DLQ is enabled.

deadLetterTopicOptionalinherited from FunctionOptions
ITopic

The SNS topic to use as a DLQ. Note that if `deadLetterQueueEnabled` is set to `true`, an SQS queue will be created rather than an SNS topic. Using an SNS topic as a DLQ requires this property to be set explicitly.

Default: - no SNS topic

descriptionOptionalinherited from FunctionOptions
string

A description of the function.

Default: - No description.

durableConfigOptionalinherited from FunctionOptions
DurableConfig

The durable configuration for the function. If durability is added to an existing function, a resource replacement will be triggered. See the 'durableConfig' section in the module README for more details.

Default: - No durable configuration

environmentOptionalinherited from FunctionOptions
{ [key: string]: string }

Key-value pairs that Lambda caches and makes available for your Lambda functions. Use environment variables to apply configuration changes, such as test and production environment configurations, without changing your Lambda function source code.

Default: - No environment variables.

environmentEncryptionOptionalinherited from FunctionOptions
IKeyRef

The AWS KMS key that's used to encrypt your function's environment variables.

Default: - AWS Lambda creates and uses an AWS managed customer master key (CMK).

ephemeralStorageSizeOptionalinherited from FunctionOptions
Size

The size of the function’s /tmp directory in MiB.

Default: 512 MiB

eventsOptionalinherited from FunctionOptions
IEventSource[]

Event sources for this function. You can also add event sources using `addEventSource`.

Default: - No event sources.

filesystemOptionalinherited from FunctionOptions
FileSystem

The filesystem configuration for the lambda function.

Default: - will not mount any filesystem

functionNameOptionalinherited from FunctionOptions
string

A name for the function.

Default: - AWS CloudFormation generates a unique physical ID and uses that ID for the function's name. For more information, see Name Type.

initialPolicyOptionalinherited from FunctionOptions
PolicyStatement[]

Initial policy statements to add to the created Lambda Role. You can call `addToRolePolicy` to the created lambda to add statements post creation.

Default: - No policy statements are added to the created Lambda role.

insightsVersionOptionalinherited from FunctionOptions
LambdaInsightsVersion

Specify the version of CloudWatch Lambda insights to use for monitoring.

Default: - No Lambda Insights

ipv6AllowedForDualStackOptionalinherited from FunctionOptions
boolean

Allows outbound IPv6 traffic on VPC functions that are connected to dual-stack subnets. Only used if 'vpc' is supplied.

Default: false

layersOptionalinherited from FunctionOptions
ILayerVersion[]

A list of layers to add to the function's execution environment. You can configure your Lambda function to pull in additional code during initialization in the form of layers. Layers are packages of libraries or other dependencies that can be used by multiple functions.

Default: - No layers.

logFormatOptionalDeprecatedinherited from FunctionOptions
string

Sets the logFormat for the function.

Default: "Text"

Deprecated: Use `loggingFormat` as a property instead.

loggingFormatOptionalinherited from FunctionOptions
LoggingFormat

Sets the loggingFormat for the function.

Default: LoggingFormat.TEXT

logGroupOptionalinherited from FunctionOptions
ILogGroupRef

The log group the function sends logs to. By default, Lambda functions send logs to an automatically created default log group named /aws/lambda/\<function name\>. However you cannot change the properties of this auto-created log group using the AWS CDK, e.g. you cannot set a different log retention. Use the `logGroup` property to create a fully customizable LogGroup ahead of time, and instruct the Lambda function to send logs to it. Providing a user-controlled log group was rolled out to commercial regions on 2023-11-16. If you are deploying to another type of region, please check regional availability first.

Default: `/aws/lambda/${this.functionName}` - default log group created by Lambda

logRemovalPolicyOptionalDeprecatedinherited from FunctionOptions
RemovalPolicy

Determine the removal policy of the log group that is auto-created by this construct. Normally you want to retain the log group so you can diagnose issues from logs even after a deployment that no longer includes the log group. In that case, use the normal date-based retention policy to age out your logs.

Default: RemovalPolicy.Retain

Deprecated: use `logGroup` instead

logRetentionOptionalDeprecatedinherited from FunctionOptions
RetentionDays

The number of days log events are kept in CloudWatch Logs. When updating this property, unsetting it doesn't remove the log retention policy. To remove the retention policy, set the value to `INFINITE`. This is a legacy API and we strongly recommend you move away from it if you can. Instead create a fully customizable log group with `logs.LogGroup` and use the `logGroup` property to instruct the Lambda function to send logs to it. Migrating from `logRetention` to `logGroup` will cause the name of the log group to change. Users and code and referencing the name verbatim will have to adjust. In AWS CDK code, you can access the log group name directly from the LogGroup construct: ```ts import * as logs from 'aws-cdk-lib/aws-logs'; declare const myLogGroup: logs.LogGroup; myLogGroup.logGroupName; ```

Default: logs.RetentionDays.INFINITE

Deprecated: use `logGroup` instead

logRetentionRetryOptionsOptionalinherited from FunctionOptions
LogRetentionRetryOptions

When log retention is specified, a custom resource attempts to create the CloudWatch log group. These options control the retry policy when interacting with CloudWatch APIs. This is a legacy API and we strongly recommend you migrate to `logGroup` if you can. `logGroup` allows you to create a fully customizable log group and instruct the Lambda function to send logs to it.

Default: - Default AWS SDK retry options.

logRetentionRoleOptionalinherited from FunctionOptions
IRole

The IAM role for the Lambda function associated with the custom resource that sets the retention policy. This is a legacy API and we strongly recommend you migrate to `logGroup` if you can. `logGroup` allows you to create a fully customizable log group and instruct the Lambda function to send logs to it.

Default: - A new role is created.

memorySizeOptionalinherited from FunctionOptions
number

The amount of memory, in MB, that is allocated to your Lambda function. Lambda uses this value to proportionally allocate the amount of CPU power. For more information, see Resource Model in the AWS Lambda Developer Guide.

Default: 128

paramsAndSecretsOptionalinherited from FunctionOptions
ParamsAndSecretsLayerVersion

Specify the configuration of Parameters and Secrets Extension.

Default: - No Parameters and Secrets Extension

profilingOptionalinherited from FunctionOptions
boolean

Enable profiling.

Default: - No profiling.

profilingGroupOptionalinherited from FunctionOptions
IProfilingGroup

Profiling Group.

Default: - A new profiling group will be created if `profiling` is set.

recursiveLoopOptionalinherited from FunctionOptions
RecursiveLoop

Sets the Recursive Loop Protection for Lambda Function. It lets Lambda detect and terminate unintended recursive loops.

Default: RecursiveLoop.Terminate

reservedConcurrentExecutionsOptionalinherited from FunctionOptions
number

The maximum of concurrent executions you want to reserve for the function.

Default: - No specific limit - account limit.

roleOptionalinherited from FunctionOptions
IRole

Lambda execution role. This is the role that will be assumed by the function upon execution. It controls the permissions that the function will have. The Role must be assumable by the 'lambda.amazonaws.com' service principal. The default Role automatically has permissions granted for Lambda execution. If you provide a Role, you must add the relevant AWS managed policies yourself. The relevant managed policies are "service-role/AWSLambdaBasicExecutionRole" and "service-role/AWSLambdaVPCAccessExecutionRole".

Default: - A unique role will be generated for this lambda function. Both supplied and generated roles can always be changed by calling `addToRolePolicy`.

runtimeManagementModeOptionalinherited from FunctionOptions
RuntimeManagementMode

Sets the runtime management configuration for a function's version.

Default: Auto

securityGroupsOptionalinherited from FunctionOptions
ISecurityGroup[]

The list of security groups to associate with the Lambda's network interfaces. Only used if 'vpc' is supplied.

Default: - If the function is placed within a VPC and a security group is not specified, either by this or securityGroup prop, a dedicated security group will be created for this function.

snapStartOptionalinherited from FunctionOptions
SnapStartConf

Enable SnapStart for Lambda Function. SnapStart is currently supported for Java 11, Java 17, Python 3.12, Python 3.13, and .NET 8 runtime

Default: - No snapstart

systemLogLevelOptionalDeprecatedinherited from FunctionOptions
string

Sets the system log level for the function.

Default: "INFO"

Deprecated: Use `systemLogLevelV2` as a property instead.

systemLogLevelV2Optionalinherited from FunctionOptions
SystemLogLevel

Sets the system log level for the function.

Default: SystemLogLevel.INFO

tenancyConfigOptionalinherited from FunctionOptions
TenancyConfig

The tenancy configuration for the function.

Default: - Tenant isolation is not enabled

timeoutOptionalinherited from FunctionOptions
Duration

The function execution time (in seconds) after which Lambda terminates the function. Because the execution time affects cost, set this value based on the function's expected execution time.

Default: Duration.seconds(3)

tracingOptionalinherited from FunctionOptions
Tracing

Enable AWS X-Ray Tracing for Lambda Function.

Default: Tracing.Disabled

vpcOptionalinherited from FunctionOptions
IVpc

VPC network to place Lambda network interfaces. Specify this if the Lambda function needs to access resources in a VPC. This is required when `vpcSubnets` is specified.

Default: - Function is not placed within a VPC.

vpcSubnetsOptionalinherited from FunctionOptions
SubnetSelection

Where to place the network interfaces within the VPC. This requires `vpc` to be specified in order for interfaces to actually be placed in the subnets. If `vpc` is not specify, this will raise an error. Note: Internet access for Lambda Functions requires a NAT Gateway, so picking public subnets is not allowed (unless `allowPublicSubnet` is set to `true`).

Default: - the Vpc default strategy if not specified

4 properties inherited from EventInvokeConfigOptions
maxEventAgeOptionalinherited from EventInvokeConfigOptions
Duration

The maximum age of a request that Lambda sends to a function for processing. Minimum: 60 seconds Maximum: 6 hours

Default: Duration.hours(6)

onFailureOptionalinherited from EventInvokeConfigOptions
IDestination

The destination for failed invocations.

Default: - no destination

onSuccessOptionalinherited from EventInvokeConfigOptions
IDestination

The destination for successful invocations.

Default: - no destination

retryAttemptsOptionalinherited from EventInvokeConfigOptions
number

The maximum number of times to retry when the function returns an error. Minimum: 0 Maximum: 2

Default: 2

Get the AWS Lambda Cheat Sheet

Everything you need to know about AWS Lambda on one page. HD quality, print-friendly.

Download Free Infographic