Terraform Lab1 : Provisioning an EC2 on AWS Using Terraform 2024

Welcome to our Terraform Lab! In this hands-on lab we will guide you through creating the EC2 instance on Amazon Web Service (AWS) using Terraform. Ensure you have Terraform installed on your machine before starting. You can download it from the official Terraform website.

Lab Outline

1.Setting up Environment

Ensure that you have valid AWS credentials configured on your machine. Replace ‘your_access_key’ and ‘your_secret_key’ with your actual credentials.

2.Creating AWS EC2 Instance

Create a new directory for your Terraform project. Inside this directory, create a file named ‘aws_ec2.tf’. This will be your main Terraform configuration file for AWS.

Open ‘aws_ec2.tf’ with a text editor and add the following configuration.

3.Initializing and Applying AWS Configuration

In the terminal navigate to the directory containing ‘aws_ec2.tf’ and run the following command,

Above command initializes your Terraform configuration and downloads the necessary provider plugins.

4. Reviewing the Terraform Plan

Run the following command to generate and preview the execution plan,

Above command shows you what Terraform will do before actually doing it. Review the output which should indicate the creation of EC2.

5. Applying the Terraform Configuration

Execute the following command to apply your Terraform configuration,

Terraform will prompt you to confirm the plan. Type ‘yes’ and press enter.

6. Cleaning Up

Once you are done experimenting, it’s essential to clean up your resources to avoid unnecessary charges. Run the following command in the respective directory,

Above command will destory/delete the resource which we have created with the above configuration file.

Congratulationas! You have completed the Terraform lab, creating EC2 instance on AWS. This exercise covers the basics of provisioning infrastructure on major cloud platforms like AWS using Terraform. Feel free to explore more advanced configurations and services as you continue your Terraform journey. Happy coding!

Leave a Comment