简体   繁体   English

SNS主题未触发Lambda

[英]SNS topic not triggering Lambda

I am attempting to set up a email-sending lambda function that is triggered by an SNS topic in cloudformation, but for some reason it is not working. 我正在尝试设置由cloudformation中的SNS主题触发的电子邮件发送lambda函数,但是由于某种原因,它无法正常工作。 I went in and checked all of the dependencies/permissions after the lambda & sns went up and everything seems to be in order, but when I publish to the topic nothing happens. 在lambda和sns上升之后,我进入并检查了所有依赖项/权限,一切似乎都井然有序,但是当我发布该主题时,什么也没有发生。 When I manually test the lambda in the Lambda console, it works perfectly. 当我在Lambda控制台中手动测试Lambda时,它可以完美运行。

Cloudformation Cloudformation

"Resources": {
    "CloudformationEventHandlerLambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "Path": "/",
        "Policies": [
          {
            "PolicyName": "CloudformationTrigger",
            "PolicyDocument": {
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                      "ses:*"
                  ],
                  "Resource": [
                    "arn:aws:ses:*"
                  ]
                }
              ]
            }
          }
        ],
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Action": [
                "sts:AssumeRole"
              ],
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              }
            }
          ]
        }
      }
    },
    "CloudformationEventHandlerLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Handler": "lambda_function.lambda_handler",
        "Role": {
          "Fn::GetAtt": [
            "CloudformationEventHandlerLambdaExecutionRole",
            "Arn"
          ]
        },
        "Code": {
          "S3Bucket": {
            "Ref": "Bucket"
          },
          "S3Key": "CloudformationEventHandler.zip"
        },
        "Runtime": "python2.7",
        "Timeout": "30"
      },
      "DependsOn": [
        "CloudformationEventHandlerLambdaExecutionRole"
      ]
    },
    "CloudformationEventHandlerLambdaInvokePermission": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "Action": "lambda:InvokeFunction",
        "SourceAccount": {
          "Ref": "AWS::AccountId"
        },
        "Principal": "sns.amazonaws.com",
        "SourceArn": {
            "Ref": "CloudformationTopic"
        },
        "FunctionName": {
          "Fn::GetAtt": [
            "CloudformationEventHandlerLambdaFunction",
            "Arn"
          ]
        }
      }
    },
    "CloudformationTopic": {
        "Type": "AWS::SNS::Topic",
        "Properties": {
            "DisplayName": "CloudformationIngestTopic",
            "Subscription": [
                {
                    "Endpoint": {
                        "Fn::GetAtt": [
                            "CloudformationEventHandlerLambdaFunction",
                            "Arn"
                        ]
                    },
                    "Protocol": "lambda"
                }
            ]
        },
        "DependsOn": [ "CloudformationEventHandlerLambdaFunction" ]
    }
  }

Python SES Lambda Python SES Lambda

import boto3

client = boto3.client('ses')

def lambda_handler(event, context):
    message = """
        Event:
        {}

        Context:
        {}
    """.format(event, context)

    response = client.send_email(
            Source='***censored***',
            Destination={ 'ToAddresses': [ ***censored***' ] },
            Message={
                    'Subject': {
                            'Data': 'CFMTest'
                        },
                    'Body': {
                            'Text': {
                                    'Data': message
                                }
                        }
                }
            )

The SourceAccount for the AWS::Lambda::Permission resource type is only meant to be used with Cloudwatch logs, CloudWatch rules, S3 and SES. AWS::Lambda::Permission资源类型的SourceAccount仅用于Cloudwatch日志,CloudWatch规则,S3和SES。
After removing this field from the CloudformationEventHandlerLambdaInvokePermission resource on your template, I am able to invoke the lambda function by publishing to the SNS topic. 从模板上的CloudformationEventHandlerLambdaInvokePermission资源中删除此字段后,我可以通过发布到SNS主题来调用lambda函数。

Refer to this documentation for more information regarding lambda permissions 有关lambda权限的更多信息,请参阅文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM