# Setting Up a Go Web Server on Amazon EC2

### Launching EC2 with Amazon Linux

1. Open the **AWS Management Console** and navigate to the **EC2.**
    
2. Click “**Launch instances”**.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726769000527/aeef113e-f502-462f-9ebc-5763017df517.png align="center")
    
3. Enter an appropriate instance name.
    
4. Select “**Amazon Linux**” under “**Application and OS Images”.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726769073771/d2bb842e-1e2a-4dc2-8295-90173d433680.png align="center")
    
5. Select any “**Free tier eligible”** Instance type.
    
6. Create and select any **“ppk key”** of the “**RSA**” type.
    
7. Allow “**SSH traffic**”, “**HTTPS traffic**” and “**HTTP traffic**” under “**Network settings”.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726769330360/af8da0d5-04fd-4b6c-bb10-0d8554a8f6e9.png align="center")
    
8. Click “**Launch instance”**.
    

### Convert pem Key to ppk Key

1. Open “**PuTTYgen”**.
    
2. Click “**Load”**.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726772394839/28b9579b-b67d-472e-bfe1-05e4cc6148db.png align="center")
    
3. Now select the pem key you use instance.
    
4. Click on “**Conversions”** → “**Export OpenSSH key**” → “**Yes**”.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726772584130/8f108d50-b4a6-408d-9547-53ec7bed0ed7.png align="center")
    
5. 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.
    
6. Click “**Save”**.
    

### Connecting to EC2 instance

1. Copy this
    
    ```bash
    ssh -i "your-key.pem" ec2-user@<your-ec2-public-ip>
    ```
    
2. Paste in the Command Prompt.
    
3. 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”**.
    
4. Go back to **AWS Management console**, Select the newly created instance.
    
5. Copy “**Public IPv4 address**”.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726777158376/0cd23e0f-4958-410d-a257-9775d150e473.png align="center")
    
6. Replace `<your-ec2-public-ip>` to the copied address.
    
7. Press **enter**.
    
8. 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.

1. Copy and Paste the following one by one.
    
    ```bash
    sudo yum update -y
    sudo yum install -y golang
    ```
    
2. 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
    
    ```bash
    scp -i "your-key.pem" -r "/path/to/your/project" ec2-user@<your-ec2-public-ip>:/home/ec2-user/
    ```
    
3. Paste this in another Command Prompt Terminal which is not connected to the EC2 instance.
    
4. Replace the `"your-key.pem"` to the pem key you created with the absolute path.
    
5. Replace the `“/path/to/your/project”` to the absolute path of your project (path to the root folder).
    
6. Replace the `<your-ec2-public-ip>` to the instance’s **“Public IPv4 address”**.
    
7. Hit Enter.
    

### Build the application

1. Copy and Paste
    
    ```bash
    cd /home/ec2-user/your-project-folder
    ```
    
    Replace you-project-folder to the name of the root folder.
    
2. Hit enter.
    
3. Build your Go application to ensure it compiles properly:
    
    ```bash
    go build -o app main.go
    ```
    

### Run the application

Once the build is successful, run the application:

```bash
./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.

```bash
nohup ./app &
```

### Configure Security Group for HTTP Access

1. Select the EC2 instance.
    
2. Under “**Security**” click on the “**Security groups**”.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726776182555/7ebf7fb4-2a5b-4f89-b243-3af0f5eab8d3.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726775828601/2629aac3-e422-42c2-989d-9d800a66202a.png align="center")
    
3. Scroll and click on “**Inbound rules**”.
    
4. Click on “**Edit inbound rules**”.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726775952687/5d702c1a-fe57-4850-a4dd-bfc087947bae.png align="center")
    
5. Click “**Add Rules**”.
    
6. Select “**Custom TCP**” under “**Type**”, set “**8080**” in “**Port range”** and “**Anywhere-IPv4”** under **“Destination”.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726776883068/8ea10133-3168-41c5-954c-0799e8c5ec7b.png align="center")
    
7. Click “**Save rules**”.
    
8. Now Click on “**Outbound Rules**”.
    
9. Click “**Edit outbound rules**”.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726776412640/8f5245c7-1043-4ddc-a8ee-453933ccd002.png align="center")
    
10. Click “**Add Rules**”.
    
11. Select “**Custom TCP**” under “**Type**”, set “**8080**” in “**Port range”** and “**Anywhere-IPv4”** under **“Destination”.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726776783176/b723208e-72c7-4a9f-8946-03476e7d4afa.png align="center")
    
12. Click “**Save rules**”.
    

### Test the Web Server

Use the curl command with the public IP and port.

For example curl http://&lt;Public-IP&gt;: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.
