Terraform ELB
provider “aws” {
region = “us-east-1”
}
resource “aws_instance” “myFirstInstance” {
ami = “ami-05fa00d4c63e32376”
instance_type = “t2.micro”
key_name= “newkey”
vpc_security_group_ids = [aws_security_group.main.id]
count=2
tags = {
“Name” = “UserData command “
}
user_data = <<-EOF
#!/bin/bash
sudo su
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo echo “<html><body><h1>Hello this is module-1 at instance id `curl http://169.254.169.254/latest/meta-data/instance-id` </h1></body></html>” > /var/www/html/index.html
EOF
}
resource “aws_security_group” “main” {
egress = [
{
cidr_blocks = [ “0.0.0.0/0”, ]
description = “”
from_port = 0
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = “-1”
security_groups = []
self = false
to_port = 0
}
]
ingress = [
{
cidr_blocks = [ “0.0.0.0/0”, ]
description = “”
from_port = 22
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = “tcp”
security_groups = []
self = false
to_port = 22
},
{
cidr_blocks = [ “0.0.0.0/0”, ]
description = “”
from_port = 80
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = “tcp”
security_groups = []
self = false
to_port = 80
}
]
}
module “elb_http” {
source = “terraform-aws-modules/elb/aws”
version = “~> 2.0”
name = “elb-example1”
subnets = [“subnet-39016813”,“subnet-c328bfcf”,“subnet-e0bae1dd”,“subnet-e8cda68d”,“subnet-e9623a9f”]
security_groups = [aws_security_group.main.id]
internal = false
listener = [
{
instance_port = 80
instance_protocol = “HTTP”
lb_port = 80
lb_protocol = “HTTP”
},
{
instance_port = 8080
instance_protocol = “http”
lb_port = 8080
lb_protocol = “http”
},
]
health_check = {
target = “HTTP:80/”
interval = 30
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
}
// ELB attachments
number_of_instances = 2
instances = [aws_instance.myFirstInstance[0].id,aws_instance.myFirstInstance[1].id]
tags = {
Owner = “user”
Environment = “dev”
}
}