AWS:将事件桥事件转发到加密的SQS(使用Amazon管理的密钥)
创始人
2024-09-28 08:00:24
0

以下是一个使用AWS SDK for Python(Boto3)的示例代码,将Amazon EventBridge事件转发到加密的SQS队列(使用Amazon管理的密钥):

import boto3

def create_event_bridge_rule(source, detail_type, target_arn):
    client = boto3.client('events')
    
    response = client.put_rule(
        Name='my-event-bridge-rule',
        EventPattern={
            'source': [source],
            'detail-type': [detail_type]
        },
        State='ENABLED',
        Description='My EventBridge rule',
    )
    
    response = client.put_targets(
        Rule='my-event-bridge-rule',
        Targets=[
            {
                'Id': '1',
                'Arn': target_arn,
            }
        ]
    )
    
    return response

def create_encrypted_sqs_queue(queue_name):
    client = boto3.client('sqs')
    
    response = client.create_queue(
        QueueName=queue_name,
        Attributes={
            'KmsMasterKeyId': 'alias/aws/sqs',
            'KmsDataKeyReusePeriodSeconds': '300'
        }
    )
    
    return response['QueueUrl']

def add_permission_to_sqs_queue(queue_url, account_id):
    client = boto3.client('sqs')
    
    response = client.add_permission(
        QueueUrl=queue_url,
        Label='event-bridge',
        AWSAccountId=[account_id],
        Actions=['SQS:SendMessage']
    )
    
    return response

# 创建加密的SQS队列
queue_name = 'my-encrypted-sqs-queue'
queue_url = create_encrypted_sqs_queue(queue_name)

# 将事件桥规则与SQS队列关联
source = 'aws.ec2'
detail_type = 'EC2 Instance State-change Notification'
target_arn = queue_url
create_event_bridge_rule(source, detail_type, target_arn)

# 授予事件桥权限发送消息到SQS队列
account_id = boto3.client('sts').get_caller_identity().get('Account')
add_permission_to_sqs_queue(queue_url, account_id)

以上代码示例中,create_event_bridge_rule函数用于创建事件桥规则,create_encrypted_sqs_queue函数用于创建加密的SQS队列,add_permission_to_sqs_queue函数用于向SQS队列添加权限。

在使用代码示例之前,请确保已正确配置AWS CLI或AWS SDK,默认凭证提供程序和区域设置。

相关内容

热门资讯

第十分钟法子!微乐小程序破解,... 第十分钟法子!微乐小程序破解,玄龙辅助工具,科技教程(有挂详细)-哔哩哔哩1)微乐小程序破解辅助挂:...
一分钟资料!福建天天开心辅助器... 一分钟资料!福建天天开心辅助器真的假的,广西老友玩插件,AI教程(有挂透视)-哔哩哔哩1、福建天天开...
2分钟手筋!潇湘游辅助,浙江宝... 2分钟手筋!潇湘游辅助,浙江宝宝游戏辅助器是真的嘛,技巧教程(有挂教学)-哔哩哔哩1.浙江宝宝游戏辅...
1分钟课程!宁夏划水辅助,葫芦... 1分钟课程!宁夏划水辅助,葫芦娃辅助修改器,可靠技巧(有挂技巧)-哔哩哔哩1、每一步都需要思考,不同...
7分钟窍要!土豪辅助,hhpo... 7分钟窍要!土豪辅助,hhpoker辅助实战视频,必备教程(今日头条)-哔哩哔哩1、完成hhpoke...
第7分钟策略!奇迹陕西靖边打锅... 第7分钟策略!奇迹陕西靖边打锅子辅助,好友赣南脚本插件,我来教教你(有挂神器)-哔哩哔哩1、完成奇迹...
第二分钟总结!南宁友乐辅助,新... 第二分钟总结!南宁友乐辅助,新荣耀平台辅助,细节揭秘(新版有挂)-哔哩哔哩1、用户打开应用后不用登录...
第9分钟指引!中至余干六副牌有... 第9分钟指引!中至余干六副牌有挂吗,创思维辅助器,软件教程(有挂分析)-哔哩哔哩1、点击下载安装,中...
3分钟阶段!微信小程序多乐辅助... 3分钟阶段!微信小程序多乐辅助器,掌中乐游戏修改器,新2025版(有挂实锤)-哔哩哔哩1、点击下载安...
第6分钟举措!旺旺福建辅助,顺... 第6分钟举措!旺旺福建辅助,顺欣茶楼怎么开挂,微扑克教程(有挂头条)-哔哩哔哩1、任何顺欣茶楼怎么开...