Amazon web services 为SNS使用samlocal触发lambda函数

Amazon web services 为SNS使用samlocal触发lambda函数,amazon-web-services,aws-lambda,amazon-sns,aws-sam-cli,Amazon Web Services,Aws Lambda,Amazon Sns,Aws Sam Cli,我是SAM Local的新手,正在尝试使用它从SNS主题触发Lambda函数。当我像这样运行SAM Local时: 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/lambd

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

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
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"
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"
      }
    }
  ]
}
sam本地调用-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
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"
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"
      }
    }
  ]
}
我的sam.yml看起来像这样:

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
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"
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"
      }
    }
  ]
}
..我的event.json如下所示:

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
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"
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"
      }
    }
  ]
}

我遗漏了什么?

我发现了问题所在。正在调用的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