【问题标题】:Serverless Framework Error: Every Resources object must contain a Type member无服务器框架错误:每个资源对象都必须包含一个类型成员
【发布时间】:2021-02-08 01:07:27
【问题描述】:

我正在尝试将 EFS 挂载到我的 Lambda 函数,以便我可以使用大型依赖项。到目前为止,我一直在关注this 教程。

我稍微修改了.yml

serverless.yml

service: test2EFS

plugins:
  - serverless-pseudo-parameters

custom:
  efsAccessPoint: fsap-00**********
  LocalMountPath: /mnt/efs
  subnetsId: subnet-0b**********
  securityGroup: sg-0b7**********

provider:
  name: aws
  runtime: python3.6
  region: us-east-2

package:
  exclude:
    - node_modules/**
    - .vscode/**
    - .serverless/**
    - .pytest_cache/**
    - __pychache__/**

functions:
  test:
    handler: handler.handler
    environment: # Service wide environment variables
      MNT_DIR: ${self:custom.LocalMountPath}
    vpc:
      securityGroupIds:
        - ${self:custom.securityGroup}
      subnetIds:
        - ${self:custom.subnetsId}
    iamManagedPolicies:
      - arn:aws:iam::aws:policy/AmazonElasticFileSystemClientReadWriteAccess
    events:
      - http:
          path: test
          method: get

resources:
  extensions:
    # Name of function
    test2EFSLambdaFunction:
      Properties:
        FileSystemConfigs:
          - Arn: 'arn:aws:elasticfilesystem:${self:provider.region}:#{AWS::AccountId}:access-point/${self:custom.efsAccessPoint}'
            LocalMountPath: '${self:custom.LocalMountPath}'

每当我运行 severless deploy 时,我都会收到以下错误:

Serverless: Uploading artifacts...
Serverless: Uploading service test2EFS.zip file to S3 (926 B)...
Serverless: Validating template...
 
  Error --------------------------------------------------
 
  Error: The CloudFormation template is invalid: Template format error: [/Resources/test2EFSLambdaFunction] Every Resources object must contain a Type member.
      at provider.request.catch (/usr/local/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/validateTemplate.js:20:13)
      at tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:725:18)
      at _drainQueueStep (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:15:14)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:126:23)

我尝试注释掉各个部分,并将错误范围缩小到某个范围内

resources:
  extensions:
    # Name of function
    test2EFSLambdaFunction:
      Properties:
        FileSystemConfigs:
          - Arn: 'arn:aws:elasticfilesystem:${self:provider.region}:#{AWS::AccountId}:access-point/${self:custom.efsAccessPoint}'
            LocalMountPath: '${self:custom.LocalMountPath}'

除了重命名(我认为是)无关紧要的函数名称之外,此代码 sn-p 与引用相同。我尝试根据我的帐户 ID 对#{AWS::AccountId} 进行硬编码,但这没有用。我现在有点难过。

【问题讨论】:

    标签: aws-lambda serverless-framework amazon-efs


    【解决方案1】:

    通常,CloudFormation 需要在资源定义中使用 Type 参数,例如Type: AWS::Lambda::Function,因此您会看到错误。在您的情况下,您正在使用 Serverless 的 Override AWS CloudFormation Resource 功能,即名称需要与 Serverless 分配的规范化函数名称完全匹配(请参阅上面链接的文档),在您的情况下,这将是 TestLambdaFunction

    将您的代码更改为:

    resources:
      extensions:
        TestLambdaFunction:
          Properties:
            [...]
    

    【讨论】:

      【解决方案2】:

      我在 GitHub 上与一些人交谈过。如 yvesonline 所述,此方法会覆盖自动生成的云形成代码。 Serverless 对 EFS 有原生支持,建议使用问题中描述的方法。

      无服务器中的原生 EFS 看起来像这样; fileSystemConfig 存在于函数定义中:

      test:
          handler: handler.test
          environment: # Service wide environment variables
            MNT_DIR: ${self:custom.LocalMountPath}
          vpc:
            securityGroupIds:
              - ${self:custom.securityGroup}
            subnetIds:
              - ${self:custom.subnetsId}
          iamManagedPolicies:
            - arn:aws:iam::aws:policy/AmazonElasticFileSystemClientReadWriteAccess
          events:
            - http:
                path: test
                method: get
          fileSystemConfig:
            localMountPath: '${self:custom.LocalMountPath}'
            arn: 'arn:aws:elasticfilesystem:${self:provider.region}:#{AWS::AccountId}:access-point/${self:custom.efsAccessPoint}'
      

      您可以阅读有关无服务器 efs 配置的更多信息here

      【讨论】:

        猜你喜欢
        • 2017-05-30
        • 1970-01-01
        • 2018-06-21
        • 2019-08-26
        • 2016-11-19
        • 2021-09-08
        • 2020-06-28
        • 2019-03-11
        • 1970-01-01
        相关资源
        最近更新 更多