I decided to play around with AWS Lambda to see if it help out to monitor the status of containers.

Creating a Lambda Function

The easiest thing to do is to create a sample function. Once logged into AWS go to the Lambda Service section and you should see a list of created lambda functions:


The simplest to do is to just click Create Function and choose a language of your preference:


I decided to use Python 3.6, since that was the language I was most comfortable with. Then for the role, I decided to use an Existing Role of lambda_basic_execution:


After that it will take you to the next page, when you can modify what the function does how it’s triggered:


As quick test, I decided to just see how the events look like. So I created a very simple function that just prints out the event that is sent to the function:

import json

def lambda_handler(event, context):
    if event["source"] != "aws.ecs":
        raise ValueError("Function only supports input from events with "
                         "a source type of: aws.ecs")
    print("event looks like this {}".format(event))

That should be enough to get started.

Create Rule in CloudWatch to Trigger your Lambda Function

Now what we can do is create a rule in CloudWatch to call our function with the event. So in AWS go to CloudWatch Service Section and then navigate to Events -> Rules, and you should see a list of configured rules:


Let’s start by clicking on Create Rule and then choose the following settings:

  • Event Source -> Event Pattern
  • Service Name -> EC2 Container Service
  • Event Type -> State Change
  • Detail Type -> Any
  • Cluster -> Any
  • Target -> Your_Function


Also if you scroll down, you can see how the events will look like when sent to your function:


Then if you go to the next page, you ran give your rule a new name and save it:


Then you should be getting the new events on your function.

Testing Out your Lambda Function

Before you enable the cloudwatch events you can also test your function manually. If you go back to your function from the Lambda Service section in AWS, on your top right you can mimic events that are sent to your function. Now that we saw how the events will look like (while we were creating the above rule). Let’s copy those json examples and mimic sending those over. So click on drop down next to the Test button and then choose configure test events:


Then create a new event, give it a name, and paste one of the json examples:


Then after it’s created you can choose it from the drop down list and click test and you will see how the results look like:


Since I was planning on sending a message to a slack channel, I wasn’t returning the actual message, but if there was another function that was parsing the output of this lambda function I would just return that as a string.

Checking out your Lambda Function Metrics

You can checkout how many times the function has been called by clicking Monitoring:


You will notice from the above output that the CI environment (where I was initially testing), does a LOT of task restarts. We can then click Jump to Logs and you will see the output of each call:


that’s it for now.