Create instances
Version-able infrastructure with Terraform using AWS as the provider.
Installing terraform in your computer
Visit the downloads page and download the appropriate binary for your system. Unzip the download and place the binary in well known path like /usr/local/bin
or$HOME/bin
. Type the terraform
command to check its working. For more specific instrucions, follow the official guide: Install Terraform
Getting started
We are going to create 3 instances. The first will be for the Consul, Zookeeper, Mesos, and Marathon master daemons. The remaining two will be used as Mesos agents to run our applications.
Lucky for you we've put together a code repository for this tutorial. Feel free to clone it; all the.tf
extension files are related to Terraform.
When Terraform is run it first globs all the .tf
files, creates a dependency graph of the resources declared, compares that against any state that might exist, and then offers you the ability to create, modify, or detroy the resources defined.
terraform plan
will do a dry run of what will be changed to the state of infrastructure if we were to apply our current resource definitions. terraform apply
will actually change the state of the infrastructure. Terraform asks for access_key_ and _secret_key when you run plan
or apply
variables.tf
Variables like project name, ssh_public_key, instance type, ami (amazon machine image), region, availability_zone, cidr are specified
The AWS access_key _and secret_key _are filled by user for every command execution.
provider.aws is an inbuilt component is responsible for all aws related commands to get executed by terraform
vpc_and_networking.tf
Creates a virtual private network with cidr block
Creates an internet gateway for the vpc that is created earlier
Creates a subnet within the vpc with specified cidr block
Creates a routing table and routing table association for the vpc's gateway
Creates a security group which has all ingress and egress rules used for all instances.
instances.tf
Creates a key in aws ec2 for that region. The key name and ssh public key are from variables.tf
All the above components are used in creating these resources
Logs the public ip for instances created
terraform.tfstate and terraform.tfstate.backup
These are the json files used by terraform to stores the state of infrastructure and understands them too.
Check the AWS web console EC2 tab to see the changes reflected. Play around !