Terraform
Provision server with Terraform
Full configuration for the same task:
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
}
}
In summary, this Terraform script sets up an environment with an Auto Scaling group of instances, which are configured at launch with a specific user data script. These instances are registered with an Application Load Balancer, which distributes incoming traffic across the instances. The health of the instances is monitored based on the settings in the target group.
To Add Atlantis β --> https://www.runatlantis.io/docs/installation-guide.html
Additional we can add Terrahax: https://docs.terrahaxs.com/#/
Syntax: https://developer.hashicorp.com/terraform/language/syntax/configuration
---
Other resources:
Last updated
Was this helpful?