Skip to content

Latest commit

 

History

History
172 lines (109 loc) · 4.61 KB

README.rst

File metadata and controls

172 lines (109 loc) · 4.61 KB

awslambda

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 known 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.