AWS SNS SNS send email & CloudWatch & Lambda function

2 篇文章 0 订阅

AWS发邮件:
进入SNS服务,创建topic --> subscribe to topic --> protocol 选email,填写自己的email。完。

CloudWatch:

  1. Metric —> ec2 --> Per Instance Metrics–> search metrics for example: CPUUtilization --> Tag : Graph Metrics --> select the BELL --> input info and select the SNS topic.
  2. Event --> rules --> create rules --> schedule(定时) OR Event Pattern (如选取ec2stop了发消息)–> Target–> select the SNS topic–> save.

Lambda :

  1. create function --> deploy -->run Test
  2. If show permission error–> Configure–> Permission —> add policy, like sns/ec2 permissions
  3. Trigger --> select CloudWatch–>rule/metric
    Below is the code:
import json
import boto3

def lambda_handler(event, context):
    # TODO implement
    ec2_cli = boto3.client('ec2','us-west-2')
    f_prod_bk = {'Name': 'tag:Prod', 'Values':['Backup', 'backup'] }
    
    paginator = ec2_cli.get_paginator('describe_volumes')
    list_val_ids = []
    for page in paginator.paginate(Filters=[f_prod_bk]):
        print(page)
        for vol in page['Volumes']:
            list_val_ids.append(vol['VolumeId'])
    
    print('Volumes Ids: {}'.format(list_val_ids))
    
    snap_ids = []
    for e in list_val_ids:
        print(e)
        res = ec2_cli.create_snapshot(
            Description="Create snap",
            VolumeId = e,
            TagSpecifications=[
                {
                    'ResourceType': 'snapshot',
                    'Tags': [
                        {
                            'Key': 'Delete-on',
                            'Value': '90'
                        },
                    ]
                },
            ],
        )
        print(res)
        snap_ids.append(res.get('SnapshotId'))
    print(snap_ids)
    
    waiter = ec2_cli.get_waiter('snapshot_completed')
    waiter.wait(SnapshotIds=snap_ids)
    
    sns_cli = boto3.client('sns','us-west-2')
    sns_cli.publish(TargetArn='arn:aws:sns:us-east-2:701808552826:topic_send_email', Message='Snapshot has been created')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值