Building a Go Microservice for AWS Fargate

Table of Contents

  1. Overview
  2. Prerequisites
  3. Setup
  4. Creating a Go Microservice
  5. Building the Docker Image
  6. Deploying to AWS Fargate
  7. Conclusion

Overview

In this tutorial, we will learn how to build a Go microservice and deploy it to AWS Fargate. We will cover the entire process from creating the microservice to building a Docker image and deploying it to AWS Fargate. By the end of this tutorial, you will have a working Go microservice running on AWS Fargate.

Prerequisites

To follow along with this tutorial, you will need:

  • A basic understanding of Go programming language
  • An AWS account
  • Docker installed on your machine
  • AWS CLI installed on your machine

Setup

Before we start building our microservice, we need to set up AWS credentials on our local machine. Follow these steps:

  1. Install the AWS CLI by following the instructions provided in the AWS documentation.

  2. Once installed, open a terminal and run the following command to configure the AWS CLI:

     aws configure
    

    This command will prompt you to enter your AWS Access Key ID, AWS Secret Access Key, default region name, and default output format. Enter the required information accordingly.

Creating a Go Microservice

Let’s begin by creating a new Go module for our microservice:

  1. Open a terminal and navigate to the desired location where you want to create the microservice.

  2. Run the following command to create a new Go module:

     go mod init github.com/your-username/microservice
    

    Replace your-username with your GitHub username or any other repository hosting provider you prefer.

  3. Create a new directory inside the module directory for our microservice code:

     mkdir service
    
  4. Change into the service directory:

     cd service
    
  5. Create a new Go file named main.go using any text editor of your choice and add the following code:

     package main
        
     import (
     	"fmt"
     	"net/http"
     )
        
     func main() {
     	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
     		fmt.Fprintf(w, "Hello, World!")
     	})
        
     	http.ListenAndServe(":8080", nil)
     }
    

    This code sets up a basic HTTP server that listens on port 8080 and responds with “Hello, World!” for any incoming requests.

  6. Save the file and exit the text editor.

Building the Docker Image

To deploy our microservice to AWS Fargate, we need to build a Docker image. Here’s how:

  1. Create a new file named Dockerfile in the root directory of your Go module:

     touch Dockerfile
    
  2. Open the Dockerfile using a text editor and add the following code:

     FROM golang:1.17-alpine
        
     WORKDIR /app
        
     COPY go.mod .
     COPY go.sum .
        
     RUN go mod download
        
     COPY . .
        
     RUN go build -o main ./service
        
     CMD ["./main"]
    

    This Dockerfile uses the official Go Docker image as the base image, copies the module files and downloads dependencies, copies the microservice code, builds the Go binary, and sets the binary as the entry point.

  3. Save the Dockerfile and exit the text editor.

  4. Open a terminal and navigate to the root directory of your Go module.

  5. Build the Docker image by running the following command:

     docker build -t my-microservice .
    

    This command builds a Docker image with the tag my-microservice using the current directory as the build context.

  6. Wait for the image to build successfully.

Deploying to AWS Fargate

Now that we have the Docker image ready, we can deploy our microservice to AWS Fargate:

  1. Open a terminal and log in to your AWS account using the AWS CLI:

     aws configure
    
  2. Run the following command to create a new ECR repository for storing the Docker image:

     aws ecr create-repository --repository-name my-microservice
    
  3. Take note of the repositoryUri value returned by the command as we will need it later.

  4. Authenticate the Docker client with the ECR registry by running the following command:

     aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com
    

    Replace your-region with your desired AWS region and your-account-id with your AWS account ID.

  5. Push the Docker image to the ECR repository by running the following command:

     docker tag my-microservice:latest your-account-id.dkr.ecr.your-region.amazonaws.com/my-microservice:latest
     docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-microservice:latest
    

    Replace your-account-id and your-region with your AWS account ID and desired AWS region.

  6. Open the AWS Management Console and navigate to the Amazon ECS service.

  7. Click on “Clusters” and create a new cluster with the desired configuration.

  8. Once the cluster is created, click on “Task Definitions” and then “Create new Task Definition”.

  9. Configure the task definition with the necessary settings, including the Docker image URI, port mappings, and resource allocation.

  10. Click on “Next” and then “Create”.

  11. Navigate back to the cluster and click on “Services”.

  12. Create a new service using the task definition created in the previous step.

  13. Configure the service with the desired settings, including the number of desired tasks and the load balancer.

  14. Click on “Next”, review the configuration, and then click on “Create Service”.

  15. Wait for the service to be created and deployed.

    Congratulations! Your Go microservice is now running on AWS Fargate. You can access it using the URL provided by the load balancer.

Conclusion

In this tutorial, we learned how to build a Go microservice and deploy it to AWS Fargate. We covered the steps for creating a Go module, writing the microservice code, building a Docker image, and deploying the image to AWS Fargate. By following these steps, you now have a fully functioning Go microservice running on AWS Fargate.

Remember to clean up your AWS resources after you finish experimenting to avoid incurring unnecessary costs.