Skip to main content

FORKED for 4dn-dcic. Use to package and deploy lambda functions.

Project description

Pypi Python Versions

Python-lambda is a toolset for developing and deploying serverless Python code in AWS Lambda.

Important

This is a FORK of the original Python-lambda package by Nick Ficano. It will NOT be updated regularly and is frozen per our projects needs.

A call for contributors

With python-lambda and pytube both continuing to gain momentum, I’m calling for contributors to help build out new features, review pull requests, fix bugs, and maintain overall code quality. If you’re interested, please email me at nficano[at]gmail.com.

Description

AWS Lambda is a service that allows you to write Python, Java, or Node.js code that gets executed in response to events like http requests or files uploaded to S3.

Working with Lambda is relatively easy, but the process of bundling and deploying your code is not as simple as it could be.

The Python-Lambda library takes away the guess work of developing your Python-Lambda services by providing you a toolset to streamline the annoying parts.

Requirements

  • Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).

  • Pip (~8.1.1)

  • Virtualenv (~15.0.0)

  • Virtualenvwrapper (~4.7.1)

Getting Started

First, you must create an IAM Role on your AWS account called lambda_basic_execution with the LambdaBasicExecution policy attached.

On your computer, create a new virtualenv and project folder.

$ mkvirtualenv pylambda
(pylambda) $ mkdir pylambda

Next, download Python-Lambda using pip via pypi.

(pylambda) $ pip install python-lambda

From your pylambda directory, run the following to bootstrap your project.

(pylambda) $ lambda init

This will create the following files: event.json, __init__.py, service.py, and config.yaml.

Let’s begin by opening config.yaml in the text editor of your choice. For the purpose of this tutorial, the only required information is aws_access_key_id and aws_secret_access_key. You can find these by logging into the AWS management console.

Next let’s open service.py, in here you’ll find the following function:

def handler(event, context):
    # Your code goes here!
    e = event.get('e')
    pi = event.get('pi')
    return e + pi

This is the handler function; this is the function AWS Lambda will invoke in response to an event. You will notice that in the sample code e and pi are values in a dict. AWS Lambda uses the event parameter to pass in event data to the handler.

So if, for example, your function is responding to an http request, event will be the POST JSON data and if your function returns something, the contents will be in your http response payload.

Next let’s open the event.json file:

{
  "pi": 3.14,
  "e": 2.718
}

Here you’ll find the values of e and pi that are being referenced in the sample code.

If you now try and run:

(pylambda) $ lambda invoke -v

You will get:

# 5.858

# execution time: 0.00000310s
# function execution timeout: 15s

As you probably put together, the lambda invoke command grabs the values stored in the event.json file and passes them to your function.

The event.json file should help you develop your Lambda service locally. You can specify an alternate event.json file by passing the --event-file=<filename>.json argument to lambda invoke.

When you’re ready to deploy your code to Lambda simply run:

(pylambda) $ lambda deploy

The deploy script will evaluate your virtualenv and identify your project dependencies. It will package these up along with your handler function to a zip file that it then uploads to AWS Lambda.

You can now log into the AWS Lambda management console to verify the code deployed successfully.

Wiring to an API endpoint

If you’re looking to develop a simple microservice you can easily wire your function up to an http endpoint.

Begin by navigating to your AWS Lambda management console and clicking on your function. Click the API Endpoints tab and click “Add API endpoint”.

Under API endpoint type select “API Gateway”.

Next change Method to POST and Security to “Open” and click submit (NOTE: you should secure this for use in production, open security is used for demo purposes).

At last you need to change the return value of the function to comply with the standard defined for the API Gateway endpoint, the function should now look like this:

def handler(event, context):
    # Your code goes here!
    e = event.get('e')
    pi = event.get('pi')
    return {
        "statusCode": 200,
        "headers": { "Content-Type": "application/json"},
        "body": e + pi
    }

Now try and run:

$ curl --header "Content-Type:application/json" \
       --request POST \
       --data '{"pi": 3.14, "e": 2.718}' \
       https://<API endpoint URL>
# 5.8580000000000005

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-lambda-4dn-0.12.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_lambda_4dn-0.12.0-py2.py3-none-any.whl (11.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file python-lambda-4dn-0.12.0.tar.gz.

File metadata

  • Download URL: python-lambda-4dn-0.12.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for python-lambda-4dn-0.12.0.tar.gz
Algorithm Hash digest
SHA256 60393e184e172ed0cc9bc1ac4ba946fc0b8220076b90752d4149adfea57f25a4
MD5 6af17650f69f53e7b5fa62da19ee949b
BLAKE2b-256 2e8dd4b728569c6707435c0a9f7230e6dae2dc54ed6f94b52389737097d0d5f6

See more details on using hashes here.

File details

Details for the file python_lambda_4dn-0.12.0-py2.py3-none-any.whl.

File metadata

  • Download URL: python_lambda_4dn-0.12.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for python_lambda_4dn-0.12.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 54c2689d2b5e1bdcb9d9aebceadd1a2ae30725a807d725f3689a4100418a949e
MD5 a75e200e0dfa6942cbf8fe3d606b0a76
BLAKE2b-256 bc438e449484d12b11cf009f12077eac838beda482d0ae31df66168eec9f2877

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page