You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过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.

  1. Head to the Lambda console, create a new function (Python runtime is ideal here).
  2. Paste this code (replace YOUR_TARGET_RDS_INSTANCE_ID with 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"
  1. Configure the Lambda's IAM role to include the rds:StopDBInstance permission—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:

  1. Go to the RDS console, navigate to Event subscriptions in the left sidebar.
  2. 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 started event is selected.
  3. Save the subscription—now every time your RDS instance boots up, an event will be sent to this SNS topic.

Finally, connect the SNS topic to trigger your Lambda function:

  1. Return to your Lambda function's console page.
  2. Under Configuration > Triggers, click Add trigger.
  3. Select SNS as the trigger type, then choose the SNS topic you created in Step 2.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:28:41