Terraform is an open-source infrastructure as code (IaC) tool that allows you to define and provision infrastructure resources in a declarative manner. It enables you to create, manage, and update infrastructure across multiple cloud providers and on-premises environments.
Here I'll be installing Terraform in the VM of Azure and creating the resources in AWS with it.
Creating a VM in Azure
Connecting to VM via SSH:
Installing Terraform
You can download the latest version of Terraform from the official website (https://www.terraform.io/downloads.html). Terraform can be installed in Ubuntu with the following command:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Connecting to AWS
Creating an access key for a user who has full access to ec2.
Defining Infrastructure
Create a new directory for your Terraform project. Inside the directory, create a file with a .tf
extension (e.g., main.tf
) to define your infrastructure. Terraform configurations are written in HashiCorp Configuration Language (HCL) or JSON syntax.
In your main.tf
file, specify the provider you want to use and configure it with your credentials and settings.
provider "aws" {
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
region = "us-west-2" # Specify your desired AWS region
}
When you run the terraform init
command, it initializes your Terraform project. It performs the following tasks:
Downloads Providers: Terraform initializes by downloading the necessary provider plugins required for the providers specified in your configuration. Providers are responsible for managing resources in different cloud platforms or infrastructure technologies.
Initializes Backend: If you have configured a backend for storing the Terraform state,
terraform init
sets up the backend connection. The backend can be a remote storage service like Amazon S3 or a local file system.Retrieves Modules: If your configuration includes modules, Terraform retrieves the module code from the specified source locations. Modules are reusable components that encapsulate infrastructure resources and configurations.
Terraform will analyze your configuration, check for any required provider plugins that are not already downloaded, and download them into a .terraform
directory within your project.
During initialization, you may see logs indicating the progress and status of the plugin downloads. Once the initialization is complete, you can proceed with other Terraform commands like terraform plan
and terraform apply
to manage your infrastructure.
Remember to run terraform init
whenever you make changes to your configuration or when setting up a new Terraform project. This ensures that Terraform is properly configured and ready to manage your infrastructure.
Creating resources
Define the resources you want to create using Terraform. Resources can be instances, networks, storage, security groups, and more. Here we'll be creating 10 EC2 instances.
In the main.tf file, append the following HCL code.
resource "aws_instance" "ec2_instance" {
count = 10
instance_type = "t2.micro"
ami = "ami-0d04e6652cb408e57" # Replace with your desired AMI ID
# Additional resource configuration as needed
tags = {
Name = "ec2-instance-${count.index + 1}"
}
}
The count
argument is set to 10, indicating that we want to create 10 instances.
You can add additional configuration properties to the aws_instance
resource block as needed, such as security groups, subnet IDs, and user data.
To create the EC2 instances, open a terminal, navigate to your Terraform project directory, and run the following commands:
Initialize Terraform:
terraform init
Terraform Validate
The terraform validate
command is used to validate the syntax and configuration of your Terraform files without actually executing any changes. It checks for errors, warnings, and potential issues in your Terraform configuration. Terraform will scan your configuration files and provide feedback on any syntax errors or configuration issues it encounters. If there are no errors, it will display a success message indicating that the configuration is valid. If there are errors, it will display error messages detailing the issues found. You'll need to review and fix these errors in your configuration files.
Note that terraform validate
does not perform any checks against the actual resources or services in your cloud provider. It focuses solely on validating the Terraform configuration files for correctness.
By running terraform validate
, you can catch common configuration errors and ensure that your Terraform files are properly structured and syntactically correct. It's a good practice to run this command before applying any changes to avoid potential issues during the provisioning process.
Preview the changes (optional):
terraform plan
This command shows you a preview of the resources that will be created.
Apply the changes:
terraform apply
Terraform will create the 10 EC2 instances according to the defined configuration.
Checking the newly created instances
Logging into one of the instances:
We didn't define any key pair while creating the instances, so I'm using an AWS native Instance Connect option.
Installing Apache web server
Creating an index.htm file in /var/www/html and writing a single page code for a portfolio website.
Adding inbound rules to access the web server.
ec2-instance-10 was not performing correctly, so I deleted that instance.
Connecting to ec2-instance-7
httpd is installed in a new machine and an index file is added. The web page is then accessed from a public IP address.
Destroying the resources
All the created resources can be destroyed using a single command
terraform destroy
All the instances created by Terraform are deleted.
Deleting the VM from Azure
Thank you for reading! Happy Learning!