Cron Job AWS Lambda Functions & How to Schedule Tasks

Cron jobs Are Usually Used to schedule the commands at a specific time. We use them to schedule different tasks like Taking backups scheduling instance Running, Monitoring the Status of system or system Maintenance tasks. Cron jobs are very helpful when you are administering a system in the cloud.

Steps:

1. Launch Ec2 instances

      Create Ec2 Instance 

2. Creating IAM Policy 

       Create the IAM policy for start and Stop the Instance.

       Put Json Code 

   {

  “Version”: “2012-10-17”,

  “Statement”: [

    {

      “Effect”: “Allow”,

      “Action”: [

        “logs:CreateLogGroup”,

        “logs:CreateLogStream”,

        “logs:PutLogEvents”

      ],

      “Resource”: “arn:aws:logs:*:*:*”

    },

    {

      “Effect”: “Allow”,

      “Action”: [

        “ec2:Start*”,

        “ec2:Stop*”

      ],

      “Resource”: “*”

    }

  ]

}

3. Creating IAM Role 

During Creating the IAM Role Select the Lambda Function instead of Ec2.Then Attached the IAM policy You created in Step 2.

4. Creating Lambda Function 

During Creating the Lambda function Select “Author from scratch”

Put the Function name and Select the Python 3.7 from Runtime. Choose an Existing Role in Permission.

You have to create two Lambda Function first for stopping and second for starting.

The code for both is below and You have to change region with your region in which instance is running. Put the Instance IDs.

Stop the Ec2 Instance 

import boto3

region = ‘Region’

instances = [‘i-12345’, ‘i-2345’]

ec2 = boto3.client(‘ec2’, region_name=region)

def lambda_handler(event, context):

ec2.stop_instances(InstanceIds=instances)

print(‘stopped your instances: ‘ + str(instances))

Starts the EC2 instances 

import boto3 region = ‘Region’ instances = [‘i-12345’, ‘i-2345’] ec2 = boto3.client(‘ec2’, region_name=region) def lambda_handler(event, context): ec2.start_instances(InstanceIds=instances)  print(‘started your instances: ‘ + str(instances))

When You create the function, You Can test These Two functions and These will stop and Start the instance. 

5. Cloud Watch 

It will also Create the Logs in Cloud watch

You have to stop and start the instance Manually. Now to do it automatically 

Create the Rule in events in Cloud watch.

Now You need to learn How to put the value in the Cron expression

 Learn About CloudWatch Events 

 After Putting the Cron Expression we add the Target

In Target we will select Lambda and will select start and stop Instance. Like in the Given Fig we have chosen 7:00 Pm and we want Our instance should be stopped at that time so we will select Stop instance. 

If You want to get notification you may add SNS as well.

Now the Same Procedure we will Repeat for Start Instance.

After Doing This You will see Your instances will stop Automatically at 7:00 PM and then will start at 7:00 AM Because Lambda Will trigger the Event at fixed Time.

Conclusion 

By using Cron Jobs of Lambda function we can schedule the instance start and stop state. We can schedule the terminating and stopping state. It helps us to save the cost when we are not working, we can stop the resources and when we are working, we Can Restart the Resources.