0% found this document useful (0 votes)
8 views

Lab 1 Terraform

Uploaded by

d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 1 Terraform

Uploaded by

d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab: Creating an EC2 Instance Using Terraform

Disclaimer
Before you proceed with this lab, carefully read and understand the following points:
1. AWS Costs: Using AWS resources may incur charges. Always monitor your
usage via the AWS Billing Dashboard. You are responsible for any costs
associated with this lab.
2. AWS Credentials: Handle your credentials securely. Never share them, upload
them to repositories, or expose them publicly. Misuse of credentials can lead to
unauthorized access and charges.
3. Resource Management: Ensure you destroy all resources created during the
lab to avoid unnecessary costs.
4. Environment Assumptions: This lab assumes the following:
o VS Code is installed on your system.
o You have a valid AWS account with necessary permissions.
o You are working on a Windows environment.

A Note About Things Going Wrong (Because They Will!)


Hey folks, let’s face it—technology can be... temperamental. Sometimes, you’ll follow
every step, double-check your configurations, and still get an error that makes no sense
whatsoever. Don’t panic! This happens to everyone, even seasoned professionals.
Here’s the deal:
 If something doesn’t work, ask me-Harjit (your ever-helpful trainer). I promise I’ll
try not to laugh at your typos. 😉
 Stuck and I’m busy? Ask a friend. Your colleagues are here to learn too, and
teamwork makes the dream work.
 Once you finish the lab, feel free to explore and try new things. But please don’t
pressure others to "hurry up." Not everyone has a supercomputer brain or
lightning-fast typing skills. Let’s keep it chill and fun!
Remember: errors are just opportunities to learn... or at least to discover new creative
ways to use Google. 😊
Section 1: Setting Up the Environment
In this section, you’ll prepare the tools and configurations needed to complete the lab.
Step 1: Install the AWS CLI
1. Open a web browser and go to the AWS CLI Downloads page.
2. Click on the appropriate installer for your operating system:
o For Windows, download the AWSCLIV2.msi file.
3. Run the downloaded installer and follow the on-screen instructions.
4. Verify the installation by opening PowerShell and running:

aws --version
You should see the installed AWS CLI version.

Step 2: Configure the AWS CLI


1. In PowerShell, run the following command:

aws configure

2. Enter the following details when prompted:


o AWS Access Key ID: Found in the AWS Console > IAM > Users >
Security Credentials.
o AWS Secret Access Key: Found in the same location as above.
o Default region name: Use a region like us-east-1 or your preferred AWS
region.
o Default output format: Use json.
3. Test your setup:
aws sts get-caller-identity
This should return your Account, UserId, and Arn.

Step 3: Install Terraform


1. Open a web browser and go to the Terraform Downloads page.
2. Download the Windows installer.
3. Add Terraform to your system’s PATH variable if it isn’t automatically added:
o Go to System Properties > Advanced > Environment Variables.
o Edit the Path variable and add the directory where Terraform is installed.
4. Verify the installation by running:

terraform –version

Step 4: Set Up Your VS Code Workspace


1. Open VS Code.
2. Navigate to your Downloads folder (or your preferred working directory).
o Create a new folder named terraform-lab.
3. Open the folder in VS Code:
o Go to File > Open Folder and select the terraform-lab folder.
4. Open the integrated terminal in VS Code:
o Go to View > Terminal or press `Ctrl + ```.
o Ensure the terminal points to your terraform-lab directory.

Questions for Section 1


1. What are the risks of exposing your AWS credentials, and how can you mitigate
them?
2. Why is it important to configure the correct AWS region when setting up the CLI?
3. How can you verify that Terraform is correctly added to your system’s PATH?
4. What should you do if the aws sts get-caller-identity command fails?

Section 2: Writing Terraform Configuration


In this section, you will write the Terraform configuration to deploy an EC2 instance.
Step 1: Create and Configure main.tf
1. In your terraform-lab folder, create a new file named main.tf.
2. Visit the Terraform AWS Provider Registry.
3. Copy the following block and paste it into your main.tf file:

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.82.2"
}
}
}

provider "aws" {
region = "us-east-1" # Replace with your preferred region
}
4. Save the file.

Step 2: Add an EC2 Instance Resource


1. Below the provider block, add the following EC2 resource:

resource "aws_instance" "my_ec2" {


ami = "ami-0c02fb55956c7d316" # Replace with a valid AMI ID for your region
instance_type = "t2.micro"

tags = {
Name = "TerraformEC2"
}
}
o Use the AWS Console or CLI to find an AMI ID appropriate for your region.
2. Save the file.

Questions for Section 2


1. Why do you need to specify the AWS region in the provider block?
2. What happens if you omit the ami parameter when creating an EC2 instance in
Terraform?
3. How does the tags block in the EC2 resource help manage your infrastructure?
4. How would you modify the configuration to deploy multiple EC2 instances?

Section 3: Deploying the EC2 Instance


In this section, you will use Terraform to deploy your EC2 instance.
Step 1: Initialize Terraform
Run the following command in your terminal:
terraform init

 This downloads the required provider and prepares Terraform to execute your
configuration.

Step 2: Validate the Configuration


Run:

terraform validate

 This checks for syntax errors in your main.tf file.

Step 3: Preview the Deployment Plan


Run:

terraform plan

 Terraform will output the actions it plans to take, such as creating an EC2
instance.

Step 4: Apply the Configuration


Run:

terraform apply

 Type yes when prompted.


 Terraform will deploy the EC2 instance and output the details.
Questions for Section 3
1. What does the terraform init command do, and why must it be run before
terraform plan?
2. How does the terraform plan command help prevent mistakes in your
infrastructure?
3. What is the purpose of typing yes during terraform apply?
4. How would you verify that your EC2 instance was successfully deployed?

Section 4: Verifying and Cleaning Up


Step 1: Verify the EC2 Instance
1. Log in to the AWS Management Console.
2. Navigate to EC2 > Instances.
3. Confirm that the instance is running and named TerraformEC2.

Step 2: Clean Up Resources


1. Run the following command to destroy the EC2 instance:

terraform destroy

2. Type yes to confirm.


3. Verify in the AWS Console that the instance has been terminated.

Questions for Section 4


1. What are the different ways to verify that your EC2 instance is running?
2. Why is it important to destroy unused resources in AWS?
3. How would you automate the cleanup process in Terraform?

You might also like