Ok, so you have Terraform installed, well… neat. Remember this is from the perspective of a guy learning Terraform, so come along with me and let’s learn together.
Let’s get into the nitty-gritty and start learning what this whole ‘infrastructure as code’ is really all about. The first thing we are going to do is open a Terminal/cmd prompt and go to the Terraform folder.
Note: On mac I had to run all terraform commands with “sudo”. I went through some changes so I could “sudo -su” the terminal to raise the permissions.
touch example.tf
or for cmd prompt
type nul > "example.tf"
Now that you have the example folder, it’s time to fill it in with the needed information for a basic apply…
Note: It’s best to go ahead and create a free account with AWS. Here is a great link to start your free account.
Now open the “example.tf” file to edit using either “vim example.tf”, (Probably need sudo) or opening notepad.
Paste the following:
provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0338bce19a7cb103e
instance_type = "t2.micro"
}
Update the access_key and secret_key with the needed information. To find this in AWS you would go to the IAM to create a user. Once the user is created, go back into the user in IAM and under “Security Credentials” and click “Create Key”.
I updated the ami to a newer template. When I tried to run the apply, the base template would not work from the site. On vim run your “:wq” or “ctrl + s” in notepad to save the work. Now run the following command
terraform init
You should see something similar to the following:
Ok, now the fun begins. You now have the AWS provider, and you have a file you can utilize for deployments. Next run:
terraform plan
You should see the following:
And finally:
terraform apply
Which will deploy the instance to your EC2 account.
Cool! But whats cooler than building stuff?? DESTROYING IT WHA HA HA… ok..
Terraform makes that easy to:
terraform destroy
This will run in realtime and loop until the instance is destroyed. I really liked that as a feature because once the destroy command is done, the machine is really gone. Pretty awesome. I plan to move through this into more variables, and into file structures per provider. Also, I really want to dig into how this will work with vRA and Azure. Stay tuned.