【问题标题】:How do I make a cloudformation template for a cloudwatch event with a lambda function target?如何为具有 lambda 函数目标的 cloudwatch 事件制作 cloudformation 模板?
【发布时间】:2020-11-02 19:40:07
【问题描述】:

我想编写我的 cloudwatch 事件并用作堆栈,而不是使用 aws 接口。但是我在如何找出这个 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)

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-cloudwatch amazon-cloudwatch-events


    【解决方案1】:

    以下是根据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"
                    }]
                }
            }
        }
    }
    

    【讨论】:

    • 这样做会给我错误:“模板格式错误:必须定义至少一个资源成员。”我改错了吗?我也将其保存为 .json
    • 这是因为它需要添加到“资源”部分。我已经更新了答案以证明它的去向。您还需要创建一个 LambdaFunction 资源来创建上面的 Lambda:docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
    • 在哪里可以获得roleruntime 所需的信息? lambda 函数上不存在此信息,我不知道如何填充这些参数
    • 运行时是您正在运行的应用程序的哪个版本(看起来像 Python,但哪个版本?)。该角色可通过 Lambda 中的“权限”选项卡访问
    猜你喜欢
    • 2022-11-03
    • 2019-07-16
    • 2016-06-13
    • 2021-04-25
    • 2019-01-17
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多