Terraform
Provision server with Terraform
provider "aws" {
region = "us-east-2"
}
resource "aws_launch_configuration" "my_lc" {
name_prefix = "terraform-lc"
image_id = "ami-024e6efaf93d85776"
instance_type = "t2.micro"
key_name = "stefano-us-east-2"
security_groups = ["sg-a7f988c5"]
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common apache2
echo "Version: 1.0.0" | sudo tee /var/www/html/index.html
sudo systemctl restart apache2
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
sudo systemctl start docker
sudo systemctl enable docker
sudo docker run -d -p 3000:3000 grafana/grafana
docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token eyJhIjoiNmI2YWQzZDVhOWM2NWY3Y2E5MTViYzZjZTMyZTk3YmQiLCJ0IjoiMDFiNjY3ZWEtYWQzYS00MDNhLWJhYTItZDU1MWY5ZWRhNDM1IiwicyI6IlpXSTFNelZsWXpjdFl6YzNaUzAwTVRZeUxUa3daV1F0T0dJMU1EQmlZelV5TkRCbCJ9
bash <(curl -Ss https://my-netdata.io/kickstart.sh) --dont-wait
EOF
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "my_asg" {
desired_capacity = 2
launch_configuration = aws_launch_configuration.my_lc.name
max_size = 4
min_size = 2
vpc_zone_identifier = ["subnet-feced596", "subnet-c7dc8dbd", "subnet-52d9611e"]
target_group_arns = [aws_lb_target_group.my_tg.arn]
}
resource "aws_lb" "my_lb" {
name = "my-lb"
internal = false
load_balancer_type = "application"
security_groups = ["sg-a7f988c5"]
subnets = ["subnet-feced596", "subnet-c7dc8dbd", "subnet-52d9611e"]
}
resource "aws_lb_target_group" "my_tg" {
name = "tf-example-lb-tg"
port = 80
protocol = "HTTP"
vpc_id = "vpc-73cb3818"
health_check {
enabled = true
interval = 30
path = "/"
protocol = "HTTP"
timeout = 5
healthy_threshold = 5
unhealthy_threshold = 2
matcher = "200"
}
}
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.my_tg.arn
}
}
Last updated