Created
July 26, 2019 19:10
-
-
Save Maelstromeous/839995055b993634f546857853f4d1cc to your computer and use it in GitHub Desktop.
Simple AWS Terraform script
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
# Install terraform and run "terraform apply" to use this | |
provider "aws" { | |
region = "eu-west-2" | |
} | |
data "aws_availability_zones" "all" {} | |
variable "web_port" { | |
description = "The port that will serve HTTP requests" | |
type = number | |
default = 80 | |
} | |
variable "elb_port" { | |
description = "Load balancer port" | |
type = number | |
default = 80 | |
} | |
variable "ami" { | |
description = "AMI to use across instances" | |
default = "ami-077a5b1762a2dde35" | |
} | |
variable "instance_type" { | |
description = "EC2 instance type" | |
default = "t2.micro" | |
} | |
resource "aws_security_group" "instance" { | |
name = "terraform-example-instance" | |
ingress { | |
from_port = var.web_port | |
to_port = var.web_port | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
} | |
resource "aws_launch_configuration" "example" { | |
image_id = var.ami | |
instance_type = var.instance_type | |
security_groups = [aws_security_group.instance.id] | |
user_data = <<-EOF | |
#!/bin/bash | |
echo "Hello, World" > index.html | |
nohup busybox httpd -f -p "${var.web_port}" & | |
EOF | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
resource "aws_autoscaling_group" "example" { | |
launch_configuration = aws_launch_configuration.example.id | |
min_size = 2 | |
max_size = 10 | |
availability_zones = data.aws_availability_zones.all.names | |
load_balancers = [aws_elb.example.name] | |
health_check_type = "ELB" | |
tag { | |
key = "Name" | |
value = "terraform-asg-example" | |
propagate_at_launch = true | |
} | |
} | |
resource "aws_elb" "example" { | |
name = "terraform-asg-example" | |
availability_zones = data.aws_availability_zones.all.names | |
security_groups = [aws_security_group.elb.id] | |
listener { | |
lb_port = var.elb_port | |
lb_protocol = "http" | |
instance_port = var.web_port | |
instance_protocol = "http" | |
} | |
health_check { | |
target = "HTTP:${var.web_port}/" | |
interval = 30 | |
timeout = 3 | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
} | |
} | |
resource "aws_security_group" "elb" { | |
name = "terraform-example-elb-sg" | |
egress { | |
from_port = 0 | |
protocol = "-1" | |
to_port = 0 | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
ingress { | |
from_port = 80 | |
protocol = "tcp" | |
to_port = var.web_port | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
} | |
output "clb_dns_name" { | |
value = aws_elb.example.dns_name | |
description = "The domain name for the CLB" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment