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,默认凭证提供程序和区域设置。

相关内容

热门资讯

玩家必备科普!hhpoker到... 玩家必备科普!hhpoker到底可以作弊吗(辅助挂)切实存在有挂(确实有挂辅助app)一、可以开透视...
2024教程!wepoker透... 2024教程!wepoker透视脚本免费使用视频(辅助挂)好像有挂(有挂分享辅助攻略)1、模拟器是什...
分享实测!wepoker可以免... 分享实测!wepoker可以免费玩吗(辅助挂)总是存在有挂(存在有挂辅助方法)1、不需要AI权限,帮...
玩家必备教程!哈糖大菠萝攻略(... 玩家必备教程!哈糖大菠萝攻略(辅助挂)竟然存在有挂(证实有挂辅助器)1、免费辅助多个强度级别选择2、...
今日百科!德州局怎么透视(辅助... 今日百科!德州局怎么透视(辅助挂)切实有挂(的确有挂辅助器)1、让任何用户在无需安装教程第三方神器的...
热点推荐!wepoker游戏下... 热点推荐!wepoker游戏下载(辅助挂)切实是有挂(有挂细节辅助脚本)进入游戏-大厅左侧-新手福利...
我来教大家!werplan有挂... 我来教大家!werplan有挂吗(辅助挂)本来是真的有挂(有挂解惑辅助app)1、打开软件启动之后找...
终于知道!wpk作弊(辅助挂)... 终于知道!wpk作弊(辅助挂)本来真的是有挂(有挂方略辅助技巧)1、进入游戏-大厅左侧-新手福利-激...
普及知识!wepoker私人局... 普及知识!wepoker私人局开挂视频(辅助挂)都是真的有挂(有挂解密辅助插件)1、实时透视辅助更新...
揭秘攻略!pokemmo手机版... 揭秘攻略!pokemmo手机版脚本(辅助挂)本来存在有挂(有挂功能辅助插件)脚本下载中分为三种模型:...