1

我是 SAM-Local 的新手,正在尝试使用它从 SNS 主题触发 Lambda 函数。当我像这样运行 SAM Local 时:

sam local invoke -t sam.yaml ServiceNowIncidentCreator -e sns_event.json

我的 Lambda 函数没有被触发。我只看到 docker 容器启动并像这样永远等待:

2018/03/13 18:39:17 Successfully parsed sam.yaml
2018/03/13 18:39:17 Connected to Docker 1.32
2018/03/13 18:39:17 Runtime image missing, will pull....
2018/03/13 18:39:17 Fetching lambci/lambda:python3.6 image for python3.6    runtime...
python3.6: Pulling from lambci/lambda
5be106c3813f: Pull complete 
e240967675e1: Pull complete 
9e3a67ef4b55: Pull complete 
f6645a04a4f3: Pull complete 
4dfe0dcbdfa8: Pull complete 
Digest: sha256:6626c2fe135d51952192273481decadbef184f528fc478c63d2379ed0efdb526
Status: Downloaded newer image for lambci/lambda:python3.6
2018/03/13 18:40:57 Reading invoke payload from stdin (you can also pass it from file with --event)
--event

我的 sam.yml 看起来像这样:

ServiceNowIncidentCreator:
    Type: 'AWS::Serverless::Function'
    Properties:
      Runtime: python3.6
      Timeout: 180
      Handler: servicenow_incident_creator.handler
      CodeUri: ./functions/servicenow_incident_creator
      Description: "Function to create Servicenow incidents from SNS notifications"
      Environment:
        Variables:
          JobQueuePrefixName: aws-hpc-shuklas-JobQueue-DEV
      Events:
  LambdaSNSTopic:
    Type: "AWS::SNS::Topic"
    Properties:
      DisplayName: "Lambda SNS topic"
      Subscription:
        -
          Endpoint:
            Fn::GetAtt:
              - "ServiceNowIncidentCreator"
              - "Arn"
          Protocol: "lambda"
      TopicName: "service-now-sns-topic"

..我的 event.json 看起来像这样:

cat sns_event.json 
{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "example message",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "EXAMPLE",
        "TopicArn": "arn:aws:sns:us-east-1:111122223333:ExampleTopic",
        "Subject": "example subject"
      }
    }
  ]
}

我错过了什么?

4

1 回答 1

2

我弄清楚了问题所在。被调用的 Lambda 函数的名称应该是sam local invoke命令中的最后一项。所以,而不是这样做:

sam local invoke -t sam.yaml ServiceNowIncidentCreator -e sns_event.json

我这样做是为了让它成功运行:

sam local invoke -t sam.yaml -e sns_event.json ServiceNowIncidentCreator
于 2018-03-15T16:14:18.003 回答