Run the following commands
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.listsudo apt update && sudo apt install terraformDownload from the following link
https://developer.hashicorp.com/terraform/install
create folder named Terraform in C:\ and extract to C:\terraform
search View Advanced System Setting
go to Advanced - Environment Variables – System Variables – New
add c:\terraform
Go to cmd and type terraform –version
You will see the following screen and verify the terraform installation
On windows
Open CMD or Power Shell run the following command
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msiNote: for silent install use /qn flag
On Linux
To install the AWS CLI, run the following command
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/installRun the following command to check if AWS CLI is installed correctly:
aws –versionYou see the following output
After Creating
Click on account name - Select Security Credentials
Click Create access key.
Note: Download the key file or copy the Access Key ID & Secret Access Key (Secret Key is shown only once!).
aws configureIt will prompt you for:
1. AWS Access Key ID: Your access key from AWS IAM.
2. AWS Secret Access Key: Your secret key from AWS IAM.
3. Default region name: (e.g., us-east-1, us-west-2).
4. Default output format: (json, table, text — default is json).
As shown bellow
Check credentials added to aws configure correctly
aws sts get-caller-identityIf your AWS CLI is properly configured, you'll see a response like this:
-
Open VS Code.
-
Go to Extensions (Ctrl+Shift+X).
-
Search for "HashiCorp Terraform" and click Install.
-
Once installed, restart VS Code.
Now Open Lab3-Terraform in Visual Studio Code
Launch VS Code.
Click File → Open Folder → locate the terraform code from your local machine.
Select your Terraform project directory and open it
You will see the following screen in vscode
Run the following commands
-
terraform init is a command used in Terraform to initialize a working directory containing Terraform configuration files
-
It sets up your environment by configuring the backend, downloading necessary provider plugins, and installing modules, allowing you to then plan, apply, and manage your infrastructure
You will see the following screen after running Terraform init
-
The terraform validate command checks whether your Terraform configuration is syntactically valid and internally consistent
-
Ensures your .tf files are written correctly (Syntax Checking) (valid HCL syntax)
-
Verifies references between resources are valid.
-
Does not check whether your AWS credentials or resources exist — that happens during plan and apply.
-
The terraform fmt command automatically formats your Terraform code to follow canonical style conventions.
-
Fixes indentation
-
Orders attributes consistently
-
Makes your .tf files cleaner and easier to read
-
Applies to files in the current directory by default
- terraform plan is a crucial command in Terraform that allows you to preview the changes that Terraform intends to make to your infrastructure based on your current configuration and state
Generates an Execution Plan: Terraform produces a detailed plan outlining the proposed actions. This plan shows:
-
Which resources will be created (+ symbol).
-
Which resources will be modified (~ symbol, showing the before and after values of changed attributes).
-
Which resources will be replaced (-/+ symbol, indicating destruction and creation).
-
Which resources will be destroyed (- symbol).
The terraform plan -out command is used to save the execution plan generated by terraform plan to a file. This is a crucial step for ensuring consistency and automation in your Terraform workflow.
- terraform apply is the command in Terraform that executes the actions proposed in the execution plan to create, modify, or destroy infrastructure resources. It's the command that actually makes changes to your real-world infrastructure
Now you can access the website from browser using the URL provided in the output
-
Login to aws Account
-
Go to EC2 – Instances
Terraform destroyThe terraform destroy command removes all resources that were created by your Terraform configuration.
terraform plan -destroy #for Double-checking what you're destroying













