diff --git a/day-24/main.tf b/day-24/main.tf new file mode 100644 index 00000000..2a466c1c --- /dev/null +++ b/day-24/main.tf @@ -0,0 +1,145 @@ +resource "aws_vpc" "myvpc" { + cidr_block = var.cidr +} + +resource "aws_subnet" "sub1" { + vpc_id = aws_vpc.myvpc.id + cidr_block = "10.0.0.0/24" + availability_zone = "us-east-1a" + map_public_ip_on_launch = true +} + +resource "aws_subnet" "sub2" { + vpc_id = aws_vpc.myvpc.id + cidr_block = "10.0.1.0/24" + availability_zone = "us-east-1b" + map_public_ip_on_launch = true +} + +resource "aws_internet_gateway" "igw" { + vpc_id = aws_vpc.myvpc.id +} + +resource "aws_route_table" "RT" { + vpc_id = aws_vpc.myvpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.igw.id + } +} + +resource "aws_route_table_association" "rta1" { + subnet_id = aws_subnet.sub1.id + route_table_id = aws_route_table.RT.id +} + +resource "aws_route_table_association" "rta2" { + subnet_id = aws_subnet.sub2.id + route_table_id = aws_route_table.RT.id +} + +resource "aws_security_group" "webSg" { + name = "web" + vpc_id = aws_vpc.myvpc.id + + ingress { + description = "HTTP from VPC" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + ingress { + description = "SSH" + from_port = 22 + to_port = 22 + 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"] + } + + tags = { + Name = "Web-sg" + } +} + +resource "aws_s3_bucket" "example" { + bucket = "abhisheksterraform2023project" +} + + +resource "aws_instance" "webserver1" { + ami = "ami-0261755bbcb8c4a84" + instance_type = "t2.micro" + vpc_security_group_ids = [aws_security_group.webSg.id] + subnet_id = aws_subnet.sub1.id + user_data = base64encode(file("userdata.sh")) +} + +resource "aws_instance" "webserver2" { + ami = "ami-0261755bbcb8c4a84" + instance_type = "t2.micro" + vpc_security_group_ids = [aws_security_group.webSg.id] + subnet_id = aws_subnet.sub2.id + user_data = base64encode(file("userdata1.sh")) +} + +#create alb +resource "aws_lb" "myalb" { + name = "myalb" + internal = false + load_balancer_type = "application" + + security_groups = [aws_security_group.webSg.id] + subnets = [aws_subnet.sub1.id, aws_subnet.sub2.id] + + tags = { + Name = "web" + } +} + +resource "aws_lb_target_group" "tg" { + name = "myTG" + port = 80 + protocol = "HTTP" + vpc_id = aws_vpc.myvpc.id + + health_check { + path = "/" + port = "traffic-port" + } +} + +resource "aws_lb_target_group_attachment" "attach1" { + target_group_arn = aws_lb_target_group.tg.arn + target_id = aws_instance.webserver1.id + port = 80 +} + +resource "aws_lb_target_group_attachment" "attach2" { + target_group_arn = aws_lb_target_group.tg.arn + target_id = aws_instance.webserver2.id + port = 80 +} + +resource "aws_lb_listener" "listener" { + load_balancer_arn = aws_lb.myalb.arn + port = 80 + protocol = "HTTP" + + default_action { + target_group_arn = aws_lb_target_group.tg.arn + type = "forward" + } +} + +output "loadbalancerdns" { + value = aws_lb.myalb.dns_name +} \ No newline at end of file diff --git a/day-24/provider.tf b/day-24/provider.tf new file mode 100644 index 00000000..ce9a20ca --- /dev/null +++ b/day-24/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "5.11.0" + } + } +} + +provider "aws" { + # Configuration options + region = "us-east-1" +} \ No newline at end of file diff --git a/day-24/userdata.sh b/day-24/userdata.sh new file mode 100644 index 00000000..df1aa015 --- /dev/null +++ b/day-24/userdata.sh @@ -0,0 +1,43 @@ +#!/bin/bash +apt update +apt install -y apache2 + +# Get the instance ID using the instance metadata +INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) + +# Install the AWS CLI +apt install -y awscli + +# Download the images from S3 bucket +#aws s3 cp s3://myterraformprojectbucket2023/project.webp /var/www/html/project.png --acl public-read + +# Create a simple HTML file with the portfolio content and display the images +cat < /var/www/html/index.html + + + + My Portfolio + + + +

Terraform Project Server 1

+

Instance ID: $INSTANCE_ID

+

Welcome to Abhishek Veeramalla's Channel

+ + + +EOF + +# Start Apache and enable it on boot +systemctl start apache2 +systemctl enable apache2 \ No newline at end of file diff --git a/day-24/userdata1.sh b/day-24/userdata1.sh new file mode 100644 index 00000000..01aab99e --- /dev/null +++ b/day-24/userdata1.sh @@ -0,0 +1,43 @@ +#!/bin/bash +apt update +apt install -y apache2 + +# Get the instance ID using the instance metadata +INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) + +# Install the AWS CLI +apt install -y awscli + +# Download the images from S3 bucket +#aws s3 cp s3://myterraformprojectbucket2023/project.webp /var/www/html/project.png --acl public-read + +# Create a simple HTML file with the portfolio content and display the images +cat < /var/www/html/index.html + + + + My Portfolio + + + +

Terraform Project Server 1

+

Instance ID: $INSTANCE_ID

+

Welcome to CloudChamp's Channel

+ + + +EOF + +# Start Apache and enable it on boot +systemctl start apache2 +systemctl enable apache2 \ No newline at end of file diff --git a/day-24/variables.tf b/day-24/variables.tf new file mode 100644 index 00000000..07560480 --- /dev/null +++ b/day-24/variables.tf @@ -0,0 +1,3 @@ +variable "cidr" { + default = "10.0.0.0/16" +} \ No newline at end of file