In this tutorial I will demonstrate how to use Terraform (a Infrastructure as Code Tool), to provision a AWS EC2 Instance and the source code that we will be using in this tutorial will be published to my terraformfiles github repository.
Requirements
To follow along this tutorial, you will need an AWS Account and Terraform installed
You will notice that I am defining my profile name default from the ~/.aws/credentials credential provider in order for terraform to authenticate with AWS.
Next I am defining the main.tf which will be the file where we define our aws resources:
variable "default_tags"{default={Environment="test"Owner="ruan.bekker"Project="terraform-blogpost"CostCenter="engineering"ManagedBy="terraform"}}variable "aws_region"{type= string
default="eu-west-1"description="the region to use in aws"}variable "vpc_id"{type= string
description="the vpc to use"}variable "ssh_keyname"{type= string
description="ssh key to use"}variable "subnet_id"{type= string
description="the subnet id where the ec2 instance needs to be placed in"}variable "instance_type"{type= string
default="t3.nano"description="the instance type to use"}variable "project_id"{type= string
default="terraform-blogpost"description="the project name"}variable "ebs_root_size_in_gb"{type= number
default= 10
description="the size in GB for the root disk"}variable "environment_name"{type= string
default="dev"description="the environment this resource will go to (assumption being made theres one account)"}
The next file is our locals.tf which just concatenates our project id and environment name:
Then our outputs.tf for the values that terraform should output:
1234567891011121314151617
output "id"{description="The ec2 instance id"value= aws_instance.ec2.id
sensitive=false}output "ip"{description="The ec2 instance public ip address"value= aws_instance.ec2.public_ip
sensitive=false}output "subnet_id"{description="the subnet id which will be used"value= var.subnet_id
sensitive=false}
Then lastly our terraform.tfvars, which you will need to supply your own values to match your AWS Account:
Now that all our configuration is in place, we need to intialize terraform by downloading the providers:
1
terraform init
Once the terraform init has completed, we can run a terraform plan which will show us what terraform will do. Since the terraform.tfvars are the default file for variables, we don’t have to specify the name of the file, but since I want to be excplicit, I will include it (should you want to change the file name):
1
terraform plan -var-file="terraform.tfvars"
Now it’s a good time to review what terraform wants to action by viewing the plan output, once you are happy you can deploy the changes by running a terraform apply:
1
terraform apply -var-file="terraform.tfvars"
Optional: You can override variables by either updating the terraform.tfvars or you can append them with terraform apply -var-file="terraform.tfvars" -var="ssh_key=default_key", a successful output should show something like this:
You can access the instance by SSH'ing to the IP that was returned by the output as well as the SSH key name that you provided, or you can make use of the terraform output to access the output value: