From 478401f7512d2f5aee9e102b67f4058702e17949 Mon Sep 17 00:00:00 2001 From: Johan LEROY Date: Wed, 5 Aug 2026 09:11:14 +0200 Subject: [PATCH] finish --- atelier3/.gitignore | 40 +++++++++++++++++ atelier3/.terraform.lock.hcl | 25 +++++++++++ atelier3/main.tf | 65 +++++++++++++++++++++++++++ atelier3/modules/network/main.tf | 26 +++++++++++ atelier3/modules/network/outputs.tf | 22 +++++++++ atelier3/modules/network/variables.tf | 31 +++++++++++++ atelier3/outputs.tf | 4 ++ atelier3/providers.tf | 3 ++ atelier3/terraform.tf | 8 ++++ atelier3/variables.tf | 64 ++++++++++++++++++++++++++ 10 files changed, 288 insertions(+) create mode 100644 atelier3/.gitignore create mode 100644 atelier3/.terraform.lock.hcl create mode 100644 atelier3/main.tf create mode 100644 atelier3/modules/network/main.tf create mode 100644 atelier3/modules/network/outputs.tf create mode 100644 atelier3/modules/network/variables.tf create mode 100644 atelier3/outputs.tf create mode 100644 atelier3/providers.tf create mode 100644 atelier3/terraform.tf create mode 100644 atelier3/variables.tf diff --git a/atelier3/.gitignore b/atelier3/.gitignore new file mode 100644 index 0000000..f5311ae --- /dev/null +++ b/atelier3/.gitignore @@ -0,0 +1,40 @@ +# Created by https://www.toptal.com/developers/gitignore/api/terraform +# Edit at https://www.toptal.com/developers/gitignore?templates=terraform + +### Terraform ### +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +# End of https://www.toptal.com/developers/gitignore/api/terraform \ No newline at end of file diff --git a/atelier3/.terraform.lock.hcl b/atelier3/.terraform.lock.hcl new file mode 100644 index 0000000..67d6eed --- /dev/null +++ b/atelier3/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "6.38.0" + constraints = "6.38.0" + hashes = [ + "h1:IMf41BcW9huOeVcrt6XjQqadYR2xD8zkUpGLLERJ4NM=", + "zh:143f118ae71059a7a7026c6b950da23fef04a06e2362ffa688bef75e43e869ed", + "zh:29ee220a017306effd877e1280f8b2934dc957e16e0e72ca0222e5514d0db522", + "zh:3a31baabf7aea7aa7669f5a3d76f3445e0e6cce5e9aea0279992765c0df12aee", + "zh:4c1908e62040dbc9901d4426ffb253f53e5dae9e3e1a9125311291ee265c8d8c", + "zh:550f4789f5f5b00e16118d4c17770be3ef4535d6b6928af1cf91ebd30f2c263b", + "zh:6537b7b70bf2c127771b0b84e4b726c834d10666b6104f017edae50c67ebae37", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:af2f9cea0c8bdf5b2a2391f2d179a946c117196f7c829b919673cae3b71d2943", + "zh:c53ffa685381aa4e73158fd9f529239f95938dea330e7aca0b32e7b2a1210432", + "zh:d0995e1d64a7ec8bbc79fc3fbec3749f989e07f211a318705c37cd6a7c7d19e4", + "zh:d2348ffcffc1282983d7a5838dd5d61f372152fe6c0d10868cd6473352318750", + "zh:e449312efb73e4747165e689302a68a1df8ba5755e7f59097069acf82c94f011", + "zh:ec3a538d264ef79380e56fdf107ffb6c0446814f07fc5890c36855fe1e03196b", + "zh:f441e69699b22e32c96a8cdd3bbe694ed302c0dcfe867cd9bd683a16df362714", + "zh:f6f8eaa605ff902234d7e9bdab4fda977185fce14f8576f7b622c914c7d98008", + ] +} diff --git a/atelier3/main.tf b/atelier3/main.tf new file mode 100644 index 0000000..6bb8b9c --- /dev/null +++ b/atelier3/main.tf @@ -0,0 +1,65 @@ +locals { + company = "ENI" + trigram = lower(var.trigram) +} + +module "network" { + source = "./modules/network" + + vpc_name = "${local.trigram}-main-vpc" + vpc_cidr_block = "10.1.0.0/16" + common_tags = var.common_tags + subnets = var.subnets +} + +data "aws_ami" "ami" { + for_each = var.images + + most_recent = true + + filter { + name = "name" + values = [each.value.name_filter] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = [each.value.owner] +} + +resource "aws_network_interface" "example" { + for_each = var.instances + + subnet_id = module.network.subnets_full[each.value.subnet].id +} + +resource "aws_ebs_volume" "vol" { + for_each = var.instances + + size = 10 + availability_zone = aws_instance.instance[each.key].availability_zone +} + +resource "aws_instance" "instance" { + for_each = var.instances + + ami = data.aws_ami.ami[each.value.image].id + instance_type = each.value.instance_type + availability_zone = module.network.subnets_full[each.value.subnet].availability_zone + + + tags = merge(var.common_tags, { + Name = "${local.trigram}-${each.key}" + }) +} + +resource "aws_volume_attachment" "ebs_att" { + for_each = var.instances + + device_name = "/dev/sdh" + volume_id = aws_ebs_volume.vol[each.key].id + instance_id = aws_instance.instance[each.key].id +} \ No newline at end of file diff --git a/atelier3/modules/network/main.tf b/atelier3/modules/network/main.tf new file mode 100644 index 0000000..d06aa0d --- /dev/null +++ b/atelier3/modules/network/main.tf @@ -0,0 +1,26 @@ +resource "aws_vpc" "main_vpc" { + cidr_block = var.vpc_cidr_block + + tags = merge(var.common_tags, { + Name = var.vpc_name + }) +} + +data "aws_availability_zones" "az" { + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +resource "aws_subnet" "subnet" { + for_each = var.subnets + + vpc_id = aws_vpc.main_vpc.id + cidr_block = each.value.cidr + availability_zone = element(data.aws_availability_zones.az.names, each.value.az_id) + + tags = merge(var.common_tags, { + Name = "${var.subnet_name_prefix}-${each.key}" + }) +} \ No newline at end of file diff --git a/atelier3/modules/network/outputs.tf b/atelier3/modules/network/outputs.tf new file mode 100644 index 0000000..6e4a660 --- /dev/null +++ b/atelier3/modules/network/outputs.tf @@ -0,0 +1,22 @@ +output "subnet_ids" { + description = "Subnet IDs" + value = { for k, v in aws_subnet.subnet : k => v.id } +} + +output "subnet_az" { + description = "Subnet AZs" + value = { for k, v in aws_subnet.subnet : k => v.availability_zone } +} + +output "subnets" { + description = "Subnets data" + value = { for k, v in aws_subnet.subnet : k => { + id = v.id, + az = v.availability_zone + }} +} + +output "subnets_full" { + description = "Subnets all data" + value = aws_subnet.subnet +} \ No newline at end of file diff --git a/atelier3/modules/network/variables.tf b/atelier3/modules/network/variables.tf new file mode 100644 index 0000000..90342b1 --- /dev/null +++ b/atelier3/modules/network/variables.tf @@ -0,0 +1,31 @@ +variable "vpc_name" { + type = string + description = "VPC name" +} + +variable "vpc_cidr_block" { + description = "VPC CIDR block" + type = string + default = "10.0.0.0/16" +} + +variable "subnet_name_prefix" { + description = "Subnet name prefix" + type = string + default = "sub" +} + +variable "common_tags" { + type = map(string) + description = "Default common tags" + default = { + Owner = "user" + CourseId = "my_course" + } +} + +variable "subnets" { + description = "Subnets to create" + type = map(map(string)) + default = {} +} \ No newline at end of file diff --git a/atelier3/outputs.tf b/atelier3/outputs.tf new file mode 100644 index 0000000..7bf45ff --- /dev/null +++ b/atelier3/outputs.tf @@ -0,0 +1,4 @@ +output "instance_ip" { + description = "Instance private IP address" + value = { for k, v in aws_instance.instance : k => v.private_ip } +} \ No newline at end of file diff --git a/atelier3/providers.tf b/atelier3/providers.tf new file mode 100644 index 0000000..2a5ccf3 --- /dev/null +++ b/atelier3/providers.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = var.aws_region +} \ No newline at end of file diff --git a/atelier3/terraform.tf b/atelier3/terraform.tf new file mode 100644 index 0000000..3970399 --- /dev/null +++ b/atelier3/terraform.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "6.38.0" + } + } +} \ No newline at end of file diff --git a/atelier3/variables.tf b/atelier3/variables.tf new file mode 100644 index 0000000..505c494 --- /dev/null +++ b/atelier3/variables.tf @@ -0,0 +1,64 @@ +variable "aws_region" { + type = string + description = "AWS region" + default = "eu-west-3" +} + +variable "vpc_cidr_block" { + description = "VPC CIDR block" + type = string + default = "10.0.0.0/16" +} + + +variable "subnets" { + description = "Subnets to create" + type = map(map(string)) + default = {} +} + +variable "images" { + description = "Images to use with instances" + type = map(map(string)) + default = { + ubuntu = { + name_filter = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*", + owner = "099720109477" + }, + amazon = { + name_filter = "al2023-ami-*-kernel-6.1-x86_64" + owner = "137112412989" + } + } +} + +variable "instances" { + description = "AWS instance type for app" + type = map(map(string)) + default = { + demo01 = { + instance_type = "t3.micro", + image = "ubuntu", + subnet = "sub01" + }, + demo02 = { + instance_type = "t3.micro", + image = "amazon", + subnet = "sub02" + } + } +} + +variable "common_tags" { + type = map(string) + description = "Default common tags" + default = { + Owner = "user" + CourseId = "my_course" + } +} + +variable "trigram" { + type = string + description = "User trigram" +} \ No newline at end of file