Skip to content

How to use AWS SDK in Go for developers who want to get started with Go

Notifications You must be signed in to change notification settings

unfor19/aws-sdk-golang-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-sdk-golang-examples

An example of how to use AWS SDK Resource Groups Tagging API (resourcegroupstaggingapi) in GoLang.

Especially useful if you're new to GoLang (like me). Tested on WSL2 Ubuntu 20.04

Requirements

Installation

  1. Prepare your terminal's ~/.rc file (.bashrc, .bash_profile, .zshrc, etc.)

    # GOLANG
    export GOPATH="$HOME/go"
    export GOBIN="/usr/local/go/bin"
    export PATH="$PATH:$GOPATH:$GOBIN"
    # export GO111MODULE=on # IMPORTANT: If you see this line, comment it out
  2. Reload the rc file - source ~/.bashrc

  3. Just in case - unset GO111MODULE

  4. Create a directory in ${HOME}/go/

    $ TARGET_DIR="${HOME}/go/src/internal/aws-sdk-golang-examples"
    $ mkdir -p "$TARGET_DIR"
  5. Why /internal/? Using the reserved keyword internal as a directory name instructs go get to consider all sub-directories as internal packages

Usage

# Download and install dependencies
$ TARGET_DIR="${HOME}/go/src/internal/aws-sdk-golang-examples"
$ cd "$TARGET_DIR"
$ wget https://raw.githubusercontent.com/unfor19/aws-sdk-golang-examples/master/main.go 
$ go get -u -v # -u = download latest, -v = verbose output
# You can also use `go mod download`
# See `go.mod` and `go.sum` section and check the Dockerfile

# `go get` Also builds a binary and copies it to /usr/local/go/bin/resourcegroupstaggingapi
# TODO: figure out why

# Edit main.go
# Set the relevant region and filters

# Run the application from source code
$ go run .

# Build the application and run the binary
$ go build -o resourcegroupstaggingapi
$ ./resourcegroupstaggingapi

# Check the size of the binary file
$ du -h resourcegroupstaggingapi # du = disk usage, -h = human readable
8.8M

go.mod and go.sum

To generate go.mod and go.sum, follow the next steps

# Adds current package to `go.mod`
$ go mod init

# Adds dependencies and creates `go.sum` (lock file)
$ go mod tidy

# Download dependencies according to go.mod
$ go mod download

Docker

Build a Docker image according to the Dockerfile. The image already is publicly available at DockerHub unfor19/aws-sdk-golang-examples

# Build
$ docker build -t unfor19/aws-sdk-golang-examples:resourcegroupstaggingapi .

# Generate `.env` file
$ printenv | grep AWS_ > .env

# Run - Passing credentials with `--env-file`
docker run --rm -it --env-file=.env unfor19/aws-sdk-golang-examples:resourcegroupstaggingapi

# Check Docker image size
docker system df -v | grep "REPOSITORY\|resourcegroupstaggingapi"
REPOSITORY                        TAG                        IMAGE ID       CREATED             SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
unfor19/aws-sdk-golang-examples   resourcegroupstaggingapi   cb130f3efcfe   7 minutes ago       23.88MB   14.74MB       9.136MB       0

#      real size:  23.88MB
#    shared size:  14.75MB
#    unique size:   9.14MB
# DockerHub size:  11.67MB

Troubleshooting

  1. go get permission denied to /usr/local/go/bin directory

    Error

    go get internal/aws-sdk-golang-examples: copying /tmp/go-build594918589/b001/exe/a.out: open /usr/local/go/bin/aws-sdk-golang-examples: permission denied
    
    # Human friendly
    go get internal/aws-sdk-golang-examples: 
    copying /tmp/go-build594918589/b001/exe/a.out: 
    open /usr/local/go/bin/aws-sdk-golang-examples: permission denied

    Fix

    # Set current user:user_group as the owner
    $ sudo chown -R $USER:$(id -g -n $USER) /usr/local/go/bin/
  2. App fails to run due to credentials issue

    Error

    failed to list resources, operation error Resource Groups Tagging API: GetResources, exceeded maximum number of attempts, 3, https response error StatusCode: 0, RequestID: , request send failed, Post "https://tagging.eu-west-1.amazonaws.com/": x509: certificate signed by unknown authority
    
    # Human friendly
    failed to list resources,
    operation error Resource Groups Tagging API: GetResources, 
    exceeded maximum number of attempts, 3, 
    https response error StatusCode: 0,
    RequestID: , request send failed,
    Post "https://tagging.eu-west-1.amazonaws.com/":
    x509: certificate signed by unknown authority

    Fix - Using alpine for the final image, and not scratch because AWS_SESSION_TOKEN env var doesn't work well on scratch.

About

How to use AWS SDK in Go for developers who want to get started with Go

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published