Coder Social home page Coder Social logo

aws-samples / amazon-rds-auto-restart-protection Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 5.0 78 KB

A plug and play solution to automatically stop your RDS instances or Aurora clusters once restarted by AWS in order not to fall behind maintenance activities.

License: MIT No Attribution

Python 100.00%
aurora rds cost-optimization aws stop automation

amazon-rds-auto-restart-protection's Introduction

Amazon RDS auto-restart protection

This is a plug and play solution to automatically stop your RDS instance once restarted by AWS in order not to fall behind maintenance activities.

Deployment

The solution is deployed using AWS CloudFormation

Keep in mind, application is deployed per region per account.

  1. Create an S3 bucket to upload your artifacts. For more information, see create bucket.
  2. Upload the following files to the root of the newly created S3 bucket:
  • stop-rds-instance-state-machine.json under sources/stepfunctions-code
  • 3 .zip files under sources/lambda-code-deployment-packages

Lambda .py files are also available under sources/lambda-code. For more information on how to create a .zip deployment package, see python package.

  1. In AWS CloudFormation, start deploying deployment/master-template.yaml. For more information, see create stack.
  2. Finally, tag your RDS instance with auto-restart-protection = yes. Instances with the tag, will be automatically stopped once restarted after 7-days.

Configure notifications

The CloudFormation deployment creates an SNS topic SnsTopicWorkFlowNotification to which the AWS StepFunctions state machine publishes the workflow execution notification. Go to the SNS console (or CLI) and subscribe to the topic using SMS, E-mail or else. You'll receive successful as well as failed notifications.

Test your deployment

In order to test the solution, create a test RDS instance, tag it with auto-restart-protection tag and set the tag value to yes. While the RDS instance is still in starting state, test the Lambda function โ€” start-statemachine-execution-lambda with a sample event that simulates that the cluster (RDS-EVENT-0153) or instance (RDS-EVENT-0154) wax started as it exceeded the maximum time to remain stopped.

To invoke a function

  • Sign in to the AWS Management Console and open the Lambda console at https://console.aws.amazon.com/lambda.
  • In navigation pane, choose Functions.
  • In Functions pane, choose start-statemachine-execution-lambda.
  • In the upper right corner, choose Test.
  • In the Configure test event page, choose Create new test event and in Event template, leave the default Hello World option. Enter an Event name and use the following sample event template. Only replace the two <RDS instance id> parameters with the the correct instance id, rest of the parameters are not validated:

A sample Aurora cluster event:

Replace resources, account, SourceIdentifier and SourceArn

{
    "version": "0",
    "id": "a19938cd-14c7-8d2e-9d66-e9db582d2f4d",
    "detail-type": "RDS DB Cluster Event",
    "source": "aws.rds",
    "account": "123456789101",
    "time": "2022-03-07T02:38:03Z",
    "region": "ap-northeast-2",
    "resources": [
        "arn:aws:rds:ap-northeast-2:123456789101:cluster:cluster-name"
    ],
    "detail": {
        "EventCategories": [
            "configuration change"
        ],
        "SourceType": "CLUSTER",
        "SourceArn": "arn:aws:rds:ap-northeast-2:123456789101:cluster:cluster-name",
        "Date": "2022-03-07T02:38:03.747Z",
        "Message": "Finished updating DB parameter group",
        "SourceIdentifier": "cluster-name",
        "EventID": "RDS-EVENT-0153"
    }
}

A sample RDS instance event:

{
    "version": "0",
    "id": "a19938cd-14c7-8d2e-9d66-e9db582d2f4d",
    "detail-type": "RDS DB Instance Event",
    "source": "aws.rds",
    "account": "123456789101",
    "time": "2022-03-07T02:38:03Z",
    "region": "ap-northeast-2",
    "resources": [
        "arn:aws:rds:ap-northeast-2:123456789101:db:database-name"
    ],
    "detail": {
        "EventCategories": [
            "configuration change"
        ],
        "SourceType": "DB_INSTANCE",
        "SourceArn": "arn:aws:rds:ap-northeast-2:123456789101:db:database-name",
        "Date": "2022-03-07T02:38:03.747Z",
        "Message": "Finished updating DB parameter group",
        "SourceIdentifier": "database-name",
        "EventID": "RDS-EVENT-0154"
    }
}

start-statemachine-execution-lambda uses the id parameter as name for the AWS Step Functions execution. The name field is unique for a certain period of time, accordingly, with every test run the id parameter value must be changed.

  • Choose Create and then choose Test. Each user can create up to 10 test events per function. Those test events are not available to other users.
  • AWS Lambda executes your function on your behalf. The handler in your Lambda function receives and then processes the sample event.
  • Upon successful execution, view results in the console.
  • The Execution result section shows the execution status as succeeded and also shows the function execution results, returned by the return statement.

Now, verify the execution of the AWS Step Functions state machine:

To verify an AWS Step Functions state machine execution status:

  • Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/states/home.
  • In navigation pane, choose State machines.
  • In the State machine pane, choose stop-rds-instance-statemachine.
  • In the Executions pane, choose the execution with the *Name *value passed in the test event MessageId parameter.
  • In the Visual workflow pane, the real-time execution status is displayed.
  • Under the Step details tab, all details related to inputs, outputs and exceptions are displayed.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

amazon-rds-auto-restart-protection's People

Contributors

amazon-auto avatar ighanim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

amazon-rds-auto-restart-protection's Issues

Lambda zip upload paths

Hello,

I would like to thank you, for this solution.

Here is a note that I would like to make about a discrepancy I noted while deploying the CloudFormation stack after reading the README.md file given.

StartStateMachineExecutionLambda:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Ref: s3Bucket
        S3Key: start-statemachine-execution-lambda.zip
      Description: Lambda function to start the Step Functions state machine execution
      Handler: lambda_function.lambda_handler
      Environment:
        Variables:
          STEPFUNCTION_ARN:
            Ref: StopRdsInstanceStateMachine
      Role:
        Fn::GetAtt:
        - StartStateMachineExecutionLambdaRole
        - Arn
      Runtime: python3.7
      Tags:
      - Key: Project
        Value: RdsAutoRestartProtection

The s3 key just mentions to upload the lambda zip files at the root of the s3 bucket. While The README.md file asks me to upload the lambda zips and the step function code under the following prefixes.

sources/lambda-deployment-packages
sources/stepfunction-code

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.