Today I will show you how to build an infrastructure in AWS using Terraform. First let me tell you what is Terraform.
What is Terraform?
Terraform is an infrastructure as code tool offered by HashiCorp, it allows you to build, change and manage infrastructure in a safe, repeatable way. It’s mainly used to manage environments with a configuration language called HashiCorp Configuration Language (HCL) for human-readable, automated deployments.
Before we move any further, I will like to explain what is Infrastructure as Code (IAC). Its a process of managing infrastructure in a file or files rather than manually configuring resources in user interface. Terraform allows the use of HCL (HashiCorp Configuration Language) to author files containing definitions of their desired resources for any cloud provider.
What are the advantages of using Terraform?
Terraform is platform agnostic, meaning it allows you to manage a heterogeneous environment with the same workflow by creating a configuration file to fit the needs of your project or organization. Overall Terraform is fairly easy to use as it has repeatable operations and a planning phase to allow users to ensure the actions taken by Terraform will not cause disruption in the their environment.
In order to follow these steps you will need an AWS account. If you don’t have an account, you can create one here.
Now that you have your AWS account, you need to install AWS CLI, see details here .
I’m using Visual Studio Code, which is making the entire process much easier.
Please note that I’m using a Linux machine.
I create a new folder in my desktop call myTerraformProject1. In that folder I create a new file call configuration.tf
Now in Visual Studio Code open the configuration.tf file.
You can now enter all the information needed to launch your EC2 Instance.
For provider in this case I will enter AWS, due to the fact that we are using AWS as a provider.
For region I will recommend that you use that region that is closer to you. In this case its us-east-1 for me.
Resource here defines the piece of infrastructure, in this case we are using an AWS instance and the name of the instance is FIRSTINSTANCE.
The AMI is the Amazon Machine Image which provides information required to launch an EC2 instance.
The instance type is the t2.micro, which falls under the free tier.
Now that you have your infrastructure setup you and now launch Terraform through the terminal using Visual Studio Code.
Make sure you are under the correct folder
Enter: Terraform init
Terraform init allows you to initialize the directory and creates a state file, it will download the plugins associated with the provider. In this case the provider is AWS.
Now that Terraform was successfully initialized, you can now proceed to the planning phase.
Enter: Terraform Plan
Terraform plan looks at the resources in its Terraform state file and makes sure everything looks OK.
Now it’s time to apply all the changes. Terraform will make all the necessary API calls to deploy the changes.
Enter: Terraform Apply
Now your EC2 Instance is launched, you can now see the message listed below.
You can always double check by accessing your AWS Console to see the EC2 Instance.
In order to save money, I will now destroy the instance.
Enter: Terraform destroy
You will be asked to confirm the destruction of the instance.
You are now a Terraform expert 😆, I will suggest that you continue using Terraform to make changes to your infrastructure.