Use this project to allocate an AWS EC2 instance programmatically.
- AMI: Ubuntu Server 20.04 LTS (ami-0851b76e8b1bce90b) (free tier eligible)
- Instance Type: t2.micro (free tier eligible)
- Security Group inbound rules will allow access to port 22 (SSH), 443 (HTTPS), 80 (HTTP) from anywhere
These configurations can be edited from main.tf
file.
- Clone the repository
git clone https://github.com/RajatSablok/terraform-aws-ec2-setup.git
- Configure AWS CLI
aws configure
- Create new AWS Key Pair (PEM) file
aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
- Initialize Terraform project
terraform init
- Edit
variables.tf
file to enter name of the EC2 instance and PEM file that you created - Apply the changes (this will allocate the EC2 instance)
terraform apply
Use
terraform destroy
to terminate the EC2 instance and the related security group.