diff --git a/shubh_resource_files/backend.tf b/shubh_resource_files/backend.tf new file mode 100644 index 0000000..b4a8399 --- /dev/null +++ b/shubh_resource_files/backend.tf @@ -0,0 +1,8 @@ +# Backend configuration to store Terraform state file into S3 Bucket +terraform { + backend "s3" { + bucket = "terra-bucket20234" + key = "terra-backend/tfstate" # The path within the bucket for the state file + region = "ap-south-1" + } +} \ No newline at end of file diff --git a/shubh_resource_files/ebs_vol.tf b/shubh_resource_files/ebs_vol.tf new file mode 100644 index 0000000..4a1aee7 --- /dev/null +++ b/shubh_resource_files/ebs_vol.tf @@ -0,0 +1,14 @@ +/*resource "aws_ebs_volume" "terra-EBS" { + availability_zone = var.ZONE1 + size = 2 + tags = { + Name = "Extra Volume Attachment" + } +} + +resource "aws_volume_attachment" "ebs_att" { + device_name = "/dev/xvdh" + volume_id = aws_ebs_volume.terra-EBS.id + instance_id = aws_instance.EC2-Instance.id +} +*/ \ No newline at end of file diff --git a/shubh_resource_files/instance.tf b/shubh_resource_files/instance.tf new file mode 100644 index 0000000..d9cdff0 --- /dev/null +++ b/shubh_resource_files/instance.tf @@ -0,0 +1,85 @@ +# AWS Key Pair +resource "aws_key_pair" "key" { + key_name = "terra-key" + public_key = file("terra-key.pub") +} + +# Create EC2 Instance +resource "aws_instance" "EC2-Instance" { + ami = var.AMIs[var.REGION] + instance_type = var.INST_TYPE + subnet_id = aws_subnet.public-subnet-1.id + vpc_security_group_ids = [aws_security_group.terra-SG.id] + key_name = aws_key_pair.key.key_name + tags = { + Name = "terraform instance" + Purpose = "Test of terraform code" + } + + user_data = file("web.sh") # Userdata to bo executed after server provisioning + /** + # Transfer file from local env to server + provisioner "file" { + source = "web.sh" + destination = "/tmp/web.sh" + } + + # Remote commands on server + provisioner "remote-exec" { + inline = [ + "chmod +x /tmp/web.sh", + "sudo /tmp/web.sh" + ] + } + + # SSH into server + connection { + user = var.user + private_key = file("terra-key") + host = self.public_ip + } + **/ +} + + + +# Allocate an Elastic IP +resource "aws_eip" "ElasticIP" { + instance = aws_instance.EC2-Instance.id +} + +# Create an RDS instance +resource "aws_db_instance" "RDS" { + identifier = "my-rds-instance" + instance_class = "db.t3.micro" # Change to your desired instance type + engine = "mysql" # Change to your preferred database engine (e.g., postgres, oracle) + engine_version = "8.0" # Change to the version of the engine you're using + allocated_storage = 20 # Size in GB + username = "admin" # Master username + password = "admin123" # Master password + db_name = "mydatabase" # Initial database name + publicly_accessible = true # Set to false for private instances + vpc_security_group_ids = [aws_security_group.rds_sg.id] + storage_type = "gp2" # General Purpose SSD + skip_final_snapshot = true # Set to false if you want to store final snapshot + + tags = { + Name = "MyRDSInstance" + } +} + +# Create extra EBS Volume +resource "aws_ebs_volume" "terra-EBS" { + availability_zone = var.ZONE1 + size = 2 + tags = { + Name = "Extra Volume Attachment" + } +} + +# Attach extra EBS Volume to Instance +resource "aws_volume_attachment" "extra_ebs_att" { + device_name = "/dev/xvdh" + volume_id = aws_ebs_volume.terra-EBS.id + instance_id = aws_instance.EC2-Instance.id +} \ No newline at end of file diff --git a/shubh_resource_files/output.tf b/shubh_resource_files/output.tf new file mode 100644 index 0000000..330c7a1 --- /dev/null +++ b/shubh_resource_files/output.tf @@ -0,0 +1,42 @@ +# Output Block for EC2 and RDS Instance + +output "ElasticIP" { + value = aws_eip.ElasticIP.public_ip +} + +output "PrivateIP" { + value = aws_instance.EC2-Instance.private_ip +} + +output "AMI" { + value = aws_instance.EC2-Instance.ami +} + +output "ZONE" { + value = aws_instance.EC2-Instance.availability_zone +} + +output "instance_type" { + value = aws_instance.EC2-Instance.instance_type +} + +output "rds_instance_endpoint" { + description = "The endpoint of the RDS instance." + value = aws_db_instance.RDS.endpoint +} + +output "rds_instance_db_name" { + description = "The database name for the RDS instance." + value = aws_db_instance.RDS.db_name +} + +output "rds_instance_port" { + description = "The port of the RDS instance." + value = aws_db_instance.RDS.port +} + + +output "rds_instance_id" { + description = "The ID of the RDS instance." + value = aws_db_instance.RDS.id +} \ No newline at end of file diff --git a/shubh_resource_files/providers.tf b/shubh_resource_files/providers.tf new file mode 100644 index 0000000..d7d4fb0 --- /dev/null +++ b/shubh_resource_files/providers.tf @@ -0,0 +1,4 @@ +# AWS Provider +provider "aws" { + region = var.REGION +} \ No newline at end of file diff --git a/shubh_resource_files/s3-bucket.tf b/shubh_resource_files/s3-bucket.tf new file mode 100644 index 0000000..4ffef34 --- /dev/null +++ b/shubh_resource_files/s3-bucket.tf @@ -0,0 +1,29 @@ +# Separate execution of s3 bucket creation should be done. +# aws s3api create-bucket --bucket terra-bucket20234 --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1 + + +/**resource "aws_s3_bucket" "terraform_state" { + bucket = "terra-bucket20234" # Replace with a globally unique bucket name + acl = "private" + + tags = { + Name = "Terraform State Bucket" + Environment = "Terra-Project" + } +} + +resource "aws_s3_bucket_versioning" "versioning" { + bucket = aws_s3_bucket.terraform_state.bucket + + versioning_configuration { + status = "Enabled" + } +} + +output "bucket_name" { + value = aws_s3_bucket.terraform_state.bucket +} +**/ + +# Separate execution of s3 bucket creation should be done. +# aws s3api create-bucket --bucket terra-bucket20234 --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1 diff --git a/shubh_resource_files/sec_grp.tf b/shubh_resource_files/sec_grp.tf new file mode 100644 index 0000000..4921864 --- /dev/null +++ b/shubh_resource_files/sec_grp.tf @@ -0,0 +1,75 @@ +# Security Group for EC2-instance + +resource "aws_security_group" "terra-SG" { + vpc_id = aws_vpc.terraform_vpc.id + name = "terra-SG" + description = "Sec_grp for SSH & HTTP" + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] # your IP + } + /** + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +**/ + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + tags = { + Name = "allow_SSH & HTTP" + } +} + +# Define a security group to allow access to the RDS instance +resource "aws_security_group" "rds_sg" { + name_prefix = "rds_sg_" + + ingress { + from_port = 3306 # Port for MySQL. + to_port = 3306 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] # your IP + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + tags = { + Name = "Allow 3306 for RDS Instance" + } +} + diff --git a/shubh_resource_files/terra-key b/shubh_resource_files/terra-key new file mode 100644 index 0000000..3c5eab8 --- /dev/null +++ b/shubh_resource_files/terra-key @@ -0,0 +1 @@ +### your private key ### diff --git a/shubh_resource_files/terra-key.pub b/shubh_resource_files/terra-key.pub new file mode 100644 index 0000000..bad8b64 --- /dev/null +++ b/shubh_resource_files/terra-key.pub @@ -0,0 +1 @@ +### Your Public Key ### diff --git a/shubh_resource_files/vars.tf b/shubh_resource_files/vars.tf new file mode 100644 index 0000000..5751c80 --- /dev/null +++ b/shubh_resource_files/vars.tf @@ -0,0 +1,45 @@ +variable "REGION" { + default = "ap-south-1" +} + +variable "ZONE1" { + default = "ap-south-1a" +} + +variable "ZONE2" { + default = "ap-south-1b" + +} + +variable "ZONE3" { + default = "ap-south-1c" +} + +variable "AMIs" { + type = map(any) + default = { + ap-south-1 = "ami-025fe52e1f2dc5044" + us-east-2 = "ami-0c11a84584d4e09dd" + } +} + +variable "INST_TYPE" { + default = "t2.micro" +} + +variable "user" { + default = "ec2-user" +} + +variable "PUB_KEY" { + default = "terra-key.pub" +} + +variable "PRI_KEY" { + default = "terra_key" +} + +/**variable "MYIP" { + default = "152.57.xxx.xxx/32" +} +**/ \ No newline at end of file diff --git a/shubh_resource_files/vpc.tf b/shubh_resource_files/vpc.tf new file mode 100644 index 0000000..6fe326b --- /dev/null +++ b/shubh_resource_files/vpc.tf @@ -0,0 +1,105 @@ +#Creating VPC, 3 Private Subnets and 3 Public Subnets +resource "aws_vpc" "terraform_vpc" { + cidr_block = "10.0.0.0/16" + instance_tenancy = "default" + enable_dns_support = "true" + enable_dns_hostnames = "true" + tags = { + Name = "terraform VPC" + } +} + +resource "aws_subnet" "public-subnet-1" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.1.0/24" + map_public_ip_on_launch = "true" + availability_zone = var.ZONE1 + tags = { + Name = "Public-Subnet-1" + } +} + +resource "aws_subnet" "public-subnet-2" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.2.0/24" + map_public_ip_on_launch = "true" + availability_zone = var.ZONE2 + tags = { + Name = "Public-Subnet-2" + } +} + +resource "aws_subnet" "public-subnet-3" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.3.0/24" + map_public_ip_on_launch = "true" + availability_zone = var.ZONE3 + tags = { + Name = "Public-Subnet-3" + } +} + +resource "aws_subnet" "private-subnet-1" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.4.0/24" + # map_public_ip_on_launch = "true" + availability_zone = var.ZONE1 + tags = { + Name = "Private-Subnet-1" + } +} + +resource "aws_subnet" "private-subnet-2" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.5.0/24" + # map_public_ip_on_launch = "true" + availability_zone = var.ZONE2 + tags = { + Name = "Private-Subnet-2" + } +} + +resource "aws_subnet" "private-subnet-3" { + vpc_id = aws_vpc.terraform_vpc.id + cidr_block = "10.0.6.0/24" + # map_public_ip_on_launch = "true" + availability_zone = var.ZONE3 + tags = { + Name = "Private-Subnet-3" + } +} + +resource "aws_internet_gateway" "terraform-IGW" { + vpc_id = aws_vpc.terraform_vpc.id + tags = { + Name = "Terraform-IGW" + } +} + +resource "aws_route_table" "terraform-RT" { + vpc_id = aws_vpc.terraform_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.terraform-IGW.id + } + + tags = { + Name = "Terraform IGW" + } +} + +resource "aws_route_table_association" "public-subnet-1a" { + subnet_id = aws_subnet.public-subnet-1.id + route_table_id = aws_route_table.terraform-RT.id +} + +resource "aws_route_table_association" "public-subnet-2b" { + subnet_id = aws_subnet.public-subnet-2.id + route_table_id = aws_route_table.terraform-RT.id +} + +resource "aws_route_table_association" "public-subnet-3c" { + subnet_id = aws_subnet.public-subnet-3.id + route_table_id = aws_route_table.terraform-RT.id +} \ No newline at end of file diff --git a/shubh_resource_files/web.sh b/shubh_resource_files/web.sh new file mode 100644 index 0000000..cf3fa55 --- /dev/null +++ b/shubh_resource_files/web.sh @@ -0,0 +1,8 @@ +#!/bin/bash +yum install wget unzip httpd -y +systemctl start httpd +systemctl enable httpd +wget https://www.tooplate.com/zip-templates/2117_infinite_loop.zip +unzip -o 2117_infinite_loop.zip +cp -r 2117_infinite_loop/* /var/www/html/ +systemctl restart httpd \ No newline at end of file