Skip to main content

A tool for deploying Python projects to AWS Lambda.

Project description

A tool for deploying Python projects to AWS Lambda.

Getting started

Authentication is left to boto3 so you can set it up just like the regular AWS CLI. You need an S3 bucket for temporary storage. For a quick tutorial on execution roles, see the official docs (of course you need one that can execute lambdas).

In a new folder, create mymodule.py.

def hello(*args):
    return "Hello, world!"

Then deploy the function (fill in your execution role resource name from the AWS console).

awslambda . mybucket --create hello mymodule.hello arn:aws:iam::xxxxxxxxxxxx:role/myrole

When awslambda is done, you can test your new function in the Lambda management console.

>From now on, if you make changes to the function, just run:

awslambda . mybucket --update hello

You can use as many options as you like (some shown here with short names).

awslambda . mybucket -u hello -u myotherlambda --delete myoldlambda

Or specify your functions in a YAML file (let’s call it sync.yaml).

hello:
    handler: mymodule.hello
    role: arn:aws:iam::xxxxxxxxxxxx:role/myrole
# myotherlambda:
#     handler: myothermodule.myotherhandler
#     role: arn:aws:iam::xxxxxxxxxxxx:role/myrole

When syncing from a file, awslambda will update existing functions and create the others automatically.

awslambda . mybucket --sync sync.yaml

To add dependencies, use your pip requirements file.

awslambda . mybucket -s sync.yaml --requirements requirements.txt

Note that compiled dependencies awslambda downloads on your local machine might not work on the AWS servers. Pure Python libraries should always work. For others, it could be helpful to run awslambda itself in a Lambda function. A process knows as awslambdaception.

A template greeting page

Let’s use the features introduced above to create a greeting page. We will use the Jinja2 templating engine. Edit mymodule.py,

from jinja2 import Template

template = Template('''
<html>
  <body>
    <h1>Hello, {{ parameters.name }}!</h1>
    <p>{{ parameters.message }}</p>
  </body>
</html>
''')


def hello(event, context):
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'text/HTML'},
        'body': template.render(parameters=event['queryStringParameters'])}

and create a simple requirements.txt.

Jinja2

Deploy,

awslambda . mybucket -s sync.yaml -r requirements.txt

then open the function in your AWS console. Go to Triggers and add an API Gateway trigger. Set security to Open for now. Open the URL of the created trigger in your browser. You should see “Hello, !”. To customize the page append e.g.

?name=Commander Shepard&message=You've received a new message at your private terminal.

to the URL and enjoy your serverless, templated webpage!

Usage

Usage: awslambda [OPTIONS] SOURCE_DIR S3_BUCKET

  Deploy Python code to AWS lambda.

  Zips the contents of the source directory together with optional pip
  requirements. The archive is temporarily uploaded to an S3 bucket and used
  to create or update lambda functions.

  Reference handlers from your source directory like you would in any Python
  module-tree (e.g. mymodule.myhandler, mymodule.mysubmodule.myhandler,
  etc.).

  Roles are ARNs like "arn:aws:iam::xxxxxxxxxxxx:role/myrole"

  YAML file entries for the sync option map function names to handlers and
  roles:

      myLambda:
          handler: mymodule.myhandler
          role: arn:aws:iam::xxxxxxxxxxxx:role/myrole

Options:
  -r, --requirements PATH         pip compatible requirements file. Will be
                                  included in the archive.
  -c, --create NAME HANDLER ROLE  Create a new lambda function. Example:
                                  --create myLambda mymodule.myhandler myrole
  -u, --update NAME               Update a lambda function.
  -d, --delete NAME               Delete a lambda function.
  -s, --sync FILENAME             Keep lambdas defined in YAML file in sync
                                  with deployed lambdas.
  --help                          Show this message and exit.

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

awslambda-1.0.6.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

awslambda-1.0.6-py2-none-any.whl (9.8 kB view details)

Uploaded Python 2

File details

Details for the file awslambda-1.0.6.tar.gz.

File metadata

  • Download URL: awslambda-1.0.6.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for awslambda-1.0.6.tar.gz
Algorithm Hash digest
SHA256 8c8b9813ae57b7e402f9495ddc47149a147de30937cf02534d7a3c92da6cf7bb
MD5 bf9c61869733bb6143cf68ebf759d0cf
BLAKE2b-256 7fecdc6779b725247012fe4a761ab17ccf9b6fb682ffd076705a593f1940eac9

See more details on using hashes here.

File details

Details for the file awslambda-1.0.6-py2-none-any.whl.

File metadata

File hashes

Hashes for awslambda-1.0.6-py2-none-any.whl
Algorithm Hash digest
SHA256 86f29e30085fc8b1324862b18d8b319e15debcae8a134f4ea1d6cb6c82477619
MD5 fb7941aa4cb6336b8913a9ef47230110
BLAKE2b-256 13f976afb2d1aea570c999d308b7cc78182670ec944492d07cdf1a54ec114284

See more details on using hashes here.

Supported by

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