no required module provides package github.com/aws/aws-sdk-go/aws

Viewed 3076

Why am I getting this error message? I'm a beginner at using aws sam and Go.

Error: GoModulesBuilder:Build - Builder Failed: main.go:9:2: no required module provides package github.com/aws/aws-sdk-go/aws; to add it:
go get github.com/aws/aws-sdk-go/aws
main.go:10:2: no required module provides package github.com/aws/aws-sdk-go/aws/session; to add it:
go get github.com/aws/aws-sdk-go/aws/session
main.go:11:2: no required module provides package github.com/aws/aws-sdk-go/service/dynamodb; to add it:<br>
go get github.com/aws/aws-sdk-go/service/dynamodb

This is my code in vscode package main

import (
    "logs"

    "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"
    "github.com/aws/aws-sdk-go/service/dynamodb"
)
4 Answers

You need to set up your Go project properly for dependency management. First follow the steps for initializing the project as described in Tutorial: Get started with Go:

go mod init YOUR_PROJECT_NAME

And then add your dependencies:

go get github.com/aws/aws-sdk-go/aws
go get github.com/aws/aws-sdk-go/service/dynamodb

The issue is that AWS SAM creates a folder structure where the root of the SAM project contains the Makefile where AWS has you build the executable, but the entry point of the application is in a sub-folder (i.e. the hello-world folder).

You must run go mod init and go mod tidy from the same location as the main.go and go.mod files, not from the root folder of your SAM application.

So for anyone else learning SAM with go, try changing to the sub-folder with your go files before you run go commands.

For people with this problem using AWS SAM and vs-code, if your folder look like this:

├── Makefile
├── README.md
├── hello-world
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   └── main_test.go
└── template.yaml

Try to move the go.mod and go.sum to the root folder (where the vs-code was opened), like this:

├── Makefile
├── README.md
├── go.mod
├── go.sum
├── hello-world
│   ├── main.go
│   └── main_test.go
└── template.yaml
Related