AWS Lambda

Lab Goal

Figure 1. Lab goal

In this lab, we will try to create an AWS lambda function that will be trigerred by a time-based CloudWatch event. The lambda function will have an IAM role attached to it for access of stopping EC2 instances.

Create Lambda Function

From the main dashboard, go to Services > Lambda. Then, click on the Create function button.

Figure 1. AWS lambda dashboard

Set the function configuration as below. It will give this function a role to stop EC2 instances. Click create function.

Figure 2. AWS Lambda function configuration

Configure the Trigger

Now the idea is to set a scheduled event trigger for this lambda function from CloudWatch event. Click on Add Trigger.

Figure 3. myStopinator details

Under Select a trigger, choose CloudWatch Events. Create a new rule and configure it as below. Then Click Add.

Figure 4. Trigger configuration

Configure the Lambda Function

Under the Designer box, click myStopinator. You can start to edit the code for this function under the Function code box. Use this following code for the purpose of stopping the assigned instance.

import boto3
region = '<REPLACE_WITH_REGION>'
instances = ['<REPLACE_WITH_INSTANCE_ID>']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

The <REPLACE_WITH_REGION> tag should be changed to the current region your AWS Lambda is running on. The <REPLACE_WITH_INSTANCE_ID> tag should also be changed to the instance ID which you would want to stop. You can easily find instance ID in the EC2 dashboard under Instances. This is what the Lambda function code for mine. Click Save.

Figure 5. myStopinator lambda function

Click ont the Monitoring tab and you will be able to see the status of the Lambda function.

Figure 6. AWS Lambda function monitoring

Verify Lambda Function

Now, to check if the Lambda function works fine. Go to EC2 dashboard and check if the EC2 instance you have assigned to the lambda function is stopped.

Figure 7. Instance stopped

As we have set in the Lambda function configurations, it will run based on rate expressions which was set every 1 minute. Thus, the stopinator function will run every one minute and stops the instance every time the code is run.

So, even if we try to start the instance again, once the 1 minute event from CloudWatch comes in, it will trigger the myStopinator lambda function to stop the instance again.

A More Real World Approach

You should have notice that there are a lot of potential in using AWS Lambda here. It eases automations of things without the need of an instance as it would suggest the word serverless computing. In a simpler concept, we can easily use AWS Lambda functions to schedule some actions such as back up or taking snapshots. In addition to that, you use AWS Lambda for checking a certain condition of an instance or a data then further processed in a certain result or even another lambda function.

AWS Lambda shows its great capabilities if combined with other services in AWS. Two of the complementary services would be AWS CloudWatch and AWS Step Functions. As we have seen through this lab, AWS CloudWatch events provide periodical or scheduled trigger for the function to run. This is will be benificial as it there will be a lot of automated tasks that are needed to be done on a certain time and these triggers will certainly help.

AWS Step Functions is a powerful tool to optimize a serverless workflows that could combine several services in AWS such as AWS Lambda, AWS Fargate and Amazon SageMaker. This service provide a simple and intuitive application development by translating your workflow into a state machine diagram. Thus, it provides an better visualization of the workflow and able to be managed easily without changing any code.

The lab we did was just a small and really simple example on how AWS Lambda works. As we do not have a lot of permissions to certain actions in the lab, it poses some restriction to me to make an example properly. However, a concept that could be implemented would be implementing a scheduled snapshot capture for certain instances. In addition to that, a deletion of snapshots after certain days would also be effective in one of this implementation.

Now imagine using AWS Step Functions with AWS Lambda regarding creating snapshots. In a certain workflow, you could create a flow of lambda functions from adding tags, counting the number of snapshots, creating the snapshot, deleting the snapshot if it exceeds certain number and of course backing up the snapshot to another region.

Leave a comment