How to use AWS SAM with Go in Visual Studio Code
Tags: awssam, go, vscode
Published: 2022-12-10 10:37:57

Setup [Visual Studio Code](https://code.visualstudio.com "Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.") as your IDE to build and deploy an AWS SAM stack using [Go](https://golang.org "Go is an open source programming language that makes it easy to build simple, reliable, and efficient software."). I recently started to experiment with [Go](https://golang.org "Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.") and the best way to learn is to play with it and build something. So I decided to build a simple application. During this process I ran into the problem that my IDE did not like having many go modules within one project.

Project layout
--------------

With AWS SAM you can build and deploy your stack or via a CI/CD pipeline, the process is the same. First you perform a `sam build`. This will compile the source code for each `AWS::Serverless::Function` in the `template.yaml`. Followed by `sam deploy`. This will upload the artifacts and deploy the template using AWS CloudFormation.

The problem that I faced was that each function needs a `go.mod` in the source folder. Visual Studio Code expects a single `go.mod` file in the project.


![Project Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5v4alxfi3ttgso47bm6.png)

As a result imports in the unit test do not get recognized:

![Sample code of the unit test without workspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5bquvhikq1h1p5rx1kw.png)

* The `RequestEvent` struct.
* The `handler` method.
* The `ResponseEvent` struct.
The reason for this is that the IDE has set the `GOPATH` to the root of the project. Therefor the imports are not correct. By using [Workspace Folders](https://github.com/golang/tools/blob/master/gopls/doc/workspace.md "Workspace folders allow you to scope the modules within one project.") you can change this behavior so that each folder has it’s own scope.

> From Go 1.16+ the default build mode has switched from `GOPATH` to [Go modules](https://golang.org/ref/mod "Modules are how Go manages dependencies.").

To do this you need to create a `workspace.code-workspace` file. Place it in the root of your project with the following content:

```json
{
    "folders": [
        {
            "path": "functions/api"
        },
        {
            "path": "functions/confirm"
        },
        {
            "path": "functions/ship"
        },
        {
            "path": "functions/stock"
        },
        {
            "path": "."
        }
    ]
}
```

Now because each Go module lives within it’s own scope the imports are correct. As a result you are able to navigate through the code using the `⌘ click` action.

Conclusion
----------

When you deal with many Go modules within one project it is important to configure your IDE. This will improve readability, reduces errors and increases your development speed!