Skip to main content

No project description provided

Project description

Overview

This is an extremely simple framework to facilitate developing rest services running as lambda services in aws.

Features

  • VERY simple
  • One lambda function required to host a whole rest service
  • Allows for local development

Shortcomings

  • Only supports Python
  • Does not support non json responses
  • Query variables not supported yet
  • Headers not exposed in route handlers
  • Many others

Please raise issues for any enhancements or bugs.

The FAQ, always the FAQ.

Index

  1. Workflow in a nutshell
  2. Installing
  3. Create and run a new project
  4. Build the aws lambda zip
  5. Install the zip as a lambda
  6. Configure the api gateway
  7. Developer section

Workflow in a nutshell

  1. Install
  2. Create project
  3. Develop and test locally
  4. Build zip file for aws lambda
  5. Upload to aws
  6. If not done goto 3

Installing

Install the package using pip:

pip install aws_lambda_mess

After a successful installation check that the cli works:

alm --help

Create and run a new project

Create a new project using the cli:

alm new demo

This will:

  • create a new directory with the name you specified
  • populate the directory with some standard boilerplate

A sample app has been created for you in src\app.py

from aws_lambda_mess.framework.Route import Route
from aws_lambda_mess.framework.success import success
from aws_lambda_mess.framework.failures import bad_request
from aws_lambda_mess.framework.server import run


def index(params, body):
    return success({"Hallo": "Index"})


def greet(params, body):
    return success({"Hallo": params["name"]})


def default(params, body):
    return bad_request()


routes = [
    Route(method_pattern="GET", path_pattern="/greet/<name>", handler=greet),
    Route(method_pattern=".*", path_pattern=".*", handler=default)
    Route(method_pattern="GET", path_pattern="/", handler=index),
]

from aws_lambda_mess.framework.lambda_dispatcher import get_handler
lambda_handler = get_handler(routes)

if __name__ == "__main__":
    run(9000, routes)

To run the app locally simply run:

python app.py

A webserver serving the above routes will spin up on port 9000

$ curl -X GET localhost:9000/
{"Hallo": "Index"}

$ curl -X GET localhost:9000/greet/somebody
{"Hallo": "somebody"}

$ curl -X GET localhost:9000/whatwhere -v
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1:9000...
* Connected to localhost (127.0.0.1) port 9000 (#0)
> GET /whatwhere HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.88.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 400 Bad Request
< Server: BaseHTTP/0.6 Python/3.9.13
< Date: Sun, 30 Apr 2023 06:38:25 GMT
<
* Closing connection 0

Build the aws lambda zip

To build the package to be uploaded to aws simply run:

alm build

This will create a zip file in the dist directory called package.zip

Upload this package to aws lambda either in the lambda console or via s3.

Install the zip as a lambda

Once the package has been uploaded change the handler setting under runtime settings to app.lambda_handler

Now test the package with some of these json test cases:

Test /

{
  "body": "{}",
  "resource": "/{proxy+}",
  "path": "/",
  "httpMethod": "GET",
  "isBase64Encoded": true
}

Returns

{
  "isBase64Encoded": false,
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{\"Hallo\": \"Index\"}"
}

Test /greet/somebody

{
  "body": "{}",
  "resource": "/{proxy+}",
  "path": "/greet/somebody",
  "httpMethod": "GET",
  "isBase64Encoded": true
}

Returns

{
  "isBase64Encoded": false,
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{\"Hallo\": \"somebody\"}"
}

Test invalid route or method

{
  "body": "{}",
  "resource": "/{proxy+}",
  "path": "/greet/somebody",
  "httpMethod": "POST",
  "isBase64Encoded": true
}
{
  "isBase64Encoded": false,
  "statusCode": 400
}

This json gets converted to a proper http response by the proxy.

Configure the api gateway

The API gateway you attach to the lambda needs to look like this API Gateway and the methods needs to be defined to look like this: Methods

Developer section

Run tests

???

Build aws_lambda_mess

hatch build

Upload to pip

py -m twine upload dist/* --user johanjordaan

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

aws_lambda_mess-0.0.9.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

aws_lambda_mess-0.0.9-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

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