Home

Awesome

Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at

http://aws.amazon.com/apache2.0/

or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Lambda ECS Worker Pattern

This code example illustrates the concept of using Amazon ECS Tasks to extend the functionality of AWS Lambda.

In this pattern, an AWS Lambda function is triggered by an Amazon S3 event. The AWS Lambda function then pushes the event data into an Amazon SQS queue and starts an Amazon ECS Task. A simple shell script running inside the Amazon ECS Task’s container fetches the message from the Amazon SQS queue and processes it.

You can read more about this pattern in the blog post: Better Together: Amazon ECS and AWS Lambda

Architecture overview of the Lambda ECS Worker Pattern

As a demo, we use this pattern to implement a ray-tracing worker using the popular open source POV-Ray ray-tracer, triggered by uploading a POV-Ray scene description wrapped into a .ZIP file in an Amazon S3 bucket. Running a ray-tracer inside AWS Lambda would probably take more than the limit of 60 seconds to complete, so we use Lambda to push the Amazon S3 Notification data into an Amazon SQS queue and start an Amazon ECS Task for the actual processing. The shell script running in the Amazon ECS Task’s container takes care of fetching the input data from Amazon S3, running the POV-Ray ray-tracing software and uploading the result image back into the Amazon S3 bucket.

Files

The following files are included in this repository:

Prerequisites

How to setup

The fabfile.py Python script included in this repository comes with all commands necessary to set up and run this sample application. You can examine the script to identify individual steps, then run <code>fab <name of function></code> individually, or you can simply run <code>fab setup</code> to get everything set up for you.

Here’s how to set up:

# 1. Clone this repository into a local directory.

git clone https://github.com/awslabs/lambda-ecs-worker-pattern.git

# 2. Edit config.py and use your own values there for your name and email address, DockerHub account details,
#    AWS region, SSH key name and other details.

# 3. Make sure you have Python2 installed. Depending on your installation,
#    you may need to use the pip2 command below, or you can use the plain pip command if it points to the Python2
#    version of PIP.

sudo pip2 install -r requirements.txt

# 4. Now, configure the AWS CLI if you haven’t already. This script will use your AWS credentials and other
#    information from your AWS CLI configuration profile(s).

fab setup

#    At some stage, the script will ask you for your DockerHub login name and password, in order to upload a docker
#    image to DockerHub and configure your ECS cluster to access your DockerHub private repository.

# 5. Take a note of the bucket name mentioned at the end of the setup.

# 6. Go to the AWS console, upload ECSLogo.zip to this bucket and wait a minute.

# 7. Check the Lambda logs for any errors. If everything goes well, you should see a .PNG image in the same
#    bucket a few minutes after uploading.

Enjoy!