Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A toolkit for developing and deploying serverless Python code in AWS Lambda.

License

NotificationsYou must be signed in to change notification settings

nficano/python-lambda

Repository files navigation

python-lambda logo

pypipypi

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

A call for contributors

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

Description

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

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

ThePython-Lambda library takes away the guess work of developing yourPython-Lambda services by providing you a toolset to streamline the annoyingparts.

Requirements

  • Python 2.7, >= 3.6 (At the time of writing this, these are the Python runtimes supported by AWS Lambda).
  • 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 calledlambda_basic_execution with theLambdaBasicExecution policy attached.

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

$ mkvirtualenv pylambda(pylambda) $ mkdir pylambda

Next, downloadPython-Lambda using pip via pypi.

(pylambda) $ pip install python-lambda

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

(pylambda) $ lambda init

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

Let's begin by openingconfig.yaml in the text editor of your choice. Forthe purpose of this tutorial, the only required information isaws_access_key_id andaws_secret_access_key. You can find these bylogging into the AWS management console.

Next let's openservice.py, in here you'll find the following function:

defhandler(event,context):# Your code goes here!e=event.get('e')pi=event.get('pi')returne+pi

This is the handler function; this is the function AWS Lambda will invoke inresponse to an event. You will notice that in the sample codee andpiare values in adict. AWS Lambda uses theevent parameter to pass inevent data to the handler.

So if, for example, your function is responding to an http request,eventwill be thePOST JSON data and if your function returns something, thecontents will be in your http response payload.

Next let's open theevent.json file:

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

Here you'll find the values ofe andpi that are being referenced inthe 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, thelambda invoke command grabs the valuesstored in theevent.json file and passes them to your function.

Theevent.json file should help you develop your Lambda service locally.You can specify an alternateevent.json file by passing the--event-file=<filename>.json argument tolambda 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 projectdependencies. It will package these up along with your handler function to azip file that it then uploads to AWS Lambda.

You can now log into theAWS Lambda management console toverify the code deployed successfully.

Wiring to an API endpoint

If you're looking to develop a simple microservice you can easily wire yourfunction up to an http endpoint.

Begin by navigating to yourAWS Lambda management console andclicking on your function. Click the API Endpoints tab and click "Add API endpoint".

Under API endpoint type select "API Gateway".

Next change Method toPOST and Security to "Open" and click submit (NOTE:you should secure this for use in production, open security is used for demopurposes).

At last you need to change the return value of the function to comply with thestandard defined for the API Gateway endpoint, the function should now looklike 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

Environment Variables

Lambda functions support environment variables. In order to set environmentvariables for your deployed code to use, you can configure them inconfig.yaml. To load the value for the environment variable at the time ofdeployment (instead of hard coding them in your configuration file), you canuse local environment values (see 'env3' in example code below).

environment_variables:env1:fooenv2:bazenv3:${LOCAL_ENVIRONMENT_VARIABLE_NAME}

This would create environment variables in the lambda instance upon deploy. Ifyour functions don't need environment variables, simply leave this section outof your config.

Uploading to S3

You may find that you do not need the toolkit to fullydeploy your Lambda or that your code bundle is too large to upload via the API.You can use theupload command to send the bundle to an S3 bucket of yourchoosing. Before doing this, you will need to set the following variables inconfig.yaml:

role:basic_s3_uploadbucket_name:'example-bucket's3_key_prefix:'path/to/file/'

Your role must haves3:PutObject permission on the bucket/key that youspecify for the upload to work properly. Once you have that set, you canexecutelambda upload to initiate the transfer.

Deploying via S3

You can also choose to use S3 as your source for Lambda deployments. This canbe done by issuinglambda deploy-s3 with the same variables/AWS permissionsyou'd set for executing theupload command.

Development

Development of "python-lambda" is facilitated exclusively on GitHub.Contributions in the form of patches, tests and feature creation and/orrequests are very welcome and highly encouraged. Please open an issue if thistool does not function as you'd expect.

Environment Setup

  1. Install pipenv
  2. Install direnv
  3. Install Precommit (optional but preferred)
  4. cd into the project and enter "direnv allow" when prompted. This will begininstalling all the development dependancies.
  5. If you installed pre-commit, runpre-commit install inside the projectdirectory to setup the githooks.

Releasing to Pypi

Once you pushed your chances to master, runone of the following:

# If you're installing a major release:make deploy-major# If you're installing a minor release:make deploy-minor# If you're installing a patch release:make deploy-patch

About

A toolkit for developing and deploying serverless Python code in AWS Lambda.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors40


[8]ページ先頭

©2009-2025 Movatter.jp