This commit is contained in:
Johan LEROY
2026-08-05 09:11:14 +02:00
parent fdbc1d04a3
commit 478401f751
10 changed files with 288 additions and 0 deletions
+40
View File
@@ -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
+25
View File
@@ -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",
]
}
+65
View File
@@ -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
}
+26
View File
@@ -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}"
})
}
+22
View File
@@ -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
}
+31
View File
@@ -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 = {}
}
+4
View File
@@ -0,0 +1,4 @@
output "instance_ip" {
description = "Instance private IP address"
value = { for k, v in aws_instance.instance : k => v.private_ip }
}
+3
View File
@@ -0,0 +1,3 @@
provider "aws" {
region = var.aws_region
}
+8
View File
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.38.0"
}
}
}
+64
View File
@@ -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"
}