Skip to main content

A simple way to create AWS Lambda projects in Python heavily inspired and copied from https://github.com/nficano/python-lambda

Project description

Lambada (Alpha)

A simple way to work with AWS Lambda projects in Python. Heavily inspired and copied from https://github.com/nficano/python-lambda

Installation

$ pip install lambada-again

Basic Usage

A basic lambda without dependencies.

$ tree

├── config.base.yaml    # Base configuration file
└── config.py           # Handler
└── service.py          # Handler

Init

$ lambada init

Run

$ lambada run 
$ lambada run  -n lambda-name

If there is more than one lambda it shows options from where to chose.

Invoke remotely

$ lambada invoke
$ lambada invoke -n lambda-name

Deploy lambdas and layers

$ lambada deploy (deploy all)
$ lambada deploy -n name
$ lambada deploy -c config.qa.yaml
$ lambada deploy -c config.prod.yaml

If there is a layer associated to the lambda in the config.yaml without a version specified it will update it with the last one.

Update lambda configuration

$ lambada update-config
$ lambada update-config -c config.qa.yaml
$ lambada update-config -c config.prod.yaml

If there is a layer associated to the lambda in the config.yaml without a version specified it will update it with the last one.

Build

$ lambada build

It will create a zip file in the ./dist directory

File structure

We need to have a configuration file and a main file to call.

With multiples configuration files

$ tree

├── config.yaml
├── config.qa.yaml
├── config.prod.yaml
├── requirements.txt
└── service.py

We can have parent-child configurations

The configuration files needs to have the same name. It will build only the requirements.txt at the lambda directory.

$ tree

├── requirements.txt
├── lambda-A
│   ├── README.md
│   ├── requirements.txt
│   └── service.py
├── lambda-B
│   ├── README.md
│   ├── requirements.txt
│   └── service.py
├── config.yaml
├── config.prod.yaml
├── requirements.txt
└── README.md

And we need to execute the commands in the parent directory and specify the name of the lambda with -n.

$ lambada run -n lambda-A
$ lambada deploy -n lambda-A -c config.prod.yaml
$ lambada invoke -n lambda-A -c config.prod.yaml

How to use it

Run locally

$ lambada run [-n lambda name] [-c configuration file]
$ lambada run
$ lambada run -n lambda-name

Configuration

Event test file (optional)

You can test the lambda locally passing an event input defined in the configuration file as:

# path to file.property
test_event: event.input

This will look for the event.py file in the lambda directory and get the input property from it.

lambda-directory$ cat event.py
input = {'test': 'test'}
Layers (optional)

If we define a local module as a layer it will load the layer so we can call it from our lambda.

layers:
  - layer-name

We need to have the dependencies installed in our local virtual environment.

Environment vars

You can pass and override environment variables in the config.yaml using the -e option.

$ lambada run -e var1=value1 -e var2=value2

Invoke remotly

$ lambada invoke [-n lambda name] [-c configuration file]
$ lambada invoke
$ lambada invoke -n lambda-name

Build

It will bundle all the dependencies and create a dist directory with the zip file.

$ lambada build [-n lambda name] [-c configuration file]
$ lambada build
$ lambada build -n lambda-name

Configuration

Requirements (optional)

If there is a requirements file specified it will install the packages locally

requirements: requirements.txt
Directories (optional)

By default it will add only the directories specified in the directories section.

directories                 
  - src
Files (optional) (default= all files + main file - directories)

By default it will add all the files. You can specify which ones in the files section.

files:                      # Files we want to include in the root directoy 
  - config.py

Symlink

It will copy the symlink into the bundle.

Deploy

It will create or update the Lambda and deploy the zipfile created in the build step into AWS.

$ lambada deploy [-n name] [-c configuration file]

Deploy all

$ lambada deploy

Deploy one lambda/layer

$ lambada deploy -n name

Choose configuration file

$ lambada deploy -c config.file.yaml

Configuration

These values are required in the configuration file

name: lambda-function-name
description: Description
region: us-east-1
main_file: service.py
handler: handler
runtime: python3.6
role: lambda_basic_execution

aws_access_key_id: access_key_id
aws_secret_access_key: secret_access_key

Default values

main_file: service.py
handler: handler
runtime: python3.6
role: lambda_basic_execution

Environment variables

environment_variables:
  DB: 'postgresql://postgres:@localhost:5432/template'

Security groups and Subnets

security_group_ids:
  - sg-123456789

subnet_ids:
  - subnet-a123456789
  - subnet-b123456789

Alias

alias: dev

Layers

layers:
  - ../lib/config.yaml
  - name-of-the-layer

Info

It will print the lambda information

$ lambada info [-n lambda name] [-c configuration file]
$ lambada info
$ lambada info -n lambda-name

Update configuration

It will update the lambda configuration. Useful if we did only configuration changes.

$ lambada update_config [-n lambda name] [-c configuration file]
$ lambada update_config
$ lambada update_config -n lambda-name

Configuration file example

$ cat config.base.yaml
lambdas:
  base:
    abstract: True
    region: us-east-1
    runtime: python3.6
    role: lambda-role
    main_file: service.py
    handler: handler
    # path to file.property
    test_event: event.input

    security_group_ids:
      - sg-12345

    subnet_ids:
      - subnet-1
      - subnet-2

  lambda-test:
    parent: base
    name: function name test
    description: function description
    path: './lambda-test'

    environment_variables:
      DB: 'postgresql://postgres:@localhost:5432/template'
      TEST: 'test'

    directories                 # Directories we want to deploy
      - src

    files:                      # Files we want to include that are in the root directoy 
      - config.py

    # We can specify a local layer or a remote layer
    layers:
      - layer-1

layers:
  layer-1:
    name: layer-1
    runtime: python3.6
    description: Layer-1
    requirements: requirements.txt
    path: layer
$ cat config.yaml
aws_access_key_id: access_key_id
aws_secret_access_key: secret_access_key

parent: config.base.yaml

Layers

We can also build, deploy, update and get info on layers.

Lambda

We can define a layer dependency inside a lambda in two ways.

We can specify the name of the layer:

layers:
  - name-of-the-layer

By default it will set up the last version of the layer.

You can specify a different like this:

layers:
  - name-of-the-layer,3

Configuration file example

The main difference is the is_layer propertiy is set to true.

name: layer_name
description: Description
is_layer: true
region: us-east-1
main_file: service.py
handler: handler
runtime: python3.6

requirements: requirements.txt
files:
  - utils.py

directories: 
  - lib

aws_access_key_id: access_key_id
aws_secret_access_key: secret_access_key

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

lambada-again-0.2.6.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

lambada_again-0.2.6-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file lambada-again-0.2.6.tar.gz.

File metadata

  • Download URL: lambada-again-0.2.6.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for lambada-again-0.2.6.tar.gz
Algorithm Hash digest
SHA256 cd1b30e43421c0d256f7a6eb01ec6efb822c8988d3c27559d9636cea056be6c6
MD5 a7682de5cbfce5f396d1509260d631ba
BLAKE2b-256 1efcf4e90a445b5c9f7b7bda0a8413a50d201d93e3996bd8c7d9a996a56195f8

See more details on using hashes here.

File details

Details for the file lambada_again-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: lambada_again-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for lambada_again-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 706aa053d8e5cbc453a412c04cef07575b15cd0583603cde700ce52ebe56c2f6
MD5 b0230540bc31541ce8241c81da52f60e
BLAKE2b-256 41c515a3519b87b1ea9ae8af31e6e2bee0a7d258e0d96e1b7c5c201a08d7eb4d

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