From 0ef8a3a02c432ab24107d648d1f24a21be3cc9c2 Mon Sep 17 00:00:00 2001 From: "art.dambrine" Date: Sun, 21 Feb 2021 15:56:12 +0100 Subject: [PATCH] refacto tf autoscalling --- .terraform.lock.hcl | 19 ++++++ main.tf | 157 +++++++++++++++++++++++++++++++++++++++++++- outputs.tf | 8 +++ variables.tf | 10 +++ 4 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 .terraform.lock.hcl diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..a79e6a8 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,19 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.27.0" + hashes = [ + "h1:ccxtk7jAtmBPvAEXswOEYJcyp5jTD9QlQeg8GEzYmxQ=", + "zh:2986eb5a1ffbb0336c6390aad533b62efc832aa8aa5460d523e1f2daa4f42f79", + "zh:825317cdb80860833125a856c0befc877cba22d41c631c5a7ca22400693d4356", + "zh:a47aad668cc74058f508c56c5407cd715dbb9b6389aa68d37543e897895db43f", + "zh:c0011502d0eb4637918127c3987a8cc07a015ea00f74f4956fd111c736286a4d", + "zh:d5088ab51043bb2239132f4ed3760292b6aa4f7296232e4b8017f8c5c34f051a", + "zh:d893658e983eb17a23a8124c79a910cc729cb1d751d5509b8e756101c828ad91", + "zh:dcc4384ee79ea9492c87eb01e664f7f6b1f1d156471476f30b28336c9d9a4aec", + "zh:e4abfaf013f31791cd029af7b6f989f73e3efca28fe2917057b428d051c4085f", + "zh:f2a4d9446d23afe2a42421e7d5f902d34451fb31b7787b5e3aef95c08fec5ced", + "zh:f54a6af10b077db9dc11556c27f59ba5c60e1b2ba96fe3aa9cd90d8c67d980f6", + ] +} diff --git a/main.tf b/main.tf index abb1899..bceac46 100644 --- a/main.tf +++ b/main.tf @@ -1,8 +1,161 @@ ### Provider definition provider "aws" { - region = "${var.aws_region}" + region = var.aws_region } -### Module Main +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" + } +} + + + + + + + + diff --git a/outputs.tf b/outputs.tf index 8b13789..e48fe50 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1 +1,9 @@ +# Décommentez le output dicovery pour découvrir le VPC +# output "discovery" { +# value = module.discovery +# } + +output "vpc_id" { + value = module.discovery.vpc_id +} diff --git a/variables.tf b/variables.tf index 8b13789..9a94096 100644 --- a/variables.tf +++ b/variables.tf @@ -1 +1,11 @@ +variable "aws_region" { + type = string + default = "us-east-1" +} + +variable "vpc_name" { + type = string + default = "mycould" + description = "VPC name" +}