Error while trying to use terraform-plugin-sdk

I am new to Terraform, Terraform plugin development, and Go lang. I am trying learn custom plugin development by following this page on Terraform website.

On that page, in this section, there is a command to build the plugin. When I run that command, it runs into multiple “cannot find package” errors. Output is as pasted below.

# go build -o terraform-provider-sample
../../github.com/hashicorp/terraform-plugin-sdk/helper/schema/backend.go:4:2: cannot find package "context" in any of:
        /usr/lib/go-1.6/src/context (from $GOROOT)
        /root/code/go/src/context (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema/validate_traversal.go:7:2: cannot find package "github.com/hashicorp/hcl/v2" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2 (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2 (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/lang/eval.go:9:2: cannot find package "github.com/hashicorp/hcl/v2/ext/dynblock" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/ext/dynblock (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/ext/dynblock (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/configs/named_values.go:7:2: cannot find package "github.com/hashicorp/hcl/v2/ext/typeexpr" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/ext/typeexpr (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/ext/typeexpr (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/configs/module_call.go:7:2: cannot find package "github.com/hashicorp/hcl/v2/gohcl" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/gohcl (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/gohcl (from $GOPATH)
../../github.com/hashicorp/terraform-plugin- sdk/internal/configs/configschema/decoder_spec.go:4:2: cannot find package "github.com/hashicorp/hcl/v2/hcldec" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/hcldec (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/hcldec (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/configs/parser.go:8:2: cannot find package "github.com/hashicorp/hcl/v2/hclparse" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/hclparse (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/hclparse (from $GOPATH)
../../github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema/validate_traversal.go:8:2: cannot find package "github.com/hashicorp/hcl/v2/hclsyntax" in any of:
        /usr/lib/go-1.6/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOROOT)
        /root/code/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOPATH)
../../golang.org/x/sys/unix/affinity_linux.go:10:2: cannot find package "math/bits" in any of:
        /usr/lib/go-1.6/src/math/bits (from $GOROOT)
        /root/code/go/src/math/bits (from $GOPATH)
../../golang.org/x/net/http2/go111.go:10:2: cannot find package "net/http/httptrace" in any of:
        /usr/lib/go-1.6/src/net/http/httptrace (from $GOROOT)
        /root/code/go/src/net/http/httptrace (from $GOPATH)

I see that I have terraform-plugin-sdk in my GOPATH, and it is up to date with github copy. I am not sure how this should be fixed. Any help in resolving this is appreciated!

This issue is resolved now.

Problem was with Go lang version being slightly older. I removed earlier golang installation (done using apt-get), downloaded latest Go version from official site and installed it.

For detailed steps, please refer this link.

Hi Prabhus,

You need to setup go modules. Use the following in your provider directory which looks to be “terraform-provider-sample” in your case.

  1. go mod init
  2. go mod download
  3. go mod tidy

Hope this helps,

Regards

Phil.