My AWS Lambda Cookbook

This cookbook is still in progress and will grow over time.

Lambda, AWS’s bite-size “serverless” compute service, is mostly awesome. However, it still has a relative lack of good documentation.

I’ve been using Lambda a lot lately, meaning I’ve had a lot of browser tabs open trying to find examples of the latest features like VPC support, Cloudformation integration and Python 2.7 functions. In this post, I’ll try to save you some time by sharing examples of a few things that have sent me searching.

CloudFormation resource example for a Lambda function in a VPC

"VPCLambdaFunction": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
        "Code": {
            "S3Bucket": "mybucket",
            "S3Key": "mykey/"
        "Description": "A function that lives in a VPC",
        "FunctionName": "MyVPCFunction",
        "Handler": "function.lambda_handler",
        "MemorySize": "128",
        "Role": "MyRole",
        "Runtime": "python2.7",
        "Timeout": 60,
        "VpcConfig": {
            "SecurityGroupIds": ["sg-12345678","sg-02345679"],
            "SubnetIds": ["subnet-12345678"]


  • You don’t need to specify the VPC ID anywhere; AWS will figure it out from the subnets and security groups in the VpcConfig object.
  • If you plan to use the AWS CLI inside your function, make sure you allow inbound internet access (HTTP/S) in one of your security groups.

CloudFormation resource example with inline Python

 "InlinePythonLambdaFunction": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
        "Code": {
             "ZipFile": { "Fn::Join": ["\n", [
                 "import json",
                 "def handler(event, context):",
                 "    lambdaRetVal = {\"Hello\":\"World\"}",
                 "    return json.dumps(lambdaRetVal)"]
        "Description": "A resource written using inline Python",
        "FunctionName": "MyInlinePythonFunction",
        "Handler": "index.handler",
        "MemorySize": "128",
        "Role": "MyRole",
        "Runtime": "python2.7",
        "Timeout": 60


  • If you use inline Python in a CF template, the “Handler” value for the lambda function resource MUST be “index.[handler]”, where [handler] is the name of your lambda handler function defined in the Python code. This is because the Python file Lambda creates from your inline code is named “”.
  • Each line of Python gets its own string. (You’ve seen this before if you use CloudFormation UserData.) Thus, indented code has leading spaces.

Deploy an AWS Lambda function using a Python library that requires native dependencies

This one deserves a little more detail. In my use case, I needed the python-ldap module in a Lambda function. No problem; Lambda lets you create “deployment packages” with external modules and scripts zipped up together.

First try: I zipped up the python-ldap library with the calling script on my Mac and uploaded it to Lambda. Didn’t work; Lambda couldn’t find the module, so I got this error:

Unable to import module 'your-module': No module named ldap

Not surprising, since python-ldap itself has a couple of native dependencies. Second try: I followed these instructions to install the library in a virtualenv, again on my Mac, and zip the results. Still no luck.

Unable to import module 'your-module': /var/task/ invalid ELF header

Realizing that my Mac itself was the issue, I rebuilt the zip file on an AWS Linux instance using the following script:

pip install virtualenv
virtualenv venv
source venv/bin/activate

#install python-ldap and its native dependencies
yum install python-devel
yum install openldap-devel
pip install python-ldap

#create zip file with libraries and wrapper script for Lambda upload
zip ./
cd $VIRTUAL_ENV/lib64/python2.7/site-packages
zip -r $zipLocation/ *

The resulting zip file worked great in Lambda! (Thanks to Austin Carroll for research assistance with this issue.)

I’ll try and add more examples to this page as time goes by. Please drop me a line if you are stumped with something Lambda-related!

My AWS Lambda Cookbook

One thought on “My AWS Lambda Cookbook

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s