AWS账单预算在警告违规行为后怎么处理?
创始人
2024-09-28 01:30:55
0

可以使用AWS Budgets的Webhooks功能,在警告阈值被触发时向SNS主题发送一个JSON消息。SNS主题可以通过AWS Lambda函数将消息传递给必要的服务或应用程序,并触发必要的操作,比如停止EC2实例或终止ECS任务。

示例代码:

  1. 创建SNS Topic
import boto3

# set up AWS credentials and create SNS client
session = boto3.Session(profile_name='my_profile')
sns = session.client('sns', region_name='us-east-1')

# create SNS Topic
sns_topic_arn = sns.create_topic(Name='billing_alert_topic')['TopicArn']
  1. 创建Lambda函数
import json
import boto3

def lambda_handler(event, context):

    # extract budget details and alert threshold from JSON message
    budget_details = event['budget']
    alert_threshold = event['alertThreshold']
    
    # check if costs exceeded the alert threshold
    cost_exceeds_threshold = float(budget_details['budgetedAndActualAmounts']['actualAmount']) >= float(alert_threshold)
    
    # if costs exceed threshold, terminate EC2 instance with specified instance id
    if cost_exceeds_threshold:
        instance_id = 'i-12345'
        
        ec2 = boto3.resource('ec2', region_name='us-east-1')
        ec2.Instance(instance_id).terminate()
  1. 创建Webhook

将SNS主题绑定到Budgets预算提醒操作的Webhook中,并指定Lambda函数作为处理程序。

  1. 测试

手动修改预算并使其超过警告阈值,触发Lambda函数并自动在AWS中终止EC2实例。

相关内容

热门资讯

透视规律"epoke... 《透视规律"epoker底牌透视"详细辅助攻略教程(透视挂底牌方法)》 epoker底牌透视软件透明...
透视规律"werpl... 透视规律"werplan外挂"详细辅助必胜教程(破解辅助插件教程);1分钟了解详细教程(微信 136...
透视规律"德普之星a... 透视规律"德普之星app安卓版破解版"详细辅助揭秘攻略(俱乐部辅助器软件);1分钟了解详细教程(微信...
透视美元局"wepo... 透视美元局"wepoker破解器有用吗"详细辅助解说技巧(私人局透视方法辅助);大神普及一款德州ai...
透视了解"hhpok... 透视了解"hhpoker德州机器人"详细辅助可靠技巧(有人用过技巧)1、点击下载安装,微扑克wpk插...
透视实锤"德普之星有... 透视实锤"德普之星有透视辅助吗"详细辅助微扑克教程(免费透视脚本攻略)是一款可以让一直输的玩家,快速...
透视工具"hhpok... 透视工具"hhpoker透视脚本"详细辅助爆料教程(祈福有用攻略);小薇(透视辅助)致您一封信;亲爱...
透视透视"wepok... 透视透视"wepoker破解器激活码"详细辅助存在挂教程(辅助器免费安装技巧);原来确实真的有挂(需...
透视辅助"hhpok... 透视辅助"hhpoker作弊实战视频"详细辅助靠谱教程(模拟器哪个好用透视);支持2-10人实时对战...
透视总结"wepok... 透视总结"wepoker看底牌"详细辅助必赢教程(透视脚本免费辅助)是一款可以让一直输的玩家,快速成...