如何使用 lambda 函数目标为 cloudwatch 事件制作 cloudformation 模板?
How do I make a cloudformation template for a cloudwatch event with a lambda function target?
我不想使用 aws 界面,而是想编写我的 cloudwatch 事件并将其用作堆栈。但是我在如何找出这个 cloudformation 堆栈的模板时遇到了麻烦。 aws 指南显示了示例,但我没有找到任何处理 cloudwatch 事件语法的内容,有帮助吗?这是事件和 lambda:
{
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"CreateBucket"
]
}
}
拉姆达:
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"
}]
}
}
}
}
我不想使用 aws 界面,而是想编写我的 cloudwatch 事件并将其用作堆栈。但是我在如何找出这个 cloudformation 堆栈的模板时遇到了麻烦。 aws 指南显示了示例,但我没有找到任何处理 cloudwatch 事件语法的内容,有帮助吗?这是事件和 lambda:
{
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"CreateBucket"
]
}
}
拉姆达:
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"
}]
}
}
}
}