Deploying Docker & Go to AWS Beanstalk
Recently, I had an opportunity to deploy a Go application into AWS Elastic Beanstalk, and I was surprised at how painless the deployment was. Without having to worry about infrastructure issues, I was able to get my Go app deployed.
Below is a break down, from start to finish, on how to get a simple Go app out to Elastic Beanstalk using docker.
Before we get started, there are a few things that need to be setup/installed:
And away we go!
Directory and Beanstalk initialization
Once all of the prerequisites are setup, we can begin by creating a new folder for where our application will live.
I like to start off by running
eb init — This will initialize Beanstalk configs for us.
Next, this will prompt a couple of questions:
- Which region you will want this application to run
- The name of your application
- Platform — Docker should be chosen since we are deploying with docker
- Docker Version — I chose the default version which at the time was
- Setup SSH — I did not enable this however you may
eb initis done running, you should see two hidden files in your directory:
- .gitignore — basic git ignore file
- .elasticbeanstalk — this folder will contain configuration files for beanstalk
The Go App
This will be a simple http app — I will however be using gorilla mux for routing. In the root directory, create a
main.go file which looks like this:
For this project, I am using Go modules to handle my dependencies to configure Go mod by running the following two commands :
➜ go mode init eb-go
➜ go mod tidy
If you want to test the app you can run
go build and check that
localhost:8081/hello prints out
Dockerizing the app
So now that we have our go app its time for us to write our dockerfile.You will need to create a file called
Dockerfile in the projects root directory.
This dockerfile is a very straight forward and simple approach to containerizing our Go app.
- Create a working directory inside the container
- Copy our entire application folder into the container
- Build the Go binary and name it
- Expose port 8081, which is the port our Go app accepts connections on
- Tell Docker what to run when we start the container
The next thing I would recommend is writing a
.dockerignore file. This will prevent docker from copying unnecessary files during the copy build process
If you want to test the docker container you can do so by running the following commands in your root directory:
➜ docker build -t eb-go .
➜ docker run --rm -p8081:8081 eb-go
docker run the container should be running on
localhost:8081 which means you are able to hit our
/hello endpoint and see “hello world” printed out.
Some side information; if you run
docker ps in a new terminal window while docker run is running, you can see some information about eb-go running.
Deploying to Beanstalk
Now we have prepped everything we will need to deploy our code to beanstalk. The project directory should look similar to this:
Next, we need to create the beanstalk environment. When we originally ran
eb init, this only configured the config file that will be required during the environment creation process.
eb create command will configure your beanstalk environment as well as do an initial deployment of your application (note this may take a few minutes).
➜ eb create eb-go-app
And just like that your app has been deployed! Now there are two ways to inspect the status and general information about your app.
One way is through the AWS elastic beanstalk console as shown below.
The other way would be through the eb cli. If you run
eb status which will print out this information:
➜ eb status
Environment details for: eb-go-app
Application name: eb-go-app
Deployed Version: app-181009_204155
Environment ID: e-iwpmhptanp
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Docker running on 64bit Amazon Linux/2.12.3
Updated: 2018-10-10 00:45:16.725000+00:00
Lets update our app to have a new endpoint called
/goodbye — In main.go we add :
Now to deploy our new changes it is as simple as running
eb deploy .
**Note about using git and beanstalk. If your project is using git beanstalk will only deploy the last commit. So if you have uncommitted code it will not get deployed. This is due to the eb cli taking a zip of the last git commit and using that during the deploy process.
Hopefully this helped demonstrate how easy it is to deploy to beanstalk using go and docker! With just a few CLI commands you can quickly get your app off the ground and not have to worry about infrastructure.
All the source code can be found at https://github.com/ddymko/Go-Beanstalk