You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行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函数,不会执行代码打包流程。

解决步骤

  1. 修正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'
  1. 重新执行sam build命令,此时.aws-sam/build目录下会生成HelloWorldFunction文件夹,包含打包后的Lambda代码文件。

内容的提问来源于stack exchange,提问作者Debadri Chowdhury

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:10:51