main.tf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # create vpc
  2. resource "aws_vpc" "main" {
  3. cidr_block = var.cidr
  4. instance_tenancy = "default"
  5. tags = {
  6. Name = var.vpc_name
  7. }
  8. }
  9. resource "aws_subnet" "subnet1" {
  10. vpc_id = aws_vpc.main.id
  11. cidr_block = var.subnet1_cidr
  12. availability_zone = "us-east-1a"
  13. tags = {
  14. Name = "pub-subnet1"
  15. }
  16. }
  17. resource "aws_subnet" "subnet2" {
  18. vpc_id = aws_vpc.main.id
  19. cidr_block = var.subnet2_cidr
  20. availability_zone = "us-east-1b"
  21. tags = {
  22. Name = "pub-subnet2"
  23. }
  24. }
  25. resource "aws_subnet" "subnet3" {
  26. vpc_id = aws_vpc.main.id
  27. cidr_block = var.subnet3_cidr
  28. availability_zone = "us-east-1a"
  29. tags = {
  30. Name = "pvt-subnet1"
  31. }
  32. }
  33. resource "aws_subnet" "subnet4" {
  34. vpc_id = aws_vpc.main.id
  35. cidr_block = var.subnet4_cidr
  36. availability_zone = "us-east-1b"
  37. tags = {
  38. Name = "pvt-subnet2"
  39. }
  40. }
  41. resource "aws_internet_gateway" "web-gw" {
  42. vpc_id = aws_vpc.main.id
  43. tags = {
  44. Name = "web-internetgateway"
  45. }
  46. }
  47. resource "aws_route_table" "pubroute" {
  48. vpc_id = aws_vpc.main.id
  49. route {
  50. cidr_block = "0.0.0.0/0"
  51. gateway_id = aws_internet_gateway.web-gw.id
  52. }
  53. tags = {
  54. Name = "pubroute"
  55. }
  56. }
  57. resource "aws_route_table" "pvtroute" {
  58. vpc_id = aws_vpc.main.id
  59. tags = {
  60. Name = "pvtroute"
  61. }
  62. }
  63. resource "aws_route_table_association" "pubsub1" {
  64. subnet_id = aws_subnet.subnet1.id
  65. route_table_id = aws_route_table.pubroute.id
  66. }
  67. resource "aws_route_table_association" "pvtsub2" {
  68. subnet_id = aws_subnet.subnet2.id
  69. route_table_id = aws_route_table.pubroute.id
  70. }
  71. resource "aws_route_table_association" "pvtsub1" {
  72. subnet_id = aws_subnet.subnet3.id
  73. route_table_id = aws_route_table.pvtroute.id
  74. }
  75. resource "aws_route_table_association" "pvtsub2" {
  76. subnet_id = aws_subnet.subnet4.id
  77. route_table_id = aws_route_table.pvtroute.id
  78. }
  79. ############ EC2 Creation with ALB & Auto scaling Group ##########
  80. resource "aws_security_group" "web_sg" {
  81. name_prefix = "web-sg-"
  82. # Inbound rule for SSH traffic
  83. ingress {
  84. from_port = 22
  85. to_port = 22
  86. protocol = "tcp"
  87. cidr_blocks = ["0.0.0.0/0"]
  88. }
  89. # Inbound rule for HTTP traffic
  90. ingress {
  91. from_port = 80
  92. to_port = 80
  93. protocol = "tcp"
  94. cidr_blocks = ["0.0.0.0/0"]
  95. }
  96. # Inbound rule for HTTPS traffic
  97. ingress {
  98. from_port = 443
  99. to_port = 443
  100. protocol = "tcp"
  101. cidr_blocks = ["0.0.0.0/0"]
  102. }
  103. # Outbound rule allowing all traffic
  104. egress {
  105. from_port = 0
  106. to_port = 0
  107. protocol = "-1"
  108. cidr_blocks = ["0.0.0.0/0"]
  109. }
  110. }
  111. resource "aws_launch_configuration" "web_lc" {
  112. name_prefix = "web-lc"
  113. image_id = var.ami
  114. instance_type = var.instance_type
  115. security_groups = [aws_security_group.web_sg.id]
  116. key_name = var.keyname
  117. user_data = <<-EOF
  118. #!/bin/bash
  119. apt update
  120. apt upgrade -y
  121. EOF
  122. lifecycle {
  123. create_before_destroy = true
  124. }
  125. }
  126. resource "aws_autoscaling_group" "web_asg" {
  127. name_prefix = "web-asg"
  128. min_size = 2
  129. max_size = 5
  130. desired_capacity = 2
  131. launch_configuration = aws_launch_configuration.web_lc.name
  132. // Define your load balancer target group ARNs here
  133. }
  134. resource "aws_lb" "example_alb" {
  135. name = "web-alb"
  136. internal = false
  137. load_balancer_type = "application"
  138. subnets = [aws_subnet.subnet1.id, aws_subnet.subnet2.id]
  139. }
  140. resource "aws_lb_listener" "example_listener" {
  141. load_balancer_arn = aws_lb.example_alb.arn
  142. port = 80
  143. protocol = "HTTP"
  144. default_action {
  145. type = "fixed-response"
  146. fixed_response {
  147. content_type = "/"
  148. status_code = "200"
  149. }
  150. }
  151. }
  152. resource "aws_lb_target_group" "web_target_group" {
  153. name = "web-target-group"
  154. port = 80
  155. protocol = "HTTP"
  156. vpc_id = aws_vpc.main.id
  157. }
  158. resource "aws_autoscaling_attachment" "web_asg_attachment" {
  159. autoscaling_group_name = aws_autoscaling_group.web_asg.name
  160. alb_target_group_arn = aws_lb_target_group.web_target_group.arn
  161. }