能否通过SNS触发Lambda实现RDS重启后自动停止(替代CloudWatch Cron)
Perfect, let's build this cost-effective, standardized workflow to auto-stop your RDS instance immediately after it starts—no CloudWatch Cron needed, just SNS triggering Lambda. Here's how to do it step by step:
Solution: Auto-Stop RDS on Startup via SNS + Lambda
Step 1: Create the Lambda Function to Stop RDS
First, we'll build a Lambda function that listens for RDS startup events and stops your target instance.
- Head to the Lambda console, create a new function (Python runtime is ideal here).
- Paste this code (replace
YOUR_TARGET_RDS_INSTANCE_IDwith your actual instance identifier):
import boto3 import json rds_client = boto3.client('rds') TARGET_INSTANCE_ID = 'YOUR_TARGET_RDS_INSTANCE_ID' def lambda_handler(event, context): # Pull the SNS message containing RDS event details sns_message = event['Records'][0]['Sns']['Message'] message_data = json.loads(sns_message) instance_id = message_data['SourceIdentifier'] # Only act on our target instance to avoid accidental stops if instance_id == TARGET_INSTANCE_ID: try: response = rds_client.stop_db_instance(DBInstanceIdentifier=instance_id) print(f"Successfully triggered stop for RDS instance: {instance_id}") return response except Exception as e: print(f"Failed to stop RDS instance: {str(e)}") raise e else: print(f"Ignoring event for non-target instance: {instance_id}") return "No action taken"
- Configure the Lambda's IAM role to include the
rds:StopDBInstancepermission—restrict it to your specific RDS instance ARN for least-privilege security.
Step 2: Set Up RDS Event Notifications to SNS
Next, we need to send RDS startup events to an SNS topic:
- Go to the RDS console, navigate to Event subscriptions in the left sidebar.
- Click Create subscription:
- Name it something descriptive (e.g.,
RDS-Startup-Alert-Topic). - For SNS topic, choose Create topic to make a new one, or use an existing topic if you prefer.
- Under Source type, select DB instances and pick your target RDS instance.
- Under Event categories, check Instance lifecycle and ensure the
RDS DB instance startedevent is selected.
- Name it something descriptive (e.g.,
- Save the subscription—now every time your RDS instance boots up, an event will be sent to this SNS topic.
Step 3: Link SNS Topic to Lambda Function
Finally, connect the SNS topic to trigger your Lambda function:
- Return to your Lambda function's console page.
- Under Configuration > Triggers, click Add trigger.
- Select SNS as the trigger type, then choose the SNS topic you created in Step 2.
- Enable the trigger and save your changes.
Key Notes
- Test It Out: Manually start your RDS instance, then check Lambda's CloudWatch logs to confirm the function ran, and verify the instance stops automatically.
- Error Handling: The sample code includes basic error logging—you can extend it to send failure alerts via SNS if you want to be notified of issues.
- Security: Always stick to least-privilege permissions for your Lambda role to prevent unintended actions on other resources.
内容的提问来源于stack exchange,提问作者 Hemanth
相关产品推荐
相关产品推荐

