Code Monkey home page Code Monkey logo

ec2-start-stop's Introduction

I forked this repository and I added start&stop RDS golang file.

EC2&RDS Start Stop

Implement EC2&RDS scheduled start and stop with Lambda golang function & CloudWatch.

Step

IAM

New Policy

For EC2

IAM -> Policies -> Create policy

Visual editor
Service -> EC2
Actions -> Access level -> Write -> StartInstances,StopInstances
Resources -> All resources

PolicyName -> StartStopEC2Instances

For RDS

IAM -> Policies -> Create policy

Visual editor
Service -> RDS
Actions -> Access level -> Write -> StartDBInstance,StopDBInstance
Resources -> All resources

PolicyName -> StartStopRDSInstances

New Role

For EC2

IAM -> Roles -> Create role

AWS service -> Lambda
Policy -> StartStopEC2Instances

Role name -> StartStopEC2Instances

For RDS

IAM -> Roles -> Create role

AWS service -> Lambda
Policy -> StartStopRDSInstances

Role name -> StartStopRDSInstances

Lambda Function

AWS Lambda -> Functions -> Create function

Name -> StartEC2Instances
Runtime -> Go 1.x
Role -> Existing role -> StartStopEC2Instances
clone this repository
./build.sh
Function code
Function package -> start-ec2-instances.zip
Handler -> start-ec2-instances

Test -> Create new test event
{
  "InstanceRegion": "ap-northeast-2",
  "InstanceIdList":[
    "i-xxxxxxxxxxxxxxxxx",
    "i-xxxxxxxxxxxxxxxxx"
  ]
}

if its a RDS
{
  "InstanceRegion": "ap-northeast-2",
  "InstanceIdList":[
    "{DB_identifier_name}",
    "{DB_identifier_name2}"
  ]
}

CloudWatch

Rules -> Create role -> Schedule

Cron expression -> xxxxx
#e.g.
00 00 ? * MON-FRI *		#9 am(GMT+9) every working day, start ec2 instances
00 10 ? * * *			#19 pm(GMT+9) every day, stop ec2 instances

Targets -> Lambda function -> StartEC2Instances
Constant(JSON text)
{
  "InstanceRegion": "ap-northeast-2",
  "InstanceIdList":[
    "i-xxxxxxxxxxxxxxxxx",
    "i-xxxxxxxxxxxxxxxxx"
  ]
}

if its a RDS
{
  "InstanceRegion": "ap-northeast-2",
  "InstanceIdList":[
    "{DB_identifier_name}",
    "{DB_identifier_name2}"
  ]
}

Reference

AWS lambda go https://github.com/aws/aws-lambda-go

AWS sdk go https://github.com/aws/aws-sdk-go

CloudWatch https://docs.amazonaws.cn/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions

ec2-start-stop's People

Contributors

guce avatar minionsong avatar

Stargazers

Hyunchul Jung avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.