Introduction
In today’s cloud-driven world, managing infrastructure efficiently and consistently is crucial for developers and IT professionals alike. Traditionally, setting up infrastructure involved navigating cloud consoles, configuring networks, launching instances, and ensuring everything was correctly connected—a manual process that was both time-consuming and prone to errors, especially when scaling.
Enter Terraform, an open-source Infrastructure as Code (IaC) tool that revolutionizes how we build, change, and version our infrastructure. Instead of manually configuring resources, Terraform is a cloud agnostic IaC tool that allows you to define your infrastructure using a simple, human-readable configuration language called HCL (HashiCorp Configuration Language). This approach not only automates deployments but also ensures consistency across environments and makes tracking changes effortless.
In this blog post, I’ll guide you step by step through deploying a basic infrastructure setup on AWS using Terraform. Whether you're a beginner or just brushing up on your skills, this tutorial will help you get hands-on experience with Terraform and HCL. We’ll cover everything from configuring your environment and writing Terraform code to deploying your first EC2 instance and setting up a simple web server. By the end, you’ll have the foundational knowledge to confidently use Terraform to manage your infrastructure in the cloud.
So, grab a cup of coffee, fire up your terminal, and let’s dive into the world of Terraform and HCL!
1. What You’ll Need
Before we start, ensure you have the following:
AWS Account: You'll need an AWS account to provision. You can see how to create an AWS free tier account .
AWS CLI Installed: Install the AWS Command Line Interface (CLI) to configure your credentials. Follow the instructions for syntax highlighting, code completion, and linting. This will make writing and managing Terraform configurations easier.
2. Configuring AWS Authentication
a. Setting Up AWS Credentials
To allow Terraform to authenticate and interact with AWS, you need to configure your AWS credentials. You can do this using the AWS CLI by running the following command in your terminal:
aws configure
You'll be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and default output format. If you don’t have access keys yet, you can create them in the AWS Management Console under IAM > Users > Security Credentials.
b. Setting Environment Variables (Alternative Method)
Alternatively, you can export your credentials as environment variables:
set AWS_ACCESS_KEY_ID=your_access_key
set AWS_SECRET_ACCESS_KEY=your_secret_key
This method is useful if you don’t want to store credentials in a file and it is also a good way to keep your access key confidential.
3. Writing the Terraform Configuration
In this section, we’ll walk through the Terraform code to deploy an EC2 instance on AWS. This EC2 instance will host a simple web server. Before that, lets take a look at the architecture diagram of the webserver we are creating.
4. Initializing Terraform
Once the configuration files are ready, you need to initialize Terraform. Initialization prepares the working directory and downloads the necessary provider plugins.
Run the following command in your terminal:
terraform init
What It Does: Downloads the AWS provider and initializes your Terraform working directory. This step is essential before running any other Terraform commands.
6. Applying the Configuration
Now that you’ve reviewed the plan, it’s time to apply the configuration and create the infrastructure.
Run the following command:
terraform apply
What It Does: Theapplycommand provisions the resources as defined in your configuration. Terraform will prompt for confirmation before proceeding. Once confirmed, it will create the EC2 instance and security group as shown on the screenshot below.
8. Cleaning Up
When you're done with the infrastructure, it's important to clean up the resources to avoid unnecessary charges. You can do this by running:
terraform destroy
What It Does: Thedestroycommand deletes all the resources that Terraform created. Terraform will prompt for confirmation before proceeding with the deletion.
Feel free to drop any questions in the comments, and happy coding!
SOCIAL SHARE CARD GENERATOR