AWS VPC Setup with Terraform
#
ObjectiveComplete the Terraform setup to create an AWS VPC with public and private subnets, launch a bastion EC2 instance in the public subnet, and launch a private EC2 instance in the private subnet.
#
Gitlab Repository#
Environment SetupInstall Terraform on your local machine.
Clone the repository containing the Terraform code.
Configure AWS Credentials:
- Create a file named
terraform.tfvars
in the project directory. - Setup AWS credentials using the command:
- Create a file named
Customize Infrastructure Configuration:
- Open the
variables.tf
file in the project directory. - Modify the variables as needed:
aws_region
: The AWS region to deploy the infrastructure in.vpc_cidr_block
: The CIDR block for the VPC.private_subnet_1_cidr_block
: The CIDR block for the first private subnet.private_subnet_2_cidr_block
: The CIDR block for the second private subnet.public_subnet_cidr_block
: The CIDR block for the public subnet.public_ec2_key_name
: The key pair name for the public EC2 instance.private_ec2_key_name
: The key pair name for the private EC2 instance.
- Open the
Deploy Infrastructure:
- Open a terminal and navigate to the project directory.
- Initialize Terraform:
- Preview the changes that will be applied:
- Apply the Terraform configuration to create the infrastructure:Confirm the changes by typing "yes" when prompted.
Access Bastion Host:
- Once the infrastructure is created, obtain the public IP of the bastion EC2 instance from the Terraform output.
- Open a terminal and run the following command to SSH into the bastion host:
- Create a file named
id_ed25519
inside the/home/ec2-user/.ssh/
directory and paste the public IP into it. - Change the permissions of the key file:
SSH into Private Server:
- Obtain the private IP of the private EC2 instance from the Terraform output.
- Open a terminal and run the following command to SSH into the private server:
Infrastructure Destruction:
- To delete the infrastructure and clean up resources, run the following command:
- To delete the infrastructure and clean up resources, run the following command: