Alfred Brown

DevOps Engineer

Blogger

Blog Post

Creating an EC2 instance in AWS via Terraform

June 7, 2024 Terraform AWS EC2
Creating an EC2 instance in AWS via Terraform

Introduction

In today’s cloud-centric world, Infrastructure as Code (IaC) has revolutionized the way we manage and provision resources. Terraform, an open-source tool by HashiCorp, allows you to define and provision your infrastructure using a high-level configuration language. In this blog post, we will walk through the process of creating an Amazon EC2 instance in AWS using Terraform. Whether you’re new to Terraform or looking to refine your skills, this guide will provide you with a comprehensive overview.

Prerequisites

Before we dive in, make sure you have the following prerequisites:

  • AWS Account: An active AWS account with necessary permissions.
  • Terraform Installed: Terraform installed on your local machine. You can download it from the Terraform website.
  • AWS CLI Configured: AWS CLI configured with your credentials. This is important for Terraform to interact with your AWS account.

Step 1: Setting Up Your Environment

Install Terraform

First, ensure Terraform is installed on your machine. You can verify this by running:

terraform -v 

If it’s not installed, follow the installation guide.

Configure AWS CLI

Make sure your AWS CLI is configured with the appropriate credentials:

aws configure

Enter your AWS Access Key ID, Secret Access Key, region, and output format when prompted.

Step 2: Writing the Terraform Configuration

Create a new directory for your Terraform project and navigate into it:

mkdir terraform-ec2
cd terraform-ec2

Create a file named main.tf. This file will contain the configuration for your EC2 instance. Open main.tf in your preferred text editor and add the following configuration:

provider "aws" {
  region = "us-west-2" # Specify your desired AWS region
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI ID
  instance_type = "t2.micro"              # Instance type

  tags = {
    Name = "TerraformExampleInstance"
  }
}

Breakdown of the Configuration

  • provider “aws”: Specifies the AWS provider and the region.
  • resource “aws_instance” “example”: Defines an EC2 instance resource.
    • ami: Amazon Machine Image ID. You can find the latest AMI IDs in the AWS Management Console.
    • instance_type: The type of EC2 instance (e.g., t2.micro for a free tier eligible instance).
    • tags: Tags to identify your instance.

Step 3: Initializing Terraform

Before applying the configuration, initialize your Terraform workspace. This step downloads the necessary provider plugins:

terraform init

Step 4: Planning the Deployment

It’s a good practice to review the changes Terraform will make before applying them. Run the following command to see a plan of the actions Terraform will take:

terraform plan

Step 5: Applying the Configuration

Once you’re satisfied with the plan, apply the configuration to create your EC2 instance:

terraform apply

Step 6: Verifying the Instance

After Terraform completes the process, you can verify the instance creation in the AWS Management Console. Navigate to the EC2 Dashboard, and you should see your new instance running.

Step 7: Cleaning Up

To avoid incurring charges, you should destroy the resources once you no longer need them. Run the following command to delete the EC2 instance:

terraform destroy

Again, Terraform will prompt you to confirm. Type yes and hit Enter. This will terminate the EC2 instance and clean up all associated resources.

Conclusion

Creating and managing EC2 instances with Terraform is a powerful way to automate your infrastructure. By defining your infrastructure as code, you can version control your configurations, collaborate more effectively, and ensure consistent environments across deployments. This guide has walked you through the basics of setting up an EC2 instance with Terraform, but there’s much more to explore. From complex networking setups to managing other AWS resources, Terraform provides a versatile toolset for your infrastructure needs.

Happy provisioning!

Comments