CRON job using AWS Lambda to Automate Starting & Stopping of EC2 Instances.

CRON job using AWS Lambda to Automate Starting & Stopping of EC2 Instances.

?? Welcome Back ?? !! This Article is in Continuation of My previous Article where I've Explained AWS lambda & Serverless Framework . Now It's Time to Build a Project with Real World Example.

?? Let's Directly Dive into the Practical cause there's no Prerequisite as far as the Previous Article is Concerned.

-----------------------------------------------------------------------------

???This Article will Cover the Following Topics?:

  • Python Runtime.
  • IAM Permissions to stop start EC2 Instances,
  • Function Timeouts & Memory
  • CloudWatch Events.
  • Testing the Function

-----------------------------------------------------------------------------

?? Let's first Create a Project using sls command.

No alt text provided for this image

?? AWS - Python - Started Project we'll Go with.

No alt text provided for this image

?? Name for the Project is python-ec2-start-stop

?? We are not Deploying this Template right now to the Lambda Service cause we have to De Some changes according to Our Requirements. So change Directory to python-ec2-start-stop and use code . command to open VS Studio Code where, we'll Walk Through the Serverless.yml file and Python File.

-----------------------------------------------------------------------------

??Building Serverless.yml :

No alt text provided for this image

?? As you can see we are inside the Project Directory.

?? The Serverless.yml is file where all the Configurations About Lambda Functions are Stored. Let's Break down this file step by step and make it easy to Understand.

No alt text provided for this image

? Here the Service is Nothing but the Name of the Project which we've created and provied being AWS since we are gonna use AWS lambda function, the runtime is the interpreter which we select while creating the Lambda Function on AWS console, so in this case its python3.9 & the Region for data center is set as ap-south-1.

No alt text provided for this image

? the profile is actually the IAM role which we created to configure our serverless in the previous Article, so make sure you choose the correct name, memory size is that Lambda allocates CPU power in proportion to the amount of memory configured. By default, your functions have 128 MB of memory allocated. You can increase that value up to 10 GB. With more CPU resources, your Lambda function's duration might decrease.

? As we know that using the IAM roles access, the user can have control over the whole services based upon the authorities Allocated. So Here in the iamRoleStatements: the effect means we are allowing access on whole EC2 service which is specified through Action : EC2:*, Here Asterisk specifies all Services of EC2 Instance.

? Resources are the Functionalities available with EC2 Instances like list, Describe, Start, Stop Instances, etc.

No alt text provided for this image

?? Now This is Very Important & Interesting.

?? The Function over here is same as the one which we get by default after creating A Lambda Function.

?? We Have 2 Functions being start-ec2 and stop-ec2, what the handler does is it goes to the handler.py file which you get when you create the project using sls command. and inside this handler file, it will go to the function name specified after handler. <function name>. So in this case if we have a Look ta the handler.py file,

No alt text provided for this image

?? Here You can see we have two function and we are referring start_ec2 with start-ec2 and stop_ec2 with stop-ec2 inside our serverless.yml file.

Coming back to the Serverless.yml file

No alt text provided for this image

?? The next we have is timeout Just like memorySize, the default value of the timeout (in seconds) can be set in the provider, and custom timeouts for individual functions can be specified in the functions section.

?? If you don't specify either the global or custom timeouts, the default value is 6 seconds.

?? Now It's time for events where the Actually working of Lambda Function will Take Place.

No alt text provided for this image

This examples defines two functions,?rateHandler?and?cronHandler, both of which are triggered by an event of?schedule?type, which is used for configuring functions to be executed at specific time or in specific intervals. For detailed information about?schedule?event, please refer to corresponding section of Serverless?docs.

When defining?schedule?events, we need to use?rate?or?cron?expression syntax.

value?- A positive number

unit?- The unit of time. ( minute | minutes | hour | hours | day | days )

Cron expressions syntax:

No alt text provided for this image

All fields are required and time zone is UTC only.

Field Values WildcardsMinutes0-59, - * /Hours0-23, - * /Day-of-month1-31, - * ? / L WMonth1-12 or JAN-DEC, - * /Day-of-week1-7 or SUN-SAT, - * ? / L #Year192199, - * /

In below example, we use?cron?syntax to define?schedule?event that will trigger our?cronHandler?function every second minute every Monday through Friday

No alt text provided for this image

So According to Our Example :

No alt text provided for this image

-----------------------------------------------------------------------------

??Handler.py :

No alt text provided for this image

?? Pretty Straightforward. As usual the Minimum requirement is to have boto3 library as we use in ansible too to connect with ec2 service for usage.

?? A start and Stop Function, & what they do is they get the EC2 Instance IDs through get_all_ec2_ids() Function which is at the Very Last of the handler file as you can see. Then they either Start or Stop them based on their Functionalities.

?? Finally when the get_all_ec2_ids() is the API call to AWS and get the instance ID back using return instances.

?? Now we Just have to Deploy the Function or project to the Lambda Service. So use sls deploy command.

No alt text provided for this image

?? So without any Error the Function is Deployed. Now lets check through the AWS management console :

No alt text provided for this image

?? So we have two functions to Start and Stop the EC2 instances Created. and Now Let's Test Them.

No alt text provided for this image

?? Here we've got Error Message because as per the Functionality, the Instances ID needs to be Specified but how can I prove that the Function will get Triggered by the Cloud Watch or Not ?

No alt text provided for this image

?? Now Go to the CloudWatch Service and then to events where we have rules.

No alt text provided for this image
No alt text provided for this image

?? Here you can see we have two rules and these are Start and Stop Rules which will Trigger The Lambda Function. So if we go to one of them.

No alt text provided for this image

?? Here you can see we have the Time Scheduled as per the cron for each Day. & Similarly for Stop :

No alt text provided for this image

?? So every Day the Instances will Start at 13.00 am GMT and stop at 21:00 GMT.

?? So the only requirement is to Specify the EC2 Instanced ID when provisioned.

-----------------------------------------------------------------------------

???So From the Above Article we learnt how to implement CRON job using AWS Lambda to Automate Starting & Stopping of EC2 Instances. If you Find This Interesting then Do Follow & Connect???.

THANK YOU !!

-----------------------------------------------------------------------------



Vimal Daga

World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC

2 年
Pintu D

Senior DevOps Engineer at Informatica || 2×Redhat Certified Engineer (RHCE,DO180) || Python || C++ || Linux || Shell Scripting || Docker/Podman || Kubernetes || Git & GitHub || Ansible || Jenkins || Terraform || AWS

2 年

Very informative ??

Ritesh Chaudhari

DevSecOps Engineer

2 年

Very useful!!

Gautam S. M

?? Python Full Stack Developer | ex-TCSer ?? 4x Microsoft Certified ??1x Postman & GraphQL Certified </> React & Vue.js| FAST API | MYSQL | Mongodb | Leader in Predictive Analytics & Infrastructure Optimization

2 年

Good work Swaroop Shinde From my knowledge after seeing your code i figured out that your code is fetching all the instances id and does starting and stopping operations in the particular time intervals as you provided. Nice research ??

要查看或添加评论,请登录

Swaroop Shinde的更多文章

社区洞察

其他会员也浏览了