Deploying Node to AWS with Terraform

2019-03-01 docker terraform aws ec2 node

DevOps Automation

A DevOps mindset requires “Highly effective, daily collaboration between software developers and IT operations people to produce relevant, working systems” —

In order to build and operate the kinds of complex, distributed software systems required for 2018 and beyond, we need to emphasize effectiveness over efficiency for technical teams. Delivering changes rapidly, reliably, and repeatedly is not possible if we aim to minimize ‘costs’ at specific points of the value chain, as this kind of efficiency usually ends up causing unnecessary constraints. Instead, we should focus on flow and completion of work in progress. —

What is Terraform?

Terraform is IaC (Infastructure as Code) middleware that uses declarative markup to automate the lifecycle of cloud infastructure in a cloud vendor agnostic manner. I’ll be using the AWS Provider, but you could easily deploy to Azure or GCP.

Using a Build Container

In this example, Docker is utilized to run a Terraform build container. The benefit of containerizing a build, is isolating build dependencies from the operating system. Common CI/CD platforms like CircleCI also use build containers, so this is a common approach. The container mounts a local plan and temporary folders to share Terraform state and SSH Keys. The entry point initializes Terraform. The plan creates a typical VPC, with internet gateway, a route table, private and public subnets, security group allowing HTTP Port 80 traffic to the client IP, and IAM role with access to S3. The Node.JS server source code is uploaded to a private S3 bucket. When the EC2 instance is created, user data copies the Node JS source from S3, and installs the server as a service. Terraform will print the public IP address of the Node server when the plan is complete.

All you need is Linux, Docker, and an AWS account to get started.

Check out the source code here.