Following aws autoscaling course from https://github.com/Lowess
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.2 KiB
161 lines
3.2 KiB
### Provider definition
|
|
|
|
provider "aws" {
|
|
region = var.aws_region
|
|
}
|
|
|
|
data "aws_vpc" "main" {
|
|
id = module.discovery.vpc_id
|
|
}
|
|
|
|
### Usage du module discobery pour utiliser directement les elements de notr VPC custom
|
|
|
|
# ami = "ami-0144fa93c0eb299d2"
|
|
|
|
module "discovery" {
|
|
source = "../discovery-module"
|
|
|
|
aws_region = var.aws_region
|
|
vpc_name = var.vpc_name
|
|
}
|
|
|
|
### Security groups
|
|
|
|
resource "aws_security_group" "allow_80_from_inet" {
|
|
name = "allow_80_from_inet"
|
|
description = "allow_80_from_inet inbound traffic"
|
|
vpc_id = module.discovery.vpc_id
|
|
|
|
ingress {
|
|
description = "HTTP 80 from internet"
|
|
from_port = 80
|
|
to_port = 80
|
|
protocol = "tcp"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
}
|
|
|
|
egress {
|
|
from_port = 0
|
|
to_port = 0
|
|
protocol = "-1"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
}
|
|
|
|
tags = {
|
|
Name = "allow_80_from_inet"
|
|
}
|
|
}
|
|
|
|
resource "aws_security_group" "allow_8080_from_vpc" {
|
|
name = "allow_8080_from_vpc"
|
|
description = "allow_8080_from_vpc inbound traffic"
|
|
vpc_id = module.discovery.vpc_id
|
|
|
|
ingress {
|
|
description = "allow_8080_from_vpc from VPC"
|
|
from_port = 8080
|
|
to_port = 8080
|
|
protocol = "tcp"
|
|
cidr_blocks = [data.aws_vpc.main.cidr_block]
|
|
}
|
|
|
|
egress {
|
|
from_port = 0
|
|
to_port = 0
|
|
protocol = "-1"
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
}
|
|
|
|
tags = {
|
|
Name = "allow_8080_from_vpc"
|
|
}
|
|
}
|
|
|
|
### Create an AWS ALB
|
|
|
|
resource "aws_lb" "front_end" {
|
|
name = "front-end-lb-tf"
|
|
internal = false
|
|
load_balancer_type = "application"
|
|
security_groups = [aws_security_group.allow_80_from_inet.id]
|
|
subnets = module.discovery.public_subnets # notre liste de sous réseaux
|
|
|
|
enable_deletion_protection = true
|
|
|
|
tags = {
|
|
Environment = "production"
|
|
}
|
|
}
|
|
|
|
resource "aws_lb_target_group" "front_end" {
|
|
name = "tf-front-end-lb-tg"
|
|
port = 8080 # port sur lequel les targets web reçoivent le traffic
|
|
protocol = "HTTP"
|
|
vpc_id = module.discovery.vpc_id
|
|
}
|
|
|
|
resource "aws_lb_listener" "front_end" {
|
|
load_balancer_arn = aws_lb.front_end.arn
|
|
port = "80"
|
|
protocol = "HTTP"
|
|
|
|
default_action {
|
|
type = "forward"
|
|
target_group_arn = aws_lb_target_group.front_end.arn
|
|
}
|
|
}
|
|
|
|
### Create the AWS Autoscaling template & group
|
|
|
|
resource "aws_launch_template" "web_template" {
|
|
name = "web_template"
|
|
|
|
capacity_reservation_specification {
|
|
capacity_reservation_preference = "open"
|
|
}
|
|
|
|
image_id = "ami-0144fa93c0eb299d2" # notre ami custom ami-0144fa93c0eb299d2
|
|
|
|
instance_initiated_shutdown_behavior = "terminate"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
monitoring {
|
|
enabled = true
|
|
}
|
|
|
|
disable_api_termination = true
|
|
|
|
vpc_security_group_ids = [aws_security_group.allow_8080_from_vpc.id]
|
|
|
|
tag_specifications {
|
|
resource_type = "instance"
|
|
|
|
tags = {
|
|
Name = "${var.vpc_name}-template-web"
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_autoscaling_group" "as_group" {
|
|
vpc_zone_identifier = module.discovery.private_subnets
|
|
desired_capacity = 3
|
|
max_size = 4
|
|
min_size = 3
|
|
|
|
target_group_arns = [aws_lb_target_group.front_end.arn]
|
|
|
|
launch_template {
|
|
id = aws_launch_template.web_template.id
|
|
version = "$Latest"
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|