Last active
June 10, 2022 14:42
-
-
Save phunguyen19/47046fa7a0967bdd5e16f638c19a76c8 to your computer and use it in GitHub Desktop.
terraform aws ec2 web auto scale load balancing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| provider "aws" { | |
| region = "ap-southeast-1" | |
| } | |
| variable "server_port" { | |
| description = "The port the server will use for HTTP requests" | |
| type = number | |
| default = 80 | |
| } | |
| output "clb_dns_name" { | |
| value = aws_elb.example.dns_name | |
| description = "The domain name of the load balancer" | |
| } | |
| data "aws_availability_zones" "all" {} | |
| resource "aws_instance" "web" { | |
| ami = "ami-00b8d9cb8a7161e41" | |
| instance_type = "t2.micro" | |
| vpc_security_group_ids = [aws_security_group.web.id] | |
| user_data = <<-EOF | |
| #!/bin/bash | |
| # Install Apache server | |
| yum update -y | |
| yum install -y httpd.x86_64 | |
| systemctl start httpd.service | |
| systemctl enable httpd.service | |
| echo "Hello world $(hostname -f)" > /var/www/html/index.html | |
| EOF | |
| tags = { | |
| Name = "terraform-example" | |
| } | |
| } | |
| resource "aws_security_group" "web" { | |
| name = "terraform-sg-web" | |
| tags = { | |
| "Name" = "terraform-example" | |
| } | |
| ingress { | |
| from_port = var.server_port | |
| to_port = var.server_port | |
| protocol = "tcp" | |
| cidr_blocks = ["0.0.0.0/0"] | |
| ipv6_cidr_blocks = ["::/0"] | |
| } | |
| egress { | |
| from_port = 0 | |
| to_port = 0 | |
| protocol = "-1" | |
| cidr_blocks = ["0.0.0.0/0"] | |
| } | |
| } | |
| resource "aws_launch_configuration" "web" { | |
| image_id = "ami-00b8d9cb8a7161e41" | |
| instance_type = "t2.micro" | |
| security_groups = [aws_security_group.web.id] | |
| user_data = <<-EOF | |
| #!/bin/bash | |
| # Install Apache server | |
| yum update -y | |
| yum install -y httpd.x86_64 | |
| systemctl start httpd.service | |
| systemctl enable httpd.service | |
| echo "Hello world $(hostname -f)" > /var/www/html/index.html | |
| EOF | |
| lifecycle { | |
| create_before_destroy = true | |
| } | |
| } | |
| resource "aws_autoscaling_group" "web" { | |
| launch_configuration = aws_launch_configuration.web.id | |
| availability_zones = data.aws_availability_zones.all.names | |
| min_size = 2 | |
| max_size = 10 | |
| load_balancers = [aws_elb.example.name] | |
| # Default of this config is "EC2" | |
| # This config tell ASG use ELB auto healthcheck | |
| # to determine if the EC2 instances are healthy or not | |
| health_check_type = "ELB" | |
| tag { | |
| key = "Name" | |
| value = "terraform-asg-example" | |
| propagate_at_launch = true | |
| } | |
| } | |
| resource "aws_elb" "example" { | |
| name = "terraform-asg-example" | |
| security_groups = [aws_security_group.elb.id] | |
| availability_zones = data.aws_availability_zones.all.names | |
| health_check { | |
| target = "HTTP:${var.server_port}/" | |
| interval = 30 | |
| timeout = 3 | |
| healthy_threshold = 2 | |
| unhealthy_threshold = 2 | |
| } | |
| # This adds a listener for incoming HTTP requests. | |
| listener { | |
| lb_port = 80 | |
| lb_protocol = "http" | |
| instance_port = var.server_port | |
| instance_protocol = "http" | |
| } | |
| } | |
| resource "aws_security_group" "elb" { | |
| name = "terraform-example-elb" | |
| # Allow all outbound | |
| egress { | |
| from_port = 0 | |
| to_port = 0 | |
| protocol = "-1" | |
| cidr_blocks = ["0.0.0.0/0"] | |
| } | |
| # Inbound HTTP from anywhere | |
| ingress { | |
| from_port = 80 | |
| to_port = 80 | |
| protocol = "tcp" | |
| cidr_blocks = ["0.0.0.0/0"] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment