Getting Started with Terraform to Deploy an EC2 Instance on AWS

Today, I dove into the basics of Terraform and explored its capabilities by deploying an EC2 instance on AWS. Here’s a quick overview of my journey, from understanding Terraform's purpose to using GitHub Codespaces to run the deployment.

📌 What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, manage, and deploy cloud infrastructure through code, which means you can treat your infrastructure the same way you manage your application code—versioned, reusable, and consistent. Terraform supports multiple providers, including AWS, Azure, and GCP, making it an ideal tool for multi-cloud environments.

🛠 Setting Up Terraform in GitHub Codespace

To get hands-on experience, I used GitHub Codespaces, a cloud-based development environment that allowed me to configure my workspace with Terraform and the AWS CLI. Here's a breakdown of the setup steps:

  1. Installed AWS CLI and Terraform:
    I installed AWS CLI to interact with AWS services and Terraform to provision and manage the EC2 instance.

  2. Configured AWS CLI for Authentication:
    Using aws configure, I provided my AWS Access Key ID and Secret Access Key to authenticate with my AWS account. I also specified my preferred region (us-east-1 in this case).

  3. Created a Terraform Configuration File (main.tf):
    The .tf file defines the desired infrastructure. Here’s the content of my main.tf:

     hclCopy codeprovider "aws" {
         region = "us-east-1"  # set your desired AWS region
     }
    
     resource "aws_instance" "this" {
         ami           = "ami-0866a3c8686eaeeba"  # Amazon Linux 2 AMI
         instance_type = "m5.large"               # Instance type
     }
    
    • The provider "aws" block configures Terraform to use the AWS provider and specifies the region.

    • The aws_instance resource creates an EC2 instance with the specified AMI and instance type.

🚀 Deploying the EC2 Instance

Once the .tf file was ready, I went through these commands in the terminal:

  1. Initialize Terraform:

     bashCopy codeterraform init
    

    This initializes the workspace, downloading the necessary plugins for AWS.

  2. Plan the Deployment:

     bashCopy codeterraform plan
    

    This command lets you preview what Terraform will do when you apply the changes.

  3. Apply the Deployment:

     bashCopy codeterraform apply
    

    Confirming this command allows Terraform to create the EC2 instance based on the configuration file. Within moments, the EC2 instance was deployed!

🌟 Key Takeaways

  • Understanding Terraform’s Role in IaC: Terraform makes deploying infrastructure as simple as writing code. It’s scalable, reusable, and supports multiple cloud providers.

  • Simplified Development with GitHub Codespaces: Codespaces provided a quick, cloud-based environment where I could install tools and work directly with Terraform.

  • Using the AWS CLI for Authentication: With aws configure, managing AWS authentication was straightforward and allowed Terraform to access my AWS account securely.

In just one session, I experienced how Terraform can bring consistency and version control to cloud infrastructure, simplifying complex deployments. This knowledge sets the stage for more advanced configurations and multi-resource deployments in the future!

Conclusion

Today’s exploration of Terraform was just the beginning. Looking ahead, I’m excited to dive deeper, exploring modules, state management, and other advanced features that make Terraform so powerful in cloud infrastructure management.

Let me know if you’ve had similar experiences or tips on taking Terraform further! 🚀

0
Subscribe to my newsletter

Read articles from Aakanchha Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aakanchha Sharma
Aakanchha Sharma

👋 Welcome to my Hashnode blog! I'm a tech enthusiast and cloud advocate with expertise in IT, backup solutions, and cloud technologies. Currently, I’m diving deep into Python and exploring its applications in automation and data management. I share insights, tutorials, and tips on all things tech, aiming to help fellow developers and enthusiasts. Join me on this journey of learning and innovation!