可能是由于 Docker 容器中缺少必要的依赖项而导致的。可以在 SAM 模板中添加以下行来安装缺少的依赖项:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: index.handler
Runtime: nodejs12.x
Layers:
- arn:aws:lambda:us-west-2:XXXXX:layer:nodejs12-psycopg2:1
Events:
MyApi:
Type: Api
Properties:
Path: /my-api
Method: get
Dependencies:
Type: AWS::Serverless::Application
Properties:
Location: ./dependencies
在 ./dependencies
目录中创建 package.json
和 package-lock.json
文件,并添加需要的依赖项。例如,在 package.json
文件中添加以下内容:
{
"name": "dependencies",
"version": "1.0.0",
"dependencies": {
"pg": "^8.7.1"
}
}
然后可以使用以下命令将依赖项打包到 SAM 包中:
sam build --use-container
这将在 Docker 容器中构建 SAM 包,并将依赖项打包在其中。然后可以使用以下命令部署 SAM 应用程序:
sam deploy --guided
这将引导您完成部署过程。在部署过程中,您可以选择运行应用程序的区域和堆栈名称等选项。