【问题标题】:AWS SAM fails to build layerAWS SAM 无法构建层
【发布时间】:2020-10-28 23:48:29
【问题描述】:

我读到 SAM 现在支持构建层,并遵循here 中提到的指示。但是,当我尝试使用 sam build samDeployLayer 在本地构建层时,我遇到了构建错误

Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "E:\Development\sam-deploy\src\sam-deploy-layer" as it does not contain a package.json file.

这是我的模板文件:

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  sam-deploy
Transform:
- AWS::Serverless-2016-10-31
Resources:
  samDeploy:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/sam-deploy
      Handler: index.handler
      Runtime: nodejs12.x
      MemorySize: 128
      Timeout: 100
      Description: A Lambda function that returns a static string.
      FunctionName: "sam-deploy"
      Layers:
        - !Ref samDeployLayer
      Policies:
        # Give Lambda basic execution Permission to the helloFromLambda
        - AWSLambdaBasicExecutionRole
  samDeployLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: sam-deploy-layer
      ContentUri: src/sam-deploy-layer
      CompatibleRuntimes:
        - nodejs12.x
    Metadata:
      BuildMethod: nodejs12.x

根据构建层的要求,我已经包含了CompatibleRuntimesMetadata 属性。此层的package.json 位于src/sam-deploy-layer/nodejs,这是 nodeJS 运行时所需的,我使用的是 SAM CLI 版本 0.53.0。我做错了什么?

【问题讨论】:

  • 你应该有一个 package.json 用于两个节点项目(samDeploy 和 samDeployLayer)。此文件应位于src/sam-deploysrc/sam-deploy-layer 位置(分别)否则更新您的CodeUri 和/或ContentUri值。

标签: node.js amazon-web-services aws-lambda yaml aws-sam


【解决方案1】:

您应该在 src/sam-deploy-layer 中创建一个名为 nodejs 的文件夹。在这里,您可以执行 npm init 和 npm install(您的层中的包)。

【讨论】:

    【解决方案2】:

    package.json 放在src/sam-deploy-layer/ 下。 运行sam build后,应该会创建目录.aws-sam/build/samDeployLayer/nodejs/node_modules/

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 2021-09-27
      • 2022-06-16
      • 2022-06-10
      • 1970-01-01
      • 2020-10-23
      • 2019-12-31
      • 2019-02-13
      • 2021-09-11
      相关资源
      最近更新 更多