Create a configuration

We start with an existing go module and an existing main.go.

The import for the config is:

import	"github.com/aws/aws-sdk-go-v2/config"

After adding this line to the import section, you have to download the library with:

  • go get "github.com/aws/aws-sdk-go-v2/config"

I tend to init the configuration and the client in the init function:

func init(){
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		panic("configuration error, " + err.Error())
	}
}

This code alone will not work, because

  • You did not import config: “./main.go:11:39: undefined: context”
  • The variable cfg is not used: “./main.go:13:2: cfg declared but not used”

So the import becomes:

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

func init(){
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		panic("configuration error, " + err.Error())
	}

}

In the next step we create the client.

See also

Source

See the full source on github.

Sources