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
- golang v1.15
- AWS Credentials
- (Optional) Visual Studio Code and its Go extension
-
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
-
Reload the rc file -
source ~/.bashrc -
Just in case -
unset GO111MODULE -
Create a directory in
${HOME}/go/$ TARGET_DIR="${HOME}/go/src/internal/aws-sdk-golang-examples" $ mkdir -p "$TARGET_DIR"
-
Why
/internal/? Using the reserved keywordinternalas a directory name instructsgo getto consider all sub-directories as internal packages
# 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.8MTo 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 downloadBuild 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-
go getpermission denied to/usr/local/go/bindirectoryError
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 deniedFix
# Set current user:user_group as the owner $ sudo chown -R $USER:$(id -g -n $USER) /usr/local/go/bin/
-
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_TOKENenv var doesn't work well on scratch.