Deploying Go Application Using EC2 and CodeDeploy
June 28, 2021
IAM - Role
- The IAM role for CodeDeploy is used to interact with EC2 instance(s).
- The one for EC2 is used to read S3.
- You can find the detailed instruction here: "Continuous Deployment with AWS CodeDeploy & Github".
EC2 - Launch
I usually prefer using the AWS console to configure and update AWS resources. The below instruction also assumes that a reader uses the AWS website to deply EC2 instance. When we try to launch an EC2 instance, we can enter User Data type so that the instance installs software without our intervention. The below script is useful to install code deploy for an instance with Ubuntu.
sudo apt update sudo apt install -y ruby cd /home/ubuntu aws s3 cp s3://aws-codedeploy-us-west-1/latest/install . chmod +x ./install sudo ./install auto
If you are interested in a script for a Linux instance, you can find the relevant script in "Continuous Deployment with AWS CodeDeploy & Github".
Make sure to have
.pem file so that you can access the deployed EC2 instance via CLI.
EC2 - Setup
If the deployed EC2 instance is Linux, enter the following to install Go:
sudo yum update -y sudo yum install -y golang
In "Deploying a Go application on AWS EC2", @mlabouardy discusses the environment variables (GOROOT, GOPATH and PATH), but it looks we do not need to do so anymore. To ensure that Go is properly installed, enter
go in your command line and see a response.
If you chose ubuntu AMI for your instance, enter the following on your CLI to install GO:
$ sudo add-apt-repository ppa:longsleep/golang-backports $ sudo apt update $ sudo apt install golang-go
One of the reasons why I wanted to write about running Go app on EC2 is because I had difficulty in dealing with issues related to CORS. In my case, I installed cors so that my React app can interact with my Go application. When I run my React app on localhost, my app sent
POST request to my Go application, and a response was received without any problem. However, when the app was deployed via Vercel or Netlify, it was not working. Some of my findings are below:
- Make sure that
OPTIONSrequest is handled. It is likely that a request from a client triggers CORS preflight becuase many requests are not "simple requests"
- It is possible to do a CORS quest from HTTPS domain to other HTTPS domains, but not to HTTP domains because they are not secure.