Deploying Go Applications to AWS App Runner: A Step-by-Step Guide
Tags: go, tutorial, programming, aws
Published: 2023-03-02 10:17:13

***...With the managed runtime for Go***

In this blog post you will learn how to run a Go application to AWS App Runner using the [Go platform runtime](https://docs.aws.amazon.com/apprunner/latest/dg/service-source-code-go1.html). You will start with an [existing Go application on GitHub](https://github.com/abhirockzz/apprunner-go-runtime-app) and deploy it to AWS App Runner. The application is based on the [URL shortener application](https://dev.to/aws/build-a-serverless-url-shortener-with-go-10i2) (with some changes) that persists data in DynamoDB.

**What's covered?**

  - [Prerequisites](#prerequisites)
  - [Create a GitHub repo for the URL shortener application](#create-a-github-repo-for-the-url-shortener-application)
  - [Create a DynamoDB table to store URL information](#create-a-dynamodb-table-to-store-url-information)
  - [Create an IAM role with DynamoDB specific permissions](#create-an-iam-role-with-dynamodb-specific-permissions)
  - [Deploy the application to AWS App Runner](#deploy-the-application-to-aws-app-runner)
  - [Verify URL shortener functionality](#verify-url-shortener-functionality)
  - [Clean up](#clean-up)
  - [Conclusion](#conclusion)

AWS App Runner can create and manage services based on two types of service sources: 

- Source code (covered in this blog post)
- Source image

**Source code** is nothing but your application code that App Runner will build and deploy. All you need to do is point App Runner to a source code repository and choose a suitable runtime that corresponds to a programming platform version. App Runner provides platform-specific managed runtimes (for Python, Node.js, Java, Go etc.).

The **AWS App Runner Go platform runtime** makes it easy to build and run containers with web applications based on a Go version. *You don't need to provide container configuration and build instructions such as a Dockerfile.* When you use a Go runtime, App Runner starts with a *managed* Go runtime image which is based on the [Amazon Linux Docker image](https://hub.docker.com/_/amazonlinux) and contains the runtime package for a version of Go and some tools. App Runner uses this managed runtime image as a base image, and adds your application code to build a Docker image. It then deploys this image to run your web service in a container.

## Prerequisites

You will need an AWS account and [install AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).

Let's get started....

## Create a GitHub repo for the URL shortener application

Clone this [GitHub repo](https://github.com/abhirockzz/apprunner-go-runtime-app) and then upload it to a GitHub repository in your account (keep the same repo name i.e. `apprunner-go-runtime-app`)

```
git clone https://github.com/abhirockzz/apprunner-go-runtime-app
```

## Create a DynamoDB table to store URL information

Create a table named `urls`. Choose the following:

- Partition key named `shortcode` (data type `String`) 
- `On-Demand` capacity mode


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu2aq43o9zc0eo95yx90.png)

## Create an IAM role with DynamoDB specific permissions

```bash
export IAM_ROLE_NAME=apprunner-dynamodb-role

aws iam create-role --role-name $IAM_ROLE_NAME --assume-role-policy-document file://apprunner-trust-policy.json
```

Before creating the policy, update the `dynamodb-access-policy.json` file to reflect the DynamoDB table ARN name.

```bash
aws iam put-role-policy --role-name $IAM_ROLE_NAME --policy-name dynamodb-crud-policy --policy-document file://dynamodb-access-policy.json
```

## Deploy the application to AWS App Runner

> If you have an existing AWS App Runner GitHub connection and want to use that, skip to the **Repository selection** step.

**Create AWS App Runner GitHub connection**

Open the App Runner console and Choose **Create service**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbxxb7zdqo3kkv7eh3iw.png)

On the **Source and deployment** page, in the **Source** section, for **Repository** type, choose **Source code repository**. Under **Connect to GitHub** choose **Add new**, and then, if prompted, provide your GitHub credentials.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz0m91egr1o8zxwcdz3f.png)

In the **Install AWS Connector for GitHub** dialog box, if prompted, choose your GitHub account name. If prompted to authorize the *AWS Connector for GitHub*, choose **Authorize AWS Connections**. Choose **Install**.

Your account name appears as the selected GitHub account/organization. You can now choose a repository in your account.

**Repository selection**

For **Repository**, choose the repository you created - *apprunner-go-runtime-app*. For **Branch**, choose the *default* branch name of your repository (for example, **main**). 

**Configure your deployment**: In the **Deployment** settings section, choose **Automatic**, and then choose **Next**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsdulfq772vb9hst6tgc.png)

**Configure application build**

On the **Configure build** page, for **Configuration** file, choose **Configure all settings** here.

Provide the following build settings:

- Runtime – Choose *Go 1*
- Build command – Enter *go build main.go*
- Start command – Enter *./main*
- Port – Enter *8080*

Choose **Next**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oklfp2n6n7ssu6ujm9oj.png)

**Configure your service.**

Under **Environment variables**, add an environment variable. For **Key**, enter `TABLE_NAME`, and for **Value**, enter the name of the DynamoDB table (`urls`) that you created before.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sjmfpcj4c44so8qw99b.png)

Under **Security > Permissions**, choose the IAM role that you had created earlier (`apprunner-dynamodb-role`).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fk6v91dqcy2h86rmzstj.png)

Choose **Next**.

On the **Review and create** page, verify all the details you've entered, and then choose **Create and deploy**. If the service is successfully created, the console shows the service dashboard, with a Service overview of the application.

## Verify URL shortener functionality

The application exposes two endpoints:

1. To create a short link for a URL
2. Access the original URL via the short link

First, export the App Runner service endpoint as an environment variable, 

```bash
export APP_URL=<enter App Runner service URL>

# example
export APP_URL=https://jt6jjprtyi.us-east-1.awsapprunner.com
```

Invoke it with a URL that you want to access via a short link.

```bash
curl -i -X POST -d 'https://abhirockzz.github.io/' $APP_URL

# output
HTTP/1.1 200 OK
Date: Thu, 21 Jul 2022 11:03:40 GMT
Content-Length: 25
Content-Type: text/plain; charset=utf-8

{"ShortCode":"ae1e31a6"}
```

You should get a `JSON` response with a short code and see an item in the `DynamoDB` table as well.

You can continue to test the application with a few other URLs.

**To access the URL associated with the short code**

Enter the following in your browser `http://<enter APP_URL>/<shortcode>`

For example, when you enter `https://jt6jjprtyi.us-east-1.awsapprunner.com/ae1e31a6`, you will be re-directed to the original URL.

You can also use `curl`. Here is an example:

```bash
export APP_URL=https://jt6jjprtyi.us-east-1.awsapprunner.com

curl -i $APP_URL/ae1e31a6

# output
HTTP/1.1 302 Found
Location: https://abhirockzz.github.io/
Date: Thu, 21 Jul 2022 11:07:58 GMT
Content-Length: 0
```

## Clean up

Once you complete this tutorial, don't forget to delete the following:

- DynamoDB table
- App Runner service

## Conclusion

In this blog post, you learned how to go from a Go application in your GitHub repository to a complete URL shortener service deployed to AWS App Runner!