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

AWS SAM部署失败:Fn::GetAtt引用未定义资源问题求助

解决AWS SAM部署失败:Fn::GetAtt引用未定义资源问题

部署AWS SAM应用时失败,状态为FAILED,错误原因:模板错误:Fn::GetAtt实例引用了未定义的资源HelloWorldFunction。

问题根源

原始模板的Resources部分定义的Lambda函数名为HelloWorldFunction,但修改后的模板将函数名改为NewFunctionName,但Outputs部分仍在引用旧的函数名及对应的隐式IAM角色,导致资源引用不匹配。

相关模板片段

原始Resources模板

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function 

修改后的Resources模板(引发错误)

Resources:
  NewFunctionName:
    Type: AWS::Serverless::Function

未同步更新的Outputs部分

Outputs:
  HelloWorldApi:
    Description: "Hello World函数Prod阶段的API Gateway端点URL"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda函数ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "为Hello World函数创建的隐式IAM角色"
    Value: !GetAtt HelloWorldFunctionRole.Arn

修复方案

当修改Lambda函数名称后,必须同步更新Outputs中所有对该函数的引用,包括SAM自动生成的隐式IAM角色:

  • 将Outputs中!GetAtt HelloWorldFunction.Arn替换为!GetAtt NewFunctionName.Arn
  • SAM会为函数自动生成格式为[函数名]Role的IAM角色,因此需将HelloWorldFunctionRole替换为NewFunctionNameRole
  • 若不需要输出IAM角色信息,可直接删除该Output项

修复后的完整模板

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app2

  sam-app2的示例SAM模板
  
Globals:
  Function:
    Timeout: 3

Resources:
  NewFunctionName:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs18.x
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints: 
        - app.ts

Outputs:
  HelloWorldApi:
    Description: "Hello World函数Prod阶段的API Gateway端点URL"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  NewFunctionName:
    Description: "Lambda函数ARN"
    Value: !GetAtt NewFunctionName.Arn
  NewFunctionNameIamRole:
    Description: "为Lambda函数创建的隐式IAM角色"
    Value: !GetAtt NewFunctionNameRole.Arn

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 09:44:51