项目作者: shungok

项目描述 :
This project provide a sytem structure to stop AWS RDS resources continuously.
高级语言: Python
项目地址: git://github.com/shungok/rds_stopper.git
创建时间: 2019-04-25T11:14:24Z
项目社区:https://github.com/shungok/rds_stopper

开源协议:

下载


Overview

This project provide a sytem structure to stop AWS RDS resources continuously.

Currently(As of 2019-04-25), you can stop DB instances / clusters that are RDS resources, but they will be started automatically after 7 days.

In order to stop the DB instance / cluster continuously under this restriction, it is realized using the following AWS services.

Lambda is used to execute a function to stop a resource that has been tagged to indicate the stop target. And CloudWatch Events is used to invoke this function daily at a.m. 0 (default) in UTC time zone.

The above configuration is built using Serverless Framework.

Project directory structure

  1. rds_stopper
  2. ├── README.md
  3. ├── lambda Lambda function scripts.
  4. └── main.py
  5. └── serverless Serverless Framework settings.
  6. ├── events CloudWatch Events settings.
  7. └── main.yml
  8. └── serverless.yml
  9. 3 directories, 4 files

Runtime environment

verificated runtime envirnment as following.

Serverless Framework

  1. $ sls --version
  2. 1.36.3

Lambda

  • Python version: 3.7.2 (default, Mar 1 2019, 11:28:42)
  • boto3 version: 1.9.42

Usage

1. Deployment of AWS Environment

First time, you have to execute deploy command.

  1. $ cd /path/to/rds_stopper/serverless
  2. $ sls deploy

If you ware changed only lambda function, you will be able to deploy function command.

  1. $ sls deploy function -f main

2. Setting for RDS Resource to stop

Using AWS console / AWS CLI, Add the following tag(Key/Value) to the resource which you want to stop.

  • Key
    • LAMBDA_RDS_STOPPER_TARGET (default)
  • Value
    • True (A string representing ‘true’, for example True/true/1…)

If you would like to change Key name, you can change TARGET_TAG in serverless.yml.

  1. $ cat serverless.yml
  2. TARGET_TAG: LAMBDA_RDS_STOPPER_TARGET

If the RDS resource is a cluster, you have to tag the cluster. Even if you tag individual instances belonging to a cluster, you can not stop them alone.

FAQ

How to invoke lambda function with manual?

basic invokation is as following.

  1. $ sls invoke -f main

If you would like to change the log level, you can set “log_level” in Lambda event data.

  1. $ sls invoke -f main -d '{ "log_level": "DEBUG" }'

valid “log_level” string are as following.

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL