https://github.com/gurpreet2828/terraform-aws-ec2
This repository contains Terraform configurations to automate the provisioning of AWS infrastructure, specifically the deployment of EC2 instances. It includes step-by-step setup instructions for installing Terraform and AWS CLI on both Linux and Windows, configuring AWS credentials, and deploying infrastructure directly from your local machine
https://github.com/gurpreet2828/terraform-aws-ec2
automation aws aws-cli aws-ec2 devops iac linux terraform terraform-aws terraform-module vscode
Last synced: 12 months ago
JSON representation
This repository contains Terraform configurations to automate the provisioning of AWS infrastructure, specifically the deployment of EC2 instances. It includes step-by-step setup instructions for installing Terraform and AWS CLI on both Linux and Windows, configuring AWS credentials, and deploying infrastructure directly from your local machine
- Host: GitHub
- URL: https://github.com/gurpreet2828/terraform-aws-ec2
- Owner: gurpreet2828
- Created: 2025-04-02T00:51:39.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-21T00:55:44.000Z (about 1 year ago)
- Last Synced: 2025-05-21T01:52:56.840Z (about 1 year ago)
- Topics: automation, aws, aws-cli, aws-ec2, devops, iac, linux, terraform, terraform-aws, terraform-module, vscode
- Language: HCL
- Homepage:
- Size: 1.78 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Manage AWS Infrastructure with Terraform
## Step 1: Installation of terraform
### **Install Terraform (Linux -Ubuntu)**
Run the following commands
```shell
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.list
```
```shell
sudo apt update && sudo apt install terraform
```
### **Install Terraform on Windows**
Download 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

## Step2: Install AWS CLI
**On windows**
Open CMD or Power Shell run the following command
```shell
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
```
Note: for silent install use /qn flag
**On Linux**
To install the AWS CLI, run the following command
```bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
```
Run the following command to check if AWS CLI is installed correctly:
```bash
aws –version
```
You see the following output

## Step3: Create AWS account
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!).
## Step 4: Configure AWS CLI with the New Access Key
```shell
aws configure
```
It 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
```shell
aws sts get-caller-identity
```
If your AWS CLI is properly configured, you'll see a response like this:

## Step 5: Install vs code
### Step 6: Install Terraform Extension in VS Code
1. Open VS Code.
2. Go to Extensions (Ctrl+Shift+X).
3. Search for "HashiCorp Terraform" and click Install.
4. 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

## Step7: Deploy EC2 instance on AWS through Terraform
Run the following commands
### 1. `Terraform init`
- 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

### 2. `Terraform validate`
- 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.

### 3. `Terraform fmt`
- 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
### 4. `Terraform plan`
- 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).

### 5. `Terraform plan -out=ec2plan` -- ec2plan is filename you can rename according to your need
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.

### 6. `Terraform apply`
- 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

## Step 8: Verifying ec2 instance running on AWS
- Login to aws Account
- Go to EC2 – Instances

## Step 9: Delete all resources
```shell
Terraform destroy
```
The terraform destroy command removes all resources that were created by your Terraform configuration.
```shell
terraform plan -destroy #for Double-checking what you're destroying
```