Lab #8: How to Create EC2 Instance in AWS using Terraform | Create EC2 Instance using Terraform

3 min read 4 hours ago
Published on Oct 17, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will learn how to create an EC2 instance in AWS using Terraform. Terraform is an infrastructure as code tool that allows you to define and provision data center infrastructure with a high level of automation. By the end of this guide, you will be able to set up an EC2 instance quickly and efficiently.

Step 1: Set Up Your Terraform Configuration

Before we can create an EC2 instance, we need to configure Terraform with AWS credentials and specify the region.

  1. Create a New Directory:

    • Create a directory where you will store your Terraform configuration files.
  2. Create a New File:

    • Create a file with a .tf extension, for example, ec2_instance.tf.
  3. Add the AWS Provider Block:

    • Open the .tf file and add the following code to specify your AWS credentials and region:
    provider "aws" {
      access_key = "YOUR_ACCESS_KEY"
      secret_key = "YOUR_SECRET_KEY"
      region     = "ap-south-1"
    }
    

    Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your actual AWS access and secret keys.

Step 2: Define the EC2 Instance Resource

Now that we have the provider configured, we can define the EC2 instance that we want to create.

  1. Add the Resource Block:

    • In the same .tf file, add the following resource block to define your EC2 instance:
    resource "aws_instance" "Terraform_Demo" {
      ami           = "ami-0c1a7f89451184c8b"
      instance_type = "t2.micro"
      key_name      = "Dev-UbuntuKey"
      tags = {
        Name = "Terraform Demo"
      }
    }
    
    • AMI: The Amazon Machine Image (AMI) ID specified here is for Ubuntu. You can replace it with any AMI ID of your choice that is available in your specified region.
    • Instance Type: t2.micro is eligible for the AWS free tier.
    • Key Name: Ensure that the key pair named Dev-UbuntuKey exists in your AWS account.

Step 3: Initialize Terraform

Before applying the configuration, you need to initialize your Terraform environment.

  1. Open Terminal:

    • Navigate to the directory containing your .tf file.
  2. Run Initialization Command:

    • Execute the following command to initialize Terraform:
    terraform init
    

    This command downloads the necessary provider plugins.

Step 4: Plan the Deployment

Next, you should review what Terraform will do before applying the configuration.

  1. Run the Plan Command:

    • Use the following command to see a preview of the changes that will be made:
    terraform plan
    

    Review the output to ensure everything is correct.

Step 5: Apply the Configuration

Now it’s time to create the EC2 instance.

  1. Execute the Apply Command:

    • Run the following command to create the resources defined in your configuration file:
    terraform apply
    
  2. Confirm the Action:

    • You will be prompted to confirm the action. Type yes and hit Enter.

Step 6: Verify the EC2 Instance

After the apply command completes, you can verify that the EC2 instance has been created.

  1. Log into AWS Console:

    • Navigate to the EC2 dashboard in your AWS Management Console.
  2. Check Instances:

    • You should see your newly created instance listed there with the name "Terraform Demo".

Conclusion

You have successfully created an EC2 instance in AWS using Terraform. Key takeaways include understanding how to configure the AWS provider, define resources, and use Terraform commands to manage infrastructure.

Next steps could involve exploring additional configurations for security groups, volumes, or learning how to destroy resources when they are no longer needed using the terraform destroy command. Happy coding!