π Terraform Series β Day 6
Deploy EC2 with VPC, Security Group & SSH Access

Search for a command to run...
Deploy EC2 with VPC, Security Group & SSH Access

No comments yet. Be the first to comment.
π Terraform Series β Automate Your Infrastructure Starting a complete **Terraform series** where Iβll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β’ What is Infrastructure as Code (IaC) & why it matters β’ Terraform fundamentals (providers, resources, state) β’ Writing and managing Terraform configurations β’ Variables, outputs & modules β’ Remote state & state management β’ Provisioning infrastructure on AWS β’ Automation & real-world use cases β’ Advanced concepts like workspaces, modules, and best practices π― Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and letβs build infrastructure the smart way β‘π» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation
Variables & Outputs (Make Your Code Smart π₯) In real-world DevOps, writing flexible and reusable code is very important.Today, we will learn how to use Variables and Outputs in Terraform to make our
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices π Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs π Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml π Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices π Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

In this hands-on, we will:
Generate SSH key
Create key pair using Terraform
Configure VPC & Security Group
Launch EC2 instance
Connect via SSH
Clean up resources
π This is your first real-world Terraform task
ssh-keygen
β This creates:
terra-key-awsβ Private key
terra-key-ec2.pub β Public key
π We will use this to access EC2
touch ec2.tf
π§± Step 3: Add Terraform Code
# Create Key Pair
resource "aws_key_pair" "my_key" {
key_name = "terra-key-aws"
public_key = file("terra-key-aws.pub")
}
# Default VPC
resource "aws_default_vpc" "default" {}
# Security Group
resource "aws_security_group" "my_groups" {
name = "my-group"
description = "Security group for EC2"
vpc_id = aws_default_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow HTTP"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound"
}
tags = {
Name = "automate-sg"
}
}
# EC2 Instance
resource "aws_instance" "my_instance" {
ami = "ami-0cb91c7de36eed2cb"
instance_type = "t2.micro"
key_name = aws_key_pair.my_key.key_name
vpc_security_group_ids = [aws_security_group.my_groups.id]
root_block_device {
volume_size = 10
volume_type = "gp3"
}
tags = {
Name = "terra-ec2"
}
}
terraform init
β Downloads AWS provider
β Prepares working directory
terraform validate
β Ensures syntax is correct
terraform plan
β Shows resources to be created:
Key Pair
VPC
Security Group
EC2 Instance
terraform apply
π Type yes to confirm
π Reason:
β Fix:
Go to AWS IAM β Attach Policy
AdministratorAccess (easy way)
OR
EC2FullAccess
VPCFullAccess
Go to EC2 Dashboard:
β Instance running
β Security group attached
β Key pair created
chmod 400 terra-key-aws
π Required before SSH
ssh -i terra-key-aws ubuntu@<your-public-ip>
π Now your server is live π
terraform destroy
π Prevent unnecessary AWS charges πΈ
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar