Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_application_load_balancer #39

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
21 changes: 21 additions & 0 deletions aws/aws_application_load_balancer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# aws_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/futurice/terraform-examples.git
$ cd terraform-examples/aws/aws_application_load_balancer/
$ terraform apply
```
45 changes: 45 additions & 0 deletions aws/aws_application_load_balancer/instances.tf
Original file line number Diff line number Diff line change
@@ -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")
}

}
}
27 changes: 27 additions & 0 deletions aws/aws_application_load_balancer/loadbalancer.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 5 additions & 0 deletions aws/aws_application_load_balancer/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "aws" {
region = "Enter_Region"
access_key = "Enter_Access_Key"
secret_key = "Enter_Secret_Key"
}
56 changes: 56 additions & 0 deletions aws/aws_application_load_balancer/security_group.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
16 changes: 16 additions & 0 deletions aws/aws_application_load_balancer/target_group_attach.tf
Original file line number Diff line number Diff line change
@@ -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
}