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

使用AWS SDK for Go V2调用Lambda示例时签名请求失败

问题分析

报错核心是SDK无法获取有效凭证:本地环境无法访问EC2实例元数据服务(169.254.169.254),且未正确加载IAM Identity Center的临时凭证。

解决方案

1. 确认本地SSO凭证配置与状态

  • 检查~/.aws/config文件,确保存在对应IAM Identity Center的profile配置,格式如下:
    [profile sso-profile]
    sso_start_url = https://<你的SSO门户地址>/start
    sso_region = <SSO所在区域>
    sso_account_id = <你的AWS账户ID>
    sso_role_name = <你分配的SSO角色名>
    region = us-east-1
    
  • 确保已通过VS Code AWS Toolkit或AWS CLI执行过sso login,获取了有效的临时会话凭证(凭证缓存路径为~/.aws/sso/cache)。

2. 修改代码指定SSO Profile

在加载配置时明确指定使用你的SSO profile,让SDK直接读取SSO凭证:

import (
    "context"
    "fmt"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/lambda"
)

func main() {
    // 替换为你的SSO profile名称
    ssoProfile := "sso-profile"
    sdkConfig, err := config.LoadDefaultConfig(context.TODO(), 
        config.WithRegion("us-east-1"),
        config.WithSharedConfigProfile(ssoProfile),
    )

    if err != nil {
        fmt.Println("Couldn't load default configuration. Have you set up your AWS account?")
        fmt.Println(err)
        return
    }
    lambdaClient := lambda.NewFromConfig(sdkConfig)

    maxItems := 10
    fmt.Printf("Let's list up to %v functions for your account.\n", maxItems)
    result, err := lambdaClient.ListFunctions(context.TODO(), &lambda.ListFunctionsInput{
        MaxItems: aws.Int32(int32(maxItems)),
    })
    if err != nil {
        fmt.Printf("Couldn't list functions for your account. Here's why: %v\n", err)
        return
    }
    if len(result.Functions) == 0 {
        fmt.Println("You don't have any functions!")
    } else {
        for _, function := range result.Functions {
            fmt.Printf("\t%v\n", *function.FunctionName)
        }
    }
}

3. 验证VS Code AWS Toolkit的连接状态

  • 打开VS Code的AWS Explorer,确认已选中正确的SSO账户和区域,且能正常浏览Lambda等资源。
  • 如果Toolkit显示已连接但代码仍报错,尝试重启VS Code或重新执行SSO登录,刷新凭证缓存。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 05:24:59