Skip to main content

AWS Lambda event handler manager

Project description

Domovoi is an extension to AWS Labs Chalice to handle AWS Lambda event sources other than HTTP requests through API Gateway. Domovoi lets you easily configure and deploy a Lambda function to run on a schedule or in response to a variety of events like an SNS push notification, S3 event, or custom state machine transition:

import json, boto3, domovoi

app = domovoi.Domovoi()

@app.scheduled_function("cron(0 18 ? * MON-FRI *)")
def foo(event, context):
    context.log("foo invoked at 06:00pm (UTC) every Mon-Fri")
    return dict(result=True)

@app.scheduled_function("rate(1 minute)")
def bar(event, context):
    context.log("bar invoked once a minute")
    boto3.resource("sns").create_topic(Name="bartender").publish(Message=json.dumps({"beer": 1}))
    return dict(result="Work work work")

@app.sns_topic_subscriber("bartender")
def tend(event, context):
    message = json.loads(event["Records"][0]["Sns"]["Message"])
    context.log(dict(beer="Quadrupel", quantity=message["beer"]))

@app.cloudwatch_event_handler(source=["aws.ecs"])
def monitor_ecs_events(event, context):
    message = json.loads(event["Records"][0]["Sns"]["Message"])
    context.log("Got an event from ECS: {}".format(message))

@app.s3_event_handler(bucket="myS3bucket", events=["s3:ObjectCreated:*"], prefix="foo", suffix=".bar")
def monitor_s3(event, context):
    message = json.loads(event["Records"][0]["Sns"]["Message"])
    context.log("Got an event from S3: {}".format(message))

# Set use_sns=False to subscribe your Lambda directly to S3 events without forwrading them through an SNS topic.
# This has fewer moving parts, but you can only subscribe one Lambda function to events in a given S3 bucket.
@app.s3_event_handler(bucket="myS3bucket", events=["s3:ObjectCreated:*"], prefix="foo", suffix=".bar", use_sns=False)
def monitor_s3(event, context):
    message = json.loads(event["Records"][0]["Sns"]["Message"])
    context.log("Got an event from S3: {}".format(message))

# See http://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html
# See the "AWS Step Functions state machines" section below for a complete example of setting up a state machine.
@app.step_function_task(state_name="Worker", state_machine_definition=state_machine)
def worker(event, context):
    return {"result": event["input"] + 1}

Installation

pip install domovoi

Usage

First-time setup:

chalice new-project

Replace the Chalice app entry point (in app.py) with the Domovoi app entry point as above, then deploy the event handlers:

domovoi deploy

To stage files into the deployment package, use a domovoilib directory in your project where you would use chalicelib in Chalice. For example, my_project/domovoilib/rds_cert.pem becomes /var/task/domovoilib/rds_cert.pem with your function executing in /var/task/app.py with /var/task as the working directory. See the Chalice docs for more information on how to set up Chalice configuration.

Supported event types

See http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html for an overview of event sources that can be used to trigger Lambda functions. Domovoi supports the following event sources:

TODO:

  • CloudWatch Logs filter subscriptions

  • DynamoDB events

  • SES (email) events

AWS Step Functions state machines

Domovoi supports AWS Lambda integration with AWS Step Functions. Step Functions state machines can be started using the StartExecution method or the API Gateway Step Functions integration.

See domovoi/examples/state_machine_app.py for a complete example of a Domovoi app.py using a state machine.

When creating a Step Functions State Machine driven Domovoi daemon Lambda, the State Machine assumes the same IAM role as the Lambda itself. To allow the State Machine to invoke the Lambda, edit the IAM policy (under your app directory, in .chalice/policy.json) to include a statement allowing the “lambda:InvokeFunction” action on all resources, or on the ARN of the Lambda itself.

Configuration: Dead Letter Queues

To enable your Lambda function to forward failed invocation notifications to dead letter queuees, set the configuration key dead_letter_queue_target_arn in the file .chalice/config.json to the target DLQ ARN. For example:

{
  "app_name": "my_app",
  ...
  "dead_letter_queue_target_arn": "arn:aws:sns:us-east-1:123456789012:it-ded"
}

You may need to update your Lambda IAM policy (.chalice/policy.json) to give your Lambda access to SNS or SQS.

License

Licensed under the terms of the Apache License, Version 2.0.

https://travis-ci.org/kislyuk/domovoi.png https://codecov.io/github/kislyuk/domovoi/coverage.svg?branch=master https://img.shields.io/pypi/v/domovoi.svg https://img.shields.io/pypi/l/domovoi.svg https://readthedocs.org/projects/domovoi/badge/?version=latest

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

domovoi-1.3.2.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

domovoi-1.3.2-py2.py3-none-any.whl (13.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file domovoi-1.3.2.tar.gz.

File metadata

  • Download URL: domovoi-1.3.2.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for domovoi-1.3.2.tar.gz
Algorithm Hash digest
SHA256 75aac02f3299ef7415d13f7d762c4993f7a925c3988d69d57a471b0125d6e617
MD5 43e3a4069bd8c6444a1f12184ed04598
BLAKE2b-256 c7779d08e5de39eb8f3757157ddb84848eb360a2504468db1c2bbba0ac58fda3

See more details on using hashes here.

File details

Details for the file domovoi-1.3.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for domovoi-1.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d5a0301daba90ad968d3446517d0ffd00d2b5077e7f1d7d39c548c99b127de77
MD5 3c0fb12c0fc5e59532c4c06be41479c5
BLAKE2b-256 70ea1f21f9444586996ae7cf666bea3cea63389f3f6dccede3cb857d6d573cef

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