Skip to content

devops-made-easy/ec2-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EC2 CRON

SonarCloud || AWSSAM

Features

  • Allows you to schedule a CRON job(cloudwatch rule) to perform START, STOP or REBOOT for EC2 INSTANCES for given tag key and its value.

Architecture Diagram

High Levl Architecture

How to Deploy

Step 1: Go to the AWSSAM

Step 2: Enter Values for the following as per your needs

  • Application name [ Stack Name of the application created via AWS Cloudformation]
  • EC2CronFunction [ Name of the Lambda Function Name ]
  • Action [Supported Values are STOP, START or REBOOT - This is Action that will be performed on fitlered ec2 instances]
  • CronSchedule [ Cron Schedule for CloudWatch Event Refer: Link for syntax help ]
  • TagKey [ Name of the Tag Key that you want to use in filter]
  • TagValue [ Value of the Tag Key that you want to use in filter]

Step 3: Select the check box "I acknowledge that this app creates custom IAM roles" and Click on Deploy

Resources that get created with this deployment are as follows:

  1. Lambda Function
  2. Cloud Watch Event
  3. CloudWatchEventPermission
  4. IAM Role with default AWSLambdaBasicExecutionRole Policy and below custom policy Policy
{
    "Statement": [
        {
            "Action": [
                "ec2:RebootInstances",
                "ec2:StartInstances",
                "ec2:StopInstances"
            ],
            "Resource": "arn:aws:ec2:us-east-2:013472794367:instance:*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "ec2:DescribeInstances"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

Local Development

To use the SAM CLI, you need the following tools.

To build and deploy your application for the first time, run the following in your shell:

sam build --use-container
sam deploy --guided
AWS$ sam local invoke --env-vars env.json

License Summary

This sample code is made available under MIT license. See the LICENSE file.

Share the Love

Like this project? Please give it a ★ on our GitHub! it helps us a lot

Contributing

Have an idea for a feature to enhance this serverless application? Open an issue or pull request !