Package: domovoi
Domovoiis an extension toAWS Chaliceto handleAWS Lambdaevent sourcesother
than HTTP requests through API Gateway. Domovoi lets you easily configure and deploy a Lambda function to serve HTTP
requests throughALB,
on a schedule, or in response to a variety of events like anSNSorSQSmessage, S3 event, or customstate machinetransition:importjson,boto3,domovoiapp=domovoi.Domovoi()# Compared to API Gateway, ALB increases the response timeout from 30s to 900s, but reduces the payload# limit from 10MB to 1MB. It also does not try to negotiate on the Accept/Content-Type headers.@app.alb_target()defserve(event,context):returndict(statusCode=200,statusDescription="200 OK",isBase64Encoded=False,headers={"Content-Type":"application/json"},body=json.dumps({"hello":"world"}))@app.scheduled_function("cron(0 18 ? * MON-FRI *)")deffoo(event,context):context.log("foo invoked at 06:00pm (UTC) every Mon-Fri")returndict(result=True)@app.scheduled_function("rate(1 minute)")defbar(event,context):context.log("bar invoked once a minute")boto3.resource("sns").create_topic(Name="bartender").publish(Message=json.dumps({"beer":1}))returndict(result="Work work work")@app.sns_topic_subscriber("bartender")deftend(event,context):message=json.loads(event["Records"][0]["Sns"]["Message"])context.log(dict(beer="Quadrupel",quantity=message["beer"]))# SQS messages are deleted upon successful exit, requeued otherwise.# See https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html@app.sqs_queue_subscriber("my_queue",batch_size=64)defprocess_queue_messages(event,context):message=json.loads(event["Records"][0]["body"])message_attributes=event["Records"][0]["messageAttributes"]# You can colocate a state machine definition with an SQS handler to launch a SFN driven lambda from SQS.returnapp.state_machine.start_execution(**message)["executionArn"]@app.cloudwatch_event_handler(source=["aws.ecs"])defmonitor_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")defmonitor_s3(event,context):context.log("Got an event from S3:{}".format(event))# Set use_sns=False, use_sqs=False to subscribe your Lambda directly to S3 events without forwarding them through an SNS-SQS bridge.# That approach 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,use_sqs=False)defmonitor_s3(event,context):context.log("Got an event from S3:{}".format(event))# DynamoDB event format: https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html@app.dynamodb_stream_handler(table_name="MyDynamoTable",batch_size=200)defhandle_dynamodb_stream(event,context):context.log("Got{}events from DynamoDB".format(len(event["Records"])))context.log("First event:{}".format(event["Records"][0]["dynamodb"]))# Use the following command to log a CloudWatch Logs message that will trigger this handler:# python -c'import watchtower as w, logging as l; L=l.getLogger(); L.addHandler(w.CloudWatchLogHandler()); L.error(dict(x=8))'# See http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html for the filter pattern syntax@app.cloudwatch_logs_sub_filter_handler(log_group_name="watchtower",filter_pattern="{$.x = 8}")defmonitor_cloudwatch_logs(event,context):print("Got a CWL subscription filter event:",event)# 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)defworker(event,context):return{"result":event["input"]+1,"my_state":context.stepfunctions_task_name}Installationpip install domovoiUsageFirst-time setup:domovoi new-projectEdit the Domovoi app entry point inapp.pyusing examples above.Edit the IAM policy for your Lambda function inmy_project/.chalice/policy-dev.jsonto add any permissions it
needs.Deploy the event handlers:domovoi deployTo stage files into the deployment package, use adomovoilibdirectory in your project where you would usechalicelibin Chalice. For example,my_project/domovoilib/rds_cert.pembecomes/var/task/domovoilib/rds_cert.pemwith your function executing in/var/task/app.pywith/var/taskas the working directory. See theChalice docsfor more information on how to set up Chalice configuration.Supported event typesSeeSupported Event Sourcesfor an
overview of event sources that can be used to trigger Lambda functions. Domovoi supports the following event sources:ALB HTTPS requestsSNS subscriptionsSQS queuesCloudWatch Events rule targets, includingCloudWatch Scheduled Events(seeCloudWatch Events Event Examplesfor a
list of event types supported by CloudWatch Events)S3 eventsAWS Step Functions state machine tasksCloudWatch Logs filter subscriptionsDynamoDB stream eventsPossible future event sources to support:Kinesis stream eventsSES (email) eventsAWS Step Functions state machinesDomovoi supports AWS Lambda integration withAWS Step Functions. Step Functions state machines can be started using theStartExecutionmethod or theAPI Gateway Step Functions integration.See thedomovoi/examplesdirectory for examples of Domovoiapp.pyapps using a state machine,
including a loop that restarts the Lambda when it’s about to hit its execution time limit, and a threadpool pattern that
divides work between multiple Lambdas.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-dev.json) to include a statement allowing the “lambda:InvokeFunction” action on all resources, or on the
ARN of the Lambda itself.ConfigurationALBTo use your Lambda as an ALB target with the@alb_target(prefix="...")decorator, you should pre-configure the
following resources in your AWS account:A Route 53 hosted DNS zone such asexample.com., with a domain (example.com) pointing to itAn active (verified/issued) ACM certificate for a DNS name within your DNS zone, such asdomovoi.example.comAfter configuring these, set thealb_acm_cert_dns_nameconfiguration key in the file.chalice/config.jsonto
your DNS name. For example:{
  "app_name": "my_app",
  ...
  "alb_acm_cert_dns_name": "domovoi.example.com"
}Domovoi will automatically create, manage, and link the ALB and DNS record in your Route 53 zone.Dead Letter QueuesTo enable your Lambda function to forward failed invocation notifications todead letter queues, set the configuration keydead_letter_queue_target_arnin
the file.chalice/config.jsonto the target DLQ ARN. For example:{
  "app_name": "my_app",
  ...
  "dead_letter_queue_target_arn": "arn:aws:sns:us-east-1:123456789012:my-dlq"
}You may need to update your Lambda IAM policy (.chalice/policy-dev.json) to give your Lambda access to SNS or SQS.Concurrency ReservationsFor high volume Lambda invocations in accounts with multiple Lambdas, you may need to setper-function concurrency
limitsto partition the overall concurrency
quota and prevent one set of Lambdas from overloading another. In Domovoi, you can do so by setting the configuration
keyreserved_concurrent_executionsin the file.chalice/config.jsonto the desired concurrency reservation. For
example:{
  "app_name": "my_app",
  ...
  "reserved_concurrent_executions": 500
}LinksProject home page (GitHub)Documentation (Read the Docs)Package distribution (PyPI)Change logBugsPlease report bugs, issues, feature requests, etc. onGitHub.LicenseLicensed under the terms of theApache License, Version 2.0.