执行sam build成功但构建目录无Lambda函数的问题求助
问题排查:SAM构建成功但未生成Lambda函数文件
问题详情
执行sam build命令后提示构建成功,但.aws-sam/build目录下仅存在template.yaml,没有预期的Lambda函数代码目录。
相关文件与结构
项目目录结构
D:. │ .gitignore │ template.yaml │ tree.txt │ ├───.aws-sam │ │ build.toml │ │ │ └───build │ template.yaml │ ├───events │ testHelloWorld.json │ └───lambdas └───HelloWorld index.py __init__.py
原template.yaml配置
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Globals: Function: Runtime: python3.9 Handler: index.handler Resources: HelloWorldFunction: Properties: Type: AWS::Serverless::Function CodeUri: lambdas/HelloWorld/ Policies: - AWSLambdaExecute - Version: '2012-10-17'
Lambda函数代码(lambdas/HelloWorld/index.py)
import json def handler(event, context): return json.dumps(event, indent=2)
问题原因
template.yaml语法错误:Type: AWS::Serverless::Function被错误嵌套在Properties字段下。SAM资源定义规则中,Type必须与Properties同级,否则SAM无法识别该资源为Lambda函数,不会执行代码打包流程。
解决步骤
- 修正template.yaml的资源结构,将
Type移到Properties外部,与Properties、Policies同级:
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Globals: Function: Runtime: python3.9 Handler: index.handler Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: lambdas/HelloWorld/ Policies: - AWSLambdaExecute - Version: '2012-10-17'
- 重新执行
sam build命令,此时.aws-sam/build目录下会生成HelloWorldFunction文件夹,包含打包后的Lambda代码文件。
内容的提问来源于stack exchange,提问作者Debadri Chowdhury
相关产品推荐
相关产品推荐

