Package: awslambda
A tool for deploying Python projects to AWS Lambda.Getting startedAuthentication is left toboto3so you can set it up just like theregular
AWS CLI. You need anS3 bucketfor
temporary storage. For a quick tutorial on execution roles, see theofficial
docs(of course you need one that can execute lambdas).In a new folder, createmymodule.py.defhello(*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/myroleWhenawslambdais 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 helloYou can use as many options as you like (some shown here with short names).awslambda . mybucket -u hello -u myotherlambda --delete myoldlambdaOr specify your functions in a YAML file (let’s call itsync.yaml).hello:handler:mymodule.hellorole:arn:aws:iam::xxxxxxxxxxxx:role/myrole# myotherlambda:#     handler: myothermodule.myotherhandler#     role: arn:aws:iam::xxxxxxxxxxxx:role/myroleWhen syncing from a file,awslambdawill update existing functions and create
the others automatically.awslambda . mybucket --sync sync.yamlTo add dependencies, use yourpip requirements file.awslambda . mybucket -s sync.yaml --requirements requirements.txtNote that compiled dependenciesawslambdadownloads on your local machine
might not work on the AWS servers. Pure Python libraries should always work.
For others, it could be helpful to runawslambdaitself in a Lambda function.
A process knows asawslambdaception.A template greeting pageLet’s use the features introduced above to create a greeting page. We will use
theJinja2templating engine.
Editmymodule.py,fromjinja2importTemplatetemplate=Template('''
<html>
  <body>
    <h1>Hello, {{ parameters.name }}!</h1>
    <p>{{ parameters.message }}</p>
  </body>
</html>
''')defhello(event,context):return{'statusCode':200,'headers':{'Content-Type':'text/HTML'},'body':template.render(parameters=event['queryStringParameters'])}and create a simplerequirements.txt.Jinja2Deploy,awslambda . mybucket -s sync.yaml -r requirements.txtthen open the function in your AWS console. Go toTriggersand add anAPI Gatewaytrigger. Set security toOpenfor 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!UsageUsage: 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.