Lambda 函数基于使用 Boto3 的标签筛选器停止所有区域中的 RDS 实例
Lambda Function to Stop RDS-Instances in all Regions Based on a Tag Filter Using Boto3
我已将此代码放在一起以停止所有 aws 区域中的 rds 实例。目前,此代码仅停止我当前默认区域中的实例。我在这里缺少什么吗?
标签:
这些实例首先被一个标签过滤,标签的 Key = ttl 和 Value = some date
筛选:
过滤器 returns 仅标记值小于今天的实例。
import boto3
from datetime import datetime
current_date = datetime.today().strftime('%Y-%m-%d')
available_regions = boto3.Session().get_available_regions('rds')
def lambda_handler(event, context):
for region in available_regions:
rds = boto3.client('rds', region_name=region)
# get all instances
instances = rds.describe_db_instances()
stopInstances = []
# Locate all instances that are tagged for stop based on date.
for instance in instances["DBInstances"]:
# Example RDS Instance tags:
tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
for tag in tags["TagList"]:
if tag['Key'] == 'ttl' or tag['Key'] == '' :
if tag['Value'] < current_date:
stopInstances.append(instance["DBInstanceIdentifier"])
rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])
pass
pass
# print if instances will stop.
if len(stopInstances) > 0:
print ("stopInstances")
else:
print ("No rds instances to shutdown.")
此代码现在有效:
import boto3
from datetime import datetime
# Get current time in format yyyy-mm-dd
# define boto3 the connection
current_date = datetime.today().strftime('%Y-%m-%d')
# get all regions
available_regions = boto3.Session().get_available_regions('rds')
def lambda_handler(event, context):
for region in available_regions:
rds = boto3.client('rds', region_name=region)
# Define instances
instances = rds.describe_db_instances()
stopInstances = []
# Define and locate tags.
for instance in instances["DBInstances"]:
tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
for tag in tags["TagList"]:
if tag['Key'] == 'ttl' or tag['Key'] == '' :
if tag['Value'] < current_date:
# Stop instances matching a tag pattern
stopInstances.append(instance["DBInstanceIdentifier"])
rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])
pass
pass
# print all instances that will stop.
if len(stopInstances) > 0:
print ("stopInstances")
else:
print ("No rds instances to shutdown.")
我已将此代码放在一起以停止所有 aws 区域中的 rds 实例。目前,此代码仅停止我当前默认区域中的实例。我在这里缺少什么吗? 标签: 这些实例首先被一个标签过滤,标签的 Key = ttl 和 Value = some date 筛选: 过滤器 returns 仅标记值小于今天的实例。
import boto3
from datetime import datetime
current_date = datetime.today().strftime('%Y-%m-%d')
available_regions = boto3.Session().get_available_regions('rds')
def lambda_handler(event, context):
for region in available_regions:
rds = boto3.client('rds', region_name=region)
# get all instances
instances = rds.describe_db_instances()
stopInstances = []
# Locate all instances that are tagged for stop based on date.
for instance in instances["DBInstances"]:
# Example RDS Instance tags:
tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
for tag in tags["TagList"]:
if tag['Key'] == 'ttl' or tag['Key'] == '' :
if tag['Value'] < current_date:
stopInstances.append(instance["DBInstanceIdentifier"])
rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])
pass
pass
# print if instances will stop.
if len(stopInstances) > 0:
print ("stopInstances")
else:
print ("No rds instances to shutdown.")
此代码现在有效:
import boto3
from datetime import datetime
# Get current time in format yyyy-mm-dd
# define boto3 the connection
current_date = datetime.today().strftime('%Y-%m-%d')
# get all regions
available_regions = boto3.Session().get_available_regions('rds')
def lambda_handler(event, context):
for region in available_regions:
rds = boto3.client('rds', region_name=region)
# Define instances
instances = rds.describe_db_instances()
stopInstances = []
# Define and locate tags.
for instance in instances["DBInstances"]:
tags = rds.list_tags_for_resource(ResourceName=instance["DBInstanceArn"])
for tag in tags["TagList"]:
if tag['Key'] == 'ttl' or tag['Key'] == '' :
if tag['Value'] < current_date:
# Stop instances matching a tag pattern
stopInstances.append(instance["DBInstanceIdentifier"])
rds.stop_db_instance(DBInstanceIdentifier=instance["DBInstanceIdentifier"])
pass
pass
# print all instances that will stop.
if len(stopInstances) > 0:
print ("stopInstances")
else:
print ("No rds instances to shutdown.")