gpt4 book ai didi

amazon-web-services - 如何为具有 lambda 函数目标的 cloudwatch 事件制作 cloudformation 模板?

转载 作者:行者123 更新时间:2023-12-03 07:14:23 26 4
gpt4 key购买 nike

我不想使用 aws 接口(interface),而是想将我的 cloudwatch 事件和函数编写为堆栈。然而,我在如何找出这个 cloudformation 堆栈的模板时遇到了麻烦。 aws 指南显示了示例,但我没有找到任何涉及 cloudwatch 事件语法的内容,有什么帮助吗?这是事件和 lambda:

{
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"CreateBucket"
]
}
}

lambda :

import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
# Get bucket name from the S3 event
print(event)

bucket_name = event['detail']['requestParameters']['bucketName']

# Create a bucket policy
bucket_policy =json.dumps({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "MustBeEncryptedAtRest",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"Resource": [
"arn:aws:s3:::{}".format(bucket_name),
"arn:aws:s3:::{}/*".format(bucket_name)
],
"Condition": {
"StringNotEquals": {
"s3:x-amz-server-side-encryption": [
"AES256",
"aws:kms"
]
}
}
},
{
"Sid": "MustBeEncryptedInTransit",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::{}".format(bucket_name),
"arn:aws:s3:::{}/*".format(bucket_name)
],
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
}
} ] })


# Set the new policy
s3.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)

最佳答案

下面是对 aws-events-rule 中的示例稍加修改的示例CloudFormation 文档。

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"LambdaFunction": .......
"EventRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "EventRule",
"EventPattern": {
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"CreateBucket"
]
}
},
"State": "ENABLED",
"Targets": [{
"Arn": {
"Fn::GetAtt": ["LambdaFunction", "Arn"]
},
"Id": "TargetFunctionV1"
}]
}
}
}
}

关于amazon-web-services - 如何为具有 lambda 函数目标的 cloudwatch 事件制作 cloudformation 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62878745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com