卓:为什么我的RDS实例关闭后仍能继续启动? [英] AWS: Why does my RDS instance keep starting after I turned it off?

查看:64
本文介绍了卓:为什么我的RDS实例关闭后仍能继续启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS上有一个RDS数据库实例,目前已将其关闭.但是,每隔几天它会自行启动.我现在没有任何其他服务在运行.

I have an RDS database instance on AWS and have turned it off for now. However, every few days it starts up on its own. I don't have any other services running right now.

我的RDS日志中有此事件:数据库实例正在启动,因为它超出了允许的最大停止时间."

There is this event in my RDS log: "DB instance is being started due to it exceeding the maximum allowed time being stopped."

为什么对我的RDS实例可以停止多长时间有限制?我只想将项目搁置几个星期,但是AWS不会让我关闭数据库吗?使其闲置每月需要花费$ 12.50,所以我不想为此付费,并且我当然不希望AWS为我启动一个不被使用的实例.

Why is there a limit to how long my RDS instance can be stopped? I just want to put my project on hold for a few weeks, but AWS won't let me turn off my DB? It costs $12.50/mo to have it sit idle, so I don't want to pay for this, and I certainly don't want AWS starting an instance for me that does not get used.

请帮助!

推荐答案

这是

您一次最多可以停止一个实例7天.7天后,它将自动启动.有关停止和启动数据库实例的更多详细信息,请参阅停止和《 Amazon RDS用户指南》中的启动数据库实例.

您可以设置cron作业以在7天后再次停止实例.您还可以更改为较小的实例大小以节省资金.

You can setup a cron job to stop the instance again after 7 days. You can also change to a smaller instance size to save money.

另一个选择是即将发布的 Aurora Serverless 会自动停止并为您启动.当运行24/7时,它可能比专用实例要昂贵.

Another option is the upcoming Aurora Serverless which stops and starts for you automatically. It might be more expensive than a dedicated instance when running 24/7.

最后,总会有 Heroku ,它会为您提供

Finally, there is always Heroku which gives you a free database instance that starts and stops itself with some limitations.

您还可以尝试将以下以下CloudFormation模板另存为 KeepDbStopped.yml ,然后使用以下命令进行部署:

You can also try saving the following following CloudFormation template as KeepDbStopped.yml and then deploy with this command:

aws cloudformation deploy --template-file KeepDbStopped.yml --stack-name stop-db --capabilities CAPABILITY_IAM --parameter-overrides DB=arn:aws:rds:us-east-1:XXX:db:XXX

确保将 arn:aws:rds:us-east-1:XXX:db:XXX 更改为RDS ARN.

Make sure to change arn:aws:rds:us-east-1:XXX:db:XXX to your RDS ARN.

Description: Automatically stop RDS instance every time it turns on due to exceeding the maximum allowed time being stopped
Parameters:
  DB:
    Description: ARN of database that needs to be stopped
    Type: String
    AllowedPattern: arn:aws:rds:[a-z0-9\-]+:[0-9]+:db:[^:]*
Resources:
  DatabaseStopperFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt DatabaseStopperRole.Arn
      Runtime: python3.6
      Handler: index.handler
      Timeout: 20
      Code:
        ZipFile:
          Fn::Sub: |
            import boto3
            import time

            def handler(event, context):
              print("got", event)
              db = event["detail"]["SourceArn"]
              id = event["detail"]["SourceIdentifier"]
              message = event["detail"]["Message"]
              region = event["region"]
              rds = boto3.client("rds", region_name=region)

              if message == "DB instance is being started due to it exceeding the maximum allowed time being stopped.":
                print("database turned on automatically, setting last seen tag...")
                last_seen = int(time.time())
                rds.add_tags_to_resource(ResourceName=db, Tags=[{"Key": "DbStopperLastSeen", "Value": str(last_seen)}])

              elif message == "DB instance started":
                print("database started (and sort of available?)")

                last_seen = 0
                for t in rds.list_tags_for_resource(ResourceName=db)["TagList"]:
                  if t["Key"] == "DbStopperLastSeen":
                    last_seen = int(t["Value"])

                if time.time() < last_seen + (60 * 20):
                  print("database was automatically started in the last 20 minutes, turning off...")
                  time.sleep(10)  # even waiting for the "started" event is not enough, so add some wait
                  rds.stop_db_instance(DBInstanceIdentifier=id)

                  print("success! removing auto-start tag...")
                  rds.add_tags_to_resource(ResourceName=db, Tags=[{"Key": "DbStopperLastSeen", "Value": "0"}])

                else:
                  print("ignoring manual database start")

              else:
                print("error: unknown database event!")
  DatabaseStopperRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Action:
              - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: Notify
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Action:
                  - rds:StopDBInstance
                Effect: Allow
                Resource: !Ref DB
              - Action:
                  - rds:AddTagsToResource
                  - rds:ListTagsForResource
                  - rds:RemoveTagsFromResource
                Effect: Allow
                Resource: !Ref DB
                Condition:
                  ForAllValues:StringEquals:
                    aws:TagKeys:
                      - DbStopperLastSeen
  DatabaseStopperPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt DatabaseStopperFunction.Arn
      Principal: events.amazonaws.com
      SourceArn: !GetAtt DatabaseStopperRule.Arn
  DatabaseStopperRule:
    Type: AWS::Events::Rule
    Properties:
      EventPattern:
        source:
          - aws.rds
        detail-type:
          - "RDS DB Instance Event"
        resources:
          - !Ref DB
        detail:
          Message:
            - "DB instance is being started due to it exceeding the maximum allowed time being stopped."
            - "DB instance started"
      Targets:
        - Arn: !GetAtt DatabaseStopperFunction.Arn
          Id: DatabaseStopperLambda

它已经为至少一个人工作了.如果您有问题,请在此处报告.

It has worked for at least one person. If you have issues please report here.

这篇关于卓:为什么我的RDS实例关闭后仍能继续启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆