A "Hello World" in Golang deployed to AWS ECS (Docker) using Terraform
Big thanks to Tadas Vilkeliskis for Bootstrapping Docker Infrastructure With Terraform write up.
-
Make sure you have the following installed and/or created/configured/working:
- Docker
- Docker Hub account
- Golang
- Terraform
- AWS access with admin priviliges and your AWS CLI is working
- AWS Key Pair
-
Allow Terraform to use your credentials in
~/.aws/config
by symlinking it tocredentials
:
ln -s ~/.aws/confic ~/.aws/credentials
- Install this example:
go get github.com/grisha/hello-go-ecs-terraform
- Change into the project directory:
cd $GOPATH/src/grisha/hello-go-ecs-terraform
- Edit
tf/variables.tf
file so that it has the following:
variable "key_name" { default = "YOUR-AWS-KEY-PAIR-NAME" }
variable "dockerimg" { default = "YOUR-DOCKER-HUB-USERNAME/IMAGE-NAME" }
- The docker hub username must be correct (though the actual image doesn not need to exist, it will be created for you), and you should be authenticated with:
docker login
- Check that terraform works with:
make plan
- If the above produces no errors, give it a try. Note that due to timing of AWS object creations sometimes you have to run this twice, it succeeds on the second try:
make apply
-
You should now see a load balancer in the AWS console, where it should list its DNS name. You should also see an ECS service and its tasks and associated EC2 instances. The whole thing will take a few minutes to create.
-
Once it's all created, you should be able to hit the ELB DNS name with your browser and see the app in action.
-
In this set up Terraform uses the
.git/logs/HEAD
file as the indicator that code has changed, but this file only changes when you commit something (The idea being that your CI, e.g. Jenkins would actually perform themake apply
). If you want to force deploy the code that you currently have, you can do this:make force_deploy
Once you do this, you should see the ECS gradually replace your tasks with the new version.
-
When finished, you can destroy everything with:
make destroy