Setting Up a Go Web Server on Amazon EC2
How to Install and Configure a Go Web Server on Amazon EC2
Launching EC2 with Amazon Linux
Open the AWS Management Console and navigate to the EC2.
Click “Launch instances”.
Enter an appropriate instance name.
Select “Amazon Linux” under “Application and OS Images”.
Select any “Free tier eligible” Instance type.
Create and select any “ppk key” of the “RSA” type.
Allow “SSH traffic”, “HTTPS traffic” and “HTTP traffic” under “Network settings”.
Click “Launch instance”.
Convert pem Key to ppk Key
Open “PuTTYgen”.
Click “Load”.
Now select the pem key you use instance.
Click on “Conversions” → “Export OpenSSH key” → “Yes”.
Enter an appropriate key name. (Eg. go-pem-key.ppm)
Note: Make sure the save as type is “All Files” and to you use the “.ppm“ at the end of the key name as given in the example.
Click “Save”.
Connecting to EC2 instance
Copy this
ssh -i "your-key.pem" ec2-user@<your-ec2-public-ip>
Paste in the Command Prompt.
Edit the pem key with the absolute path and the key name you just created. (Eg. If the absolute path is “C:\Users\username” and the key name is “go-pem-key.pem”. Then replace “you-key.pem” to “C:\Users\username\go-pem-key.pem”.
Go back to AWS Management console, Select the newly created instance.
Copy “Public IPv4 address”.
Replace
<your-ec2-public-ip>
to the copied address.Press enter.
Type “yes” then again press enter.
Uploading Go webserver to EC2 instance
Note: Before uploading the go server to the EC2 instance the go server must set to run on default IP and 8080 port.
Copy and Paste the following one by one.
sudo yum update -y sudo yum install -y golang
You can use scp to transfer your main.go, .env, and any other required files from your local machine to the EC2 instance.
Copy the following
scp -i "your-key.pem" -r "/path/to/your/project" ec2-user@<your-ec2-public-ip>:/home/ec2-user/
Paste this in another Command Prompt Terminal which is not connected to the EC2 instance.
Replace the
"your-key.pem"
to the pem key you created with the absolute path.Replace the
“/path/to/your/project”
to the absolute path of your project (path to the root folder).Replace the
<your-ec2-public-ip>
to the instance’s “Public IPv4 address”.Hit Enter.
Build the application
Copy and Paste
cd /home/ec2-user/your-project-folder
Replace you-project-folder to the name of the root folder.
Hit enter.
Build your Go application to ensure it compiles properly:
go build -o app main.go
Run the application
Once the build is successful, run the application:
./app
Run Your Application in the Background
To keep your application running after logging out, use a process manager like screen or tmux, or run the app as a background process.
nohup ./app &
Configure Security Group for HTTP Access
Select the EC2 instance.
Under “Security” click on the “Security groups”.
Scroll and click on “Inbound rules”.
Click on “Edit inbound rules”.
Click “Add Rules”.
Select “Custom TCP” under “Type”, set “8080” in “Port range” and “Anywhere-IPv4” under “Destination”.
Click “Save rules”.
Now Click on “Outbound Rules”.
Click “Edit outbound rules”.
Click “Add Rules”.
Select “Custom TCP” under “Type”, set “8080” in “Port range” and “Anywhere-IPv4” under “Destination”.
Click “Save rules”.
Test the Web Server
Use the curl command with the public IP and port.
For example curl http://<Public-IP>:8080/path_to _service.
Replace <Public-IP>
with the instance’s Public IPv4 address and path-to-service
with the path where any GET request is sent.