Home > OS >  Golang using aws lambda error: InvokeInput not declared by package lambda
Golang using aws lambda error: InvokeInput not declared by package lambda

Time:10-13

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

CodePudding user response:

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),
    },
)
  • Related