gpt4 book ai didi

database - AWS : Why does my RDS instance keep starting after I turned it off?

转载 作者:太空狗 更新时间:2023-10-30 01:38:49 24 4
gpt4 key购买 nike

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

我的 RDS 日志中有此事件:“数据库实例正在启动,因为它超过了允许停止的最长时间。”

为什么我的 RDS 实例可以停止多长时间有限制?我只想将我的项目搁置几周,但 AWS 不允许我关闭我的数据库?让它闲置的费用为 12.50 美元/月,所以我不想为此付费,而且我当然不希望 AWS 为我启动一个未被使用的实例。

请帮忙!

最佳答案

这是此 new feature 的限制.

You can stop an instance for up to 7 days at a time. After 7 days, it will be automatically started. For more details on stopping and starting a database instance, please refer to Stopping and Starting a DB Instance in the Amazon RDS User Guide.

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

另一种选择是即将推出的 Aurora Serverless它会自动为您停止和启动。在 24/7 全天候运行时,它可能比专用实例更昂贵。

最后,总有Heroku这给你一个 free database instance它在某些限制下自行启动和停止。


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

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。

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

它至少对一个人有用。如有问题请反馈here .

关于database - AWS : Why does my RDS instance keep starting after I turned it off?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com