英文:
Golang using aws lambda error: InvokeInput not declared by package lambda
问题
我正在尝试使用以下代码调用另一个 Lambda 函数:
sess := session.Must(
session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
},
),
)
svc := lambda.New(sess, &aws.Config{Region: aws.String("ap-east-1")})
result, err := svc.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
})
但是出现了两个错误:
New not declared by package lambda
和
InvokeInput not declared by package lambda
我尝试初始化了 go.mod 文件,但是这并没有修复这两个错误。
有什么想法吗?
我的 main.go
文件的导入部分如下:
"fmt"
"os"
"pkg/log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
还有 go.mod
文件的内容如下:
module somefunc
go 1.16
require (
github.com/aws/aws-lambda-go v1.27.0
github.com/aws/aws-sdk-go v1.40.59
pkg/log v0.0.0-00010101000000-000000000000
)
replace pkg/log => ./../../go/common/pkg/log
英文:
I'm trying to call another lambda function using the following code:
sess := session.Must(
session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
},
),
)
svc := lambda.New(sess, &aws.Config{Region: aws.String("ap-east-1")})
result, err := svc.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
})
But there are two errors
New not declared by package lambda
and
InvokeInput not declared by package lambda
I've tried to initialize the go.mod file, but it doesn't fix both of the errors.
Any ideas?
The imports of my main.go
file:
"fmt"
"os"
"pkg/log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
And the go.mod
file
module somefunc
go 1.16
require (
github.com/aws/aws-lambda-go v1.27.0
github.com/aws/aws-sdk-go v1.40.59
pkg/log v0.0.0-00010101000000-000000000000
)
replace pkg/log => ./../../go/common/pkg/log
答案1
得分: 0
根据你的代码片段,看起来你正在使用 AWS SDK for Go V2。建议使用 AWS SDK for Go V2(请查看迁移到 AWS SDK for Go V2 文档)。
请按照 SDK 的 Github 页面 上的说明初始化一个 Go Modules 项目:
mkdir YOUR-PROJECT
cd YOUR-PROJECT
go mod init YOUR-PROJECT
按照以下方式添加依赖项:
go get github.com/aws/aws-sdk-go-v2/aws
go get github.com/aws/aws-sdk-go-v2/config
go get github.com/aws/aws-sdk-go-v2/service/lambda
上述步骤应该能够为你提供一个工作正常的项目,其中 SDK 包的依赖项将会解析。
相应的 V2 代码将类似于以下内容:
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
)
if err != nil {
[...]
}
svc := lambda.NewFromConfig(cfg)
result, err := svc.Invoke(context.TODO(),
&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
},
)
英文:
Based on your code snipped, it looks as if you are using the AWS SDK for Go V2. It is recommended to use the AWS SDK for Go V2 (please review the Migrating to the AWS SDK for Go V2 documentation).
Please initialize a Go Modules project (as described on the SDK's Github page):
mkdir YOUR-PROJECT
cd YOUR-PROJECT
go mod init YOUR-PROJECT
Add the dependencies as follows:
go get github.com/aws/aws-sdk-go-v2/aws
go get github.com/aws/aws-sdk-go-v2/config
go get github.com/aws/aws-sdk-go-v2/service/lambda
The above should give you a working project where the dependencies for the SDK packages will resolve.
The corresponding V2 code will look something like the following:
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
)
if err != nil {
[...]
}
svc := lambda.NewFromConfig(cfg)
result, err := svc.Invoke(context.TODO(),
&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
},
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论