AWS CloudFormation
User Guide (Version )

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

AWS::Lambda::Function

The AWS::Lambda::Function resource creates a Lambda function. To create a function, you need a deployment package and an execution role. The deployment package contains your function code. The execution role grants the function permission to use AWS services, such as Amazon CloudWatch Logs for log streaming and AWS X-Ray for request tracing.

Syntax

To declare this entity in your AWS CloudFormation template, use the following syntax:

JSON

{ "Type" : "AWS::Lambda::Function", "Properties" : { "Code" : Code, "DeadLetterConfig" : DeadLetterConfig, "Description" : String, "Environment" : Environment, "FunctionName" : String, "Handler" : String, "KmsKeyArn" : String, "Layers" : [ String, ... ], "MemorySize" : Integer, "ReservedConcurrentExecutions" : Integer, "Role" : String, "Runtime" : String, "Tags" : [ Tag, ... ], "Timeout" : Integer, "TracingConfig" : TracingConfig, "VpcConfig" : VpcConfig } }

YAML

Type: AWS::Lambda::Function Properties: Code: Code DeadLetterConfig: DeadLetterConfig Description: String Environment: Environment FunctionName: String Handler: String KmsKeyArn: String Layers: - String MemorySize: Integer ReservedConcurrentExecutions: Integer Role: String Runtime: String Tags: - Tag Timeout: Integer TracingConfig: TracingConfig VpcConfig: VpcConfig

Properties

Code

The code for the function.

Required: Yes

Type: Code

Update requires: No interruption

DeadLetterConfig

A dead letter queue configuration that specifies the queue or topic where Lambda sends asynchronous events when they fail processing. For more information, see Dead Letter Queues.

Required: No

Type: DeadLetterConfig

Update requires: No interruption

Description

A description of the function.

Required: No

Type: String

Minimum: 0

Maximum: 256

Update requires: No interruption

Environment

Environment variables that are accessible from function code during execution.

Required: No

Type: Environment

Update requires: No interruption

FunctionName

The name of the Lambda function, up to 64 characters in length. If you don't specify a name, AWS CloudFormation generates one.

If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.

Required: No

Type: String

Minimum: 1

Maximum: 140

Pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}(-gov)?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-_]+)(:(\$LATEST|[a-zA-Z0-9-_]+))?

Update requires: Replacement

Handler

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 Programming Model.

Required: Yes

Type: String

Maximum: 128

Pattern: [^\s]+

Update requires: No interruption

KmsKeyArn

The ARN of the AWS Key Management Service (AWS KMS) key that's used to encrypt your function's environment variables. If it's not provided, AWS Lambda uses a default service key.

Required: No

Type: String

Pattern: (arn:(aws[a-zA-Z-]*)?:[a-z0-9-.]+:.*)|()

Update requires: No interruption

Layers

A list of function layers to add to the function's execution environment. Specify each layer by its ARN, including the version.

Required: No

Type: List of String

Update requires: No interruption

MemorySize

The amount of memory that your function has access to. Increasing the function's memory also increases its CPU allocation. The default value is 128 MB. The value must be a multiple of 64 MB.

Required: No

Type: Integer

Minimum: 128

Maximum: 3008

Update requires: No interruption

ReservedConcurrentExecutions

The number of simultaneous executions to reserve for the function.

Required: No

Type: Integer

Minimum: 0

Update requires: No interruption

Role

The Amazon Resource Name (ARN) of the function's execution role.

Required: Yes

Type: String

Pattern: arn:(aws[a-zA-Z-]*)?:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+

Update requires: No interruption

Runtime

The identifier of the function's runtime.

Required: Yes

Type: String

Allowed Values: dotnetcore1.0 | dotnetcore2.1 | go1.x | java8 | nodejs10.x | nodejs8.10 | provided | python2.7 | python3.6 | python3.7 | ruby2.5

Update requires: No interruption

Tags

A list of tags to apply to the function.

Required: No

Type: List of Tag

Update requires: No interruption

Timeout

The amount of time that Lambda allows a function to run before stopping it. The default is 3 seconds. The maximum allowed value is 900 seconds.

Required: No

Type: Integer

Minimum: 1

Update requires: No interruption

TracingConfig

Set Mode to Active to sample and trace a subset of incoming requests with AWS X-Ray.

Required: No

Type: TracingConfig

Update requires: No interruption

VpcConfig

For network connectivity to AWS resources in a VPC, specify a list of security groups and subnets in the VPC. When you connect a function to a VPC, it can only access resources and the internet through that VPC. For more information, see VPC Settings.

Required: No

Type: VpcConfig

Update requires: No interruption

Return Values

Ref

When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the resource name.

For more information about using the Ref function, see Ref.

Fn::GetAtt

The Fn::GetAtt intrinsic function returns a value for a specified attribute of this type. The following are the available attributes and sample return values.

For more information about using the Fn::GetAtt intrinsic function, see Fn::GetAtt.

Arn

The Amazon Resource Name (ARN) of the function.

Examples

Function

Create a Node.js function.

JSON

"AMIIDLookup": { "Type": "AWS::Lambda::Function", "Properties": { "Handler": "index.handler", "Role": { "Fn::GetAtt": [ "LambdaExecutionRole", "Arn" ] }, "Code": { "S3Bucket": "lambda-functions", "S3Key": "amilookup.zip" }, "Runtime": "nodejs8.10", "Timeout": 25, "TracingConfig": { "Mode": "Active" } } }

YAML

AMIIDLookup: Type: "AWS::Lambda::Function" Properties: Handler: "index.handler" Role: Fn::GetAtt: - "LambdaExecutionRole" - "Arn" Code: S3Bucket: "lambda-functions" S3Key: "amilookup.zip" Runtime: "nodejs8.10" Timeout: 25 TracingConfig: Mode: "Active"