123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- # create vpc
- resource "aws_vpc" "main" {
- cidr_block = var.cidr
- instance_tenancy = "default"
- tags = {
- Name = var.vpc_name
- }
- }
- resource "aws_subnet" "subnet1" {
- vpc_id = aws_vpc.main.id
- cidr_block = var.subnet1_cidr
- availability_zone = "us-east-1a"
- tags = {
- Name = "pub-subnet1"
- }
- }
- resource "aws_subnet" "subnet2" {
- vpc_id = aws_vpc.main.id
- cidr_block = var.subnet2_cidr
- availability_zone = "us-east-1b"
- tags = {
- Name = "pub-subnet2"
- }
- }
- resource "aws_subnet" "subnet3" {
- vpc_id = aws_vpc.main.id
- cidr_block = var.subnet3_cidr
- availability_zone = "us-east-1a"
- tags = {
- Name = "pvt-subnet1"
- }
- }
- resource "aws_subnet" "subnet4" {
- vpc_id = aws_vpc.main.id
- cidr_block = var.subnet4_cidr
- availability_zone = "us-east-1b"
- tags = {
- Name = "pvt-subnet2"
- }
- }
- resource "aws_internet_gateway" "web-gw" {
- vpc_id = aws_vpc.main.id
- tags = {
- Name = "web-internetgateway"
- }
- }
- resource "aws_route_table" "pubroute" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- gateway_id = aws_internet_gateway.web-gw.id
- }
- tags = {
- Name = "pubroute"
- }
- }
- resource "aws_route_table" "pvtroute" {
- vpc_id = aws_vpc.main.id
-
- tags = {
- Name = "pvtroute"
- }
- }
- resource "aws_route_table_association" "pubsub1" {
- subnet_id = aws_subnet.subnet1.id
- route_table_id = aws_route_table.pubroute.id
- }
- resource "aws_route_table_association" "pvtsub2" {
- subnet_id = aws_subnet.subnet2.id
- route_table_id = aws_route_table.pubroute.id
- }
- resource "aws_route_table_association" "pvtsub1" {
- subnet_id = aws_subnet.subnet3.id
- route_table_id = aws_route_table.pvtroute.id
- }
- resource "aws_route_table_association" "pvtsub2" {
- subnet_id = aws_subnet.subnet4.id
- route_table_id = aws_route_table.pvtroute.id
- }
- ############ EC2 Creation with ALB & Auto scaling Group ##########
- resource "aws_security_group" "web_sg" {
- name_prefix = "web-sg-"
- # Inbound rule for SSH traffic
- ingress {
- from_port = 22
- to_port = 22
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- }
- # Inbound rule for HTTP traffic
- ingress {
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- }
- # Inbound rule for HTTPS traffic
- ingress {
- from_port = 443
- to_port = 443
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- }
- # Outbound rule allowing all traffic
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- }
- }
- resource "aws_launch_configuration" "web_lc" {
- name_prefix = "web-lc"
- image_id = var.ami
- instance_type = var.instance_type
- security_groups = [aws_security_group.web_sg.id]
- key_name = var.keyname
- user_data = <<-EOF
- #!/bin/bash
- apt update
- apt upgrade -y
- EOF
- lifecycle {
- create_before_destroy = true
- }
- }
- resource "aws_autoscaling_group" "web_asg" {
- name_prefix = "web-asg"
- min_size = 2
- max_size = 5
- desired_capacity = 2
- launch_configuration = aws_launch_configuration.web_lc.name
- // Define your load balancer target group ARNs here
- }
- resource "aws_lb" "example_alb" {
- name = "web-alb"
- internal = false
- load_balancer_type = "application"
- subnets = [aws_subnet.subnet1.id, aws_subnet.subnet2.id]
- }
- resource "aws_lb_listener" "example_listener" {
- load_balancer_arn = aws_lb.example_alb.arn
- port = 80
- protocol = "HTTP"
- default_action {
- type = "fixed-response"
- fixed_response {
- content_type = "/"
- status_code = "200"
- }
- }
- }
- resource "aws_lb_target_group" "web_target_group" {
- name = "web-target-group"
- port = 80
- protocol = "HTTP"
- vpc_id = aws_vpc.main.id
- }
- resource "aws_autoscaling_attachment" "web_asg_attachment" {
- autoscaling_group_name = aws_autoscaling_group.web_asg.name
- alb_target_group_arn = aws_lb_target_group.web_target_group.arn
- }
|