diff --git a/application_load_balancer/README.md b/application_load_balancer/README.md new file mode 100644 index 00000000..892053b9 --- /dev/null +++ b/application_load_balancer/README.md @@ -0,0 +1,21 @@ +# application_load_balancer + +These templates implements a application load balancer, and associated necessary steps require for loadbalancing. We used below services : + +- Security groups for instances and loadbalancer +- Target grout attachment for instances +- Template for launching instances +- Load balancer + + +-- Mention your region, secret and access keys, vpc_id, subnet_ids and ami_id required in the templates. + +To run these templates, clone the repository and run `terraform apply` within its own directory. + +For example: + +```tf +$ git clone https://github.com/wardviaene/terraform-course.git +$ cd terraform-course/application_load_balancer/ +$ terraform apply +``` diff --git a/application_load_balancer/instances.tf b/application_load_balancer/instances.tf new file mode 100644 index 00000000..a47b5c1d --- /dev/null +++ b/application_load_balancer/instances.tf @@ -0,0 +1,45 @@ +resource "aws_instance" "web1" { + ami = "enter-ami-id" + instance_type = "t2.micro" + subnet_id = "enter-subnet-id" + vpc_security_group_ids = [aws_security_group.allow_http_instances.id] + key_name = "enter-key-name" + provisioner "remote-exec" { + inline = [ + "sudo yum install httpd -y", + "sudo service httpd start", + "sudo chkconfig httpd on" + ] + + connection { + type = "ssh" + user = "ec2-user" + host = aws_instance.web.public_ip + private_key = file("${path.module}/key-name.pem") + } + +} +} + +resource "aws_instance" "web2" { + ami = "enter-ami-id" + instance_type = "t2.micro" + subnet_id = "enter-your-subnet-id" + vpc_security_group_ids = [aws_security_group.allow_http_instances.id] + key_name = "enter-key-name" + provisioner "remote-exec" { + inline = [ + "sudo yum install https -y", + "sudo service httpd start", + "sudo chkconfig httpd on" + ] + + connection { + type = "ssh" + user = "ec2-user" + host = aws_instance.web2.public_ip + private_key = file("${path.module}/kay-name.pem") + } + +} +} diff --git a/application_load_balancer/loadbalancer.tf b/application_load_balancer/loadbalancer.tf new file mode 100644 index 00000000..d7d947d3 --- /dev/null +++ b/application_load_balancer/loadbalancer.tf @@ -0,0 +1,27 @@ +resource "aws_lb" "my-lb" { + name = "lb-tf" + internal = false + load_balancer_type = "application" + security_groups = [aws_security_group.allow_http.id] + # Enter you subnet ids under vpc below + subnets = ["subnet-id1","subnet-id2","subnet-id3","subnet-id4"] + + enable_deletion_protection = false + + + + tags = { + name = "my-first-load-balancer" + } +} + +resource "aws_lb_listener" "front_end" { + load_balancer_arn = aws_lb.my-lb.arn + port = "80" + protocol = "HTTP" + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.target-lb.arn + } +} diff --git a/application_load_balancer/provider.tf b/application_load_balancer/provider.tf new file mode 100644 index 00000000..640bc033 --- /dev/null +++ b/application_load_balancer/provider.tf @@ -0,0 +1,5 @@ +provider "aws" { + region = "Enter_Region" + access_key = "Enter_Access_Key" + secret_key = "Enter_Secret_Key" +} diff --git a/application_load_balancer/security_group.tf b/application_load_balancer/security_group.tf new file mode 100644 index 00000000..19e3ce21 --- /dev/null +++ b/application_load_balancer/security_group.tf @@ -0,0 +1,56 @@ +resource "aws_security_group" "allow_http" { + name = "alb_http" + description = "Allow http traffic to alb" + vpc_id = "enter_vpc_id" + + ingress { + description = "http for alb" + from_port = 80 + to_port = 80 + 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 = "allow_http_alb" + } +} + +resource "aws_security_group" "allow_http_instances" { + name = "instances_http" + description = "Allow http traffic to instances" + vpc_id = "enter_vpc_id" + + ingress { + description = "http for instances" + from_port = 80 + to_port = 80 + protocol = "tcp" + security_groups = [aws_security_group.allow_http.id] + } + +ingress { + description = "ssh for instances" + 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 = "allow_http_instaces" + } +} diff --git a/application_load_balancer/target_group_attach.tf b/application_load_balancer/target_group_attach.tf new file mode 100644 index 00000000..25b085f7 --- /dev/null +++ b/application_load_balancer/target_group_attach.tf @@ -0,0 +1,16 @@ +resource "aws_lb_target_group" "target-lb" { + name = "lb-tg" + port = 80 + protocol = "HTTP" + vpc_id = "enter_vpc_id" +} +resource "aws_lb_target_group_attachment" "test1" { + target_group_arn = aws_lb_target_group.target-lb.arn + target_id = aws_instance.web1.id + port = 80 +} +resource "aws_lb_target_group_attachment" "test2" { + target_group_arn = aws_lb_target_group.target-lb.arn + target_id = aws_instance.web2.id + port = 80 +}