Deploy Go Applications to ECS using AWS CDK

Cagdas Ozbey
TysonWorks
Published in
2 min readJul 23, 2019
AWS Cloud Development Kit

AWS CDK(Cloud Development Kit) is a tool for managing AWS resources. CDK allows us to use common programming languages to provision and manage complex cloud workloads on AWS. We’ve introduced our readers to CDK earlier. In this post, we are going to walk you through the deployment of basic Go application to Amazon Elastic Container Service using CDK.

Let’s start with the Go application first. We are using the mux library here. We create two API handlers and we will get a random response when we hit the endpoints.

Now, we need to write a Dockerfile for our Go application. We are going to use a multi-stage layout to speed up the build process.

All set. We can start provisioning AWS resources.

We are creating a new VPC, new ECS Cluster and a Fargate Service with a Load Balancer. We are using built-in CDK tools to build Docker images locally and push it to ECR repo, which will be created by CDK during the bootstrapping step.

To Deploy the stack:

# Install dependencies
npm install
# Edit .env file for environment variables
vim .env
# We need to bootstrap environment before we deploy (This will create the necessary resources before the deployment)
cdk bootstrap
# Deploy using CLI
cdk deploy

After a successful deployment, we should be seeing the URL of the service.

Try hitting /dogs and /dogs{name} paths. You should be seeing data coming from our server.

After you’re done with the tutorial, don’t forget to destroy the resources we’ve created earlier.

cdk destroy

The completed project can be found here:

Are you ready to enhance your AWS Cloud journey? Head over to our website and book a free consultation call

--

--