Skip to content

Instantly share code, notes, and snippets.

@webmasterdevlin
Created June 13, 2020 11:04
Show Gist options
  • Save webmasterdevlin/341be5930661c51d201eb1ba7dfa6236 to your computer and use it in GitHub Desktop.
Save webmasterdevlin/341be5930661c51d201eb1ba7dfa6236 to your computer and use it in GitHub Desktop.
Terraform AWS
provider "aws" {
region = "us-east-2"
}
resource "aws_launch_configuration" "test" {
image_id = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
security_groups = [aws_security_group.test.id]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p "${var.server_port}" &
EOF
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "test" {
name = "devlin-security-group-demo"
ingress {
from_port = var.server_port
to_port = var.server_port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_autoscaling_group" "test" {
launch_configuration = aws_launch_configuration.test.id
availability_zones = data.aws_availability_zones.all.names
min_size = 2
max_size = 4
load_balancers = [aws_elb.test.name]
health_check_type = "ELB"
tag {
key = "Name"
value = "terraform-asg-demo"
propagate_at_launch = true
}
}
data "aws_availability_zones" "all" {
}
resource "aws_elb" "test" {
name = "terraform-clb-demo"
security_groups = [aws_security_group.test2.id]
availability_zones = data.aws_availability_zones.all.names
# this adds a listener for incoming HTTP requests
listener {
lb_port = 80
lb_protocol = "http"
instance_port = var.server_port
instance_protocol = "http"
}
health_check {
target = "HTTP:${var.server_port}/"
interval = 30
timeout = 3
healthy_threshold = 2
unhealthy_threshold = 2
}
}
resource "aws_security_group" "test2" {
name = "devlin-security-group-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"]
}
}
output "clb_dns_name" {
value = aws_elb.test.dns_name
description = "The domain name of the load balancer"
}
variable "server_port" {
description = "The port the server will use for HTTP requests"
type = number
default = 8080
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment