由于找不到处理程序错误,无法部署 aws sam 堆栈

Cannot deploy aws sam stack due to Handler not found error

我在使用 sam.

在嵌套目录中部署带有处理程序的 lambda 时遇到问题

我执行以下步骤:

  1. 包:

    sam package --template template.yaml --output-template-file packaged.yaml --s3-bucket

创建我在下一步中使用的 packaged.yaml。

  1. 部署:

    aws cloudformation deploy --template-file /Users/localuser/Do/learn-sam/dynamo-stream-lambda/packaged.yaml --stack-name barkingstack

错误

Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [PublishNewBark] is invalid. Missing required property 'Handler'.

Cloudformation/SAM模板

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Globals:
  Function:
    Runtime: nodejs8.10
    Timeout: 300

Resources:
  PublishNewBark:
    Type: AWS::Serverless::Function
    FunctionName: publishNewBark
    CodeUri: .
    Handler: src/index.handler
    Role: "<ROLE_ARN>"
    Description: Reads from the DynamoDB Stream and publishes to an SNS topic
    Events:
      - ReceiveBark:
          Type: DynamoDB
          Stream: !GetAtt BarkTable.StreamArn
          StartingPosition: TRIM_HORIZON
          BatchSize: 1


  BarkTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: BarkTable
      KeySchema:
        - KeyType: HASH
          AttributeName: id
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      ProvisionedThroughput:
        WriteCapacityUnits: 5
        ReadCapacityUnits: 5

  WooferTopic:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: wooferTopic
      TopicName: wooferTopic
      Subscription:
        - Endpoint: <my_email>
          Protocol: email

目录结构

root_directory/ 事件/(示例事件) policies/(用于使用 CLI 为 lambda 创建 IAM 角色) src/index.js package.json node_modules template.yaml

处理程序代码

async function handler (event, context) {
  console.log(JSON.stringify(event, null, 2))
  return {}
}

module.exports = {handler}

我相信您必须将除资源类型之外的所有内容都放在 "Properties" 下。

你的函数声明应该是:

PublishNewBark:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: publishNewBark
        CodeUri: .
        Handler: src/index.handler
        Role: "<ROLE_ARN>"
        Description: Reads from the DynamoDB Stream and publishes to an SNS topic
        Events:
          - ReceiveBark:
              Type: DynamoDB
              Stream: !GetAtt BarkTable.StreamArn
              StartingPosition: TRIM_HORIZON
              BatchSize: 1