Deploy a Go App to AWS EC2

We built a web application of Blog with GoLang in a previous article. In this article, we are going to deploy it on AWS EC2. We are going to learn how to set up Nginx and MySQL on the Linux server. We know that there are many easier ways to deploy GoLang web apps, but it is about basic deployment, and it is important to know.

AWS EC2

What you need as a prerequisite is only AWS EC2. You can sign up https://aws.amazon.com/free/. You will get 750 hours of AWS EC2 for 12 months called Free Tier. We can try and learn many things with this Free Tier.

Amazon Elastic Compute Cloud (EC2) offers virtual computers, available anytime over the Internet. AWS virtual computers emulate most real computers, including Central Processing Unit (CPU), Graphics Processing Unit (GPU), RAM, hard drive / SSD storage, operating system, network, etc. Maybe we also can get similar services from Microsoft Azure, Google Cloud Platform, or Alibaba Cloud. But this article runs on AWS EC2.

EC2 Instance

Let’s create an instance.

EC2 Dashboard

Click Launch Instance!

EC2 AMI

Choose Amazon Linux 2 AMI. Because it is an official image of AWS EC2, and it is free tier eligible.

EC2 Instance Type

Choose t2.micro. It is enough to handle our blog web app, and it is free tier eligible.

EC2 Configure Instance

You can configure the network, subnet, etc.

EC2 Storage

8 GB is enough.

EC2 Tags

You can create tags if you want.

EC2 Security Group

You can create a security group or choose an existing one. As you can see here, I opened HTTP, HTTPS, and SSH for my default security group.

EC2 Review

You can review your configuration in the end.

EC2 Keys

Pick a key pair. We will log in using it on SSH.

EC2 Launch Status

We just created an EC2 instance.

EC2 Instances

Make sure it is running.

Nginx

Nginx will forward our web app to the public. Let’s set up it.

  1. Set permission to our key. Eg: chmod 400 go-blog.pem.
  2. SSH to the server using the public IPv4 address. Eg: ssh -i go-blog.pem ec2-user@175.41.171.199.
  3. Install Nginx. Eg: sudo amazon-linux-extras install nginx1 -y.
  4. Nginx has been installed. Check it! Eg: sudo systemctl status nginx.
  5. Let’s start Nginx and make it auto-start when the server gets restarted. Eg: sudo systemctl enable nginx && sudo systemctl start nginx.
  6. Let’s check it again. Make sure it is running. Eg: sudo systemctl status nginx.
  7. Open the web browser. Go to the public IPv4. We will see the Nginx default page.

EC2 Nginx

MySQL

Let’s set up the database. We’re going to use MySQL.

  1. Install MySQL 8 repository. Eg: sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y.
  2. Install the dependency for MySQL. sudo amazon-linux-extras install epel -y.
  3. Install MySQL. Eg: sudo yum install mysql-community-server -y.
  4. Let’s start MySQL and make it auto-start when the server gets restarted. Eg: sudo systemctl enable mysqld && sudo systemctl start mysqld.
  5. Grab the temporary password. Eg: sudo grep ‘temporary password’ /var/log/mysqld.log.
  6. Set up the new password for MySQL. Eg: sudo mysql_secure_installation.
  7. Log in to the MySQL CLI. Eg: mysql -u root -p.
  8. Create a database, user, and set the permission for the web app.

    CREATE DATABASE go_blog;
    CREATE USER ‘go_blog’@‘localhost’ IDENTIFIED BY ‘your_password’;
    GRANT ALL PRIVILEGES ON go_blog.* to ‘go_blog’@‘localhost’;
    FLUSH PRIVILEGES;
    exit;
    

Git

We’re going to clone the project.

  1. Install Git. Eg: sudo yum install git -y.
  2. Clone the project. Eg: git clone https://github.com/aristorinjuang/blog.git.
  3. Import the tables to the database. Eg: mysql -u go_blog -p go_blog < go_blog.sql.

GoLang

Install GoLang to the server and compile it.

  1. Download GoLang. My blog project was on Go 1.16.3. So I downloaded the same version. Eg: wget https://golang.org/dl/go1.16.3.linux-amd64.tar.gz.
  2. Extract the compressed file. Eg: sudo tar -C /usr/local -xzf go1.16.3.linux-amd64.tar.gz.
  3. Add these paths to the .bash_profile Make sure export PATH is also at the end of lines.

    PATH=$PATH:$HOME/.local/bin:$HOME/bin
    PATH=$PATH:/usr/local/go/bin
    PATH=$PATH:/home/ec2-user/go
    
  4. Load the bash profile. Eg: source .bash_profile.
  5. Create the .env file from the example. Eg: cp .env.example .env.
  6. Set the DATABASE and PORT with your configuration.
  7. Install dependencies. Eg: go install.
  8. Create a script for the system to run the web app with the filename blog.service on the folder /etc/systemd/system.

    [Unit]
    Description=A simple blog that runs on Go.
    
    [Service]
    WorkingDirectory=/home/ec2-user/bloge
    ExecStart=/home/ec2-user/go/bin/blog
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
  9. Restart the daemon or systemctl, enable and start the blog. Eg: sudo systemctl daemon-reload && sudo systemctl enable blog && sudo systemctl start blog.
  10. Create a reverse proxy for Nginx. You can set it on the default.conf of Nginx.

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
    
        location / {
            proxy_pass http://127.0.0.1:8000
        }
    }
    

Congratulations

Blog Front Page

Refresh your web browser that is on the public IPv4. You will see the blog is running.

Congratulations. We learn many basics of deployments. The next step is to route the domain using AWS Route 53. Or put the database on AWS RDS. Or using containerization with Docker and AWS ECS. Using AWS S3 and AWS EFS for storage. Many more. Feel free to explore by yourself.

Related Articles

Comments