Menu
AWS CloudFormation
User Guide (API Version 2010-05-15)

AWS::Lambda::Function

The AWS::Lambda::Function resource creates an AWS Lambda (Lambda) function that can run code in response to events. For more information, see CreateFunction in the AWS Lambda Developer Guide.

Syntax

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

JSON

{
  "Type" : "AWS::Lambda::Function",
  "Properties" : {
    "Code" : Code,
    "Description" : String,
    "FunctionName" : String,
    "Handler" : String,
    "MemorySize" : Integer,
    "Role" : String,
    "Runtime" : String,
    "Timeout" : Integer,
    "VpcConfig" : VPCConfig
  }
}

YAML

Type: "AWS::Lambda::Function"
Properties: 
  Code:
    Code
  Description: String
  FunctionName: String
  Handler: String
  MemorySize: Integer
  Role: String
  Runtime: String
  Timeout: Integer
  VpcConfig:
    VPCConfig

Properties

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.

Required: Yes

Type: AWS Lambda Function Code

Update requires: No interruption

Description

A description of the function.

Required: No

Type: String

Update requires: No interruption

FunctionName

A name for the function. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the function's name. For more information, see Name Type.

Important

If you specify a name, you cannot do updates that require this resource to be replaced. You can still do updates that require no or some interruption. If you must replace the resource, specify a new name.

Required: No

Type: String

Update requires: Replacement

Handler

The name of the function (within your source code) that Lambda calls to start running your code. For more information, see the Handler property in the AWS Lambda Developer Guide.

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.

Required: Yes

Type: String

Update requires: No interruption

MemorySize

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.

Your function use case determines your CPU and memory requirements. For example, a database operation might need less memory than an image processing function. You must specify a value that is greater than or equal to 128, and it must be a multiple of 64. You cannot specify a size larger than 1536. The default value is 128 MB.

Required: No

Type: Integer

Update requires: No interruption

Role

The Amazon Resource Name (ARN) of the AWS Identity and Access Management (IAM) execution role that Lambda assumes when it runs your code to access AWS services.

Required: Yes

Type: String

Update requires: No interruption

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.

Required: Yes

Type: String

Update requires: Replacement

Timeout

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. By default, Timeout is set to 3 seconds.

Required: No

Type: Integer

Update requires: No interruption

VpcConfig

If the Lambda function requires access to resources in a VPC, specify a VPC configuration that Lambda uses to set up an elastic network interface (ENI). The ENI enables your function to connect to other resources in your VPC, but it doesn't provide public Internet access. If your function requires Internet access (for example, to access AWS services that don't have VPC endpoints), configure a Network Address Translation (NAT) instance inside your VPC or use an Amazon Virtual Private Cloud (Amazon VPC) NAT gateway. For more information, see NAT Gateways in the Amazon VPC User Guide.

Required: No

Type: AWS Lambda Function VPCConfig

Update requires: No interruption

Return Values

Ref

When the logical ID of this resource is provided to the Ref intrinsic function, Ref returns the resource name.

In the following sample, the Ref function returns the name of the AMILookUp function, such as MyStack-AMILookUp-NT5EUXTNTXXD.

{ "Ref": "AMILookUp" }

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

Fn::GetAtt

Fn::GetAtt returns a value for a specified attribute of this type. This section lists the available attributes and sample return values.

Arn

The ARN of the Lambda function, such as arn:aws:lambda:us-west-2:123456789012:MyStack-AMILookUp-NT5EUXTNTXXD.

For more information about using Fn::GetAtt, see Fn::GetAtt.

Example

The following example uses a packaged file in an S3 bucket to create a Lambda function.

JSON

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

YAML

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

Related Resources

For more information about how you can use a Lambda function with AWS CloudFormation custom resources, see AWS Lambda-backed Custom Resources.

For a sample template, see AWS Lambda Template.