From d0db52d90c08d2a957bc71cc9558951a108301d2 Mon Sep 17 00:00:00 2001 From: "art.dambrine" Date: Fri, 29 Jan 2021 12:08:46 +0100 Subject: [PATCH] 1st part TP ok --- .terraform.lock.hcl | 19 +++++++++++++++++++ TP Terraform - AWS.md | 17 +++++++++++++++++ main.tf | 41 +++++++++++++++++++++++++++++++++++++++++ outputs.tf | 3 +++ variables.tf | 23 +++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 .terraform.lock.hcl diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..3c981c6 --- /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.26.0" + hashes = [ + "h1:0i78FItlPeiomd+4ThZrtm56P5K33k7/6dnEe4ZePI0=", + "zh:26043eed36d070ca032cf04bc980c654a25821a8abc0c85e1e570e3935bbfcbb", + "zh:2fe68f3f78d23830a04d7fac3eda550eef1f627dfc130486f70a65dc5c254300", + "zh:3d66484c608c64678e639db25d63872783ce60363a1246e30317f21c9c23b84b", + "zh:46ffd755cfd4cf94fe66342797b5afdcef010a24e126c67fee141b357d393535", + "zh:5e96f24357e945c9067cf5e032ad1d003609629c956c2f9f642fefe714e74587", + "zh:60c27aca36bb63bf3e865c2193be80ca83b376581d00f9c220af4b013e163c4d", + "zh:896f0f22d19d41e71b22f9240b261714c3915b165ddefeb771e7734d69dc47ea", + "zh:90de9966cb2fd3e2f326df291595e55d2dd2d90e7d6dd085c2c8691dce82bdb4", + "zh:ad05a91a88ceb1d6de5a568f7cc0b0e5bc0a79f3da70bc28c1e7f3750e362d58", + "zh:e8c63f59c6465329e1f3357498face3dd7ef10a033df3c366a33aa9e94b46c01", + ] +} diff --git a/TP Terraform - AWS.md b/TP Terraform - AWS.md index 02752d4..aa731b4 100644 --- a/TP Terraform - AWS.md +++ b/TP Terraform - AWS.md @@ -2,3 +2,20 @@ https://infrastructure.aws +Mise en place de l'envirronement de dev + +``` +terraform init +terraform plan +``` + + + +## Importation de AWS vers notre code Terraform + +Exemple avec un vpc : + +``` +terraform import aws_vpc.main +``` + diff --git a/main.tf b/main.tf index 96771af..1110a22 100644 --- a/main.tf +++ b/main.tf @@ -4,3 +4,44 @@ provider "aws" { region = var.aws_region } +resource "aws_vpc" "main" { + cidr_block = var.vpc_cidr + + tags = { + Name = "${var.vpc_name}-vpc", + Terraform = true, + Environment = "production" + } +} + +## Fonction super utile pour le calc de ss réseaux +# cidrsubnet("172.22.0.0/16",4,iterate_value) + +resource "aws_subnet" "private" { + for_each = var.azs + + vpc_id = aws_vpc.main.id + cidr_block = cidrsubnet(var.vpc_cidr, 4, each.value) + availability_zone = "${var.aws_region}${each.key}" + + map_public_ip_on_launch = false + + tags = { + Name = "${var.vpc_name}-private-${var.aws_region}${each.key}" + } +} + +resource "aws_subnet" "public" { + for_each = var.azs + + vpc_id = aws_vpc.main.id + cidr_block = cidrsubnet(var.vpc_cidr, 4, 15 - each.value) + availability_zone = "${var.aws_region}${each.key}" + + map_public_ip_on_launch = true + + tags = { + Name = "${var.vpc_name}-public-${var.aws_region}${each.key}" + } +} + diff --git a/outputs.tf b/outputs.tf index e69de29..c6852c4 100644 --- a/outputs.tf +++ b/outputs.tf @@ -0,0 +1,3 @@ +output "vpc_id" { + value = aws_vpc.main.id +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 647f418..4543396 100644 --- a/variables.tf +++ b/variables.tf @@ -2,3 +2,26 @@ variable "aws_region" { type = string default = "us-east-1" } + +variable "vpc_name" { + type = string + default = "my-could" + description = "VPC name" +} + +variable "vpc_cidr" { + type = string + default = "172.22.0.0/16" + description = "VPC Cidr block" +} + +variable "azs" { + type = map(any) + default = { + "a" = 0, + "b" = 1, + "c" = 2, + "d" = 3 + } + description = "Availability zones to create within subnet" +}