From 12f997894a2d0424c122106e9d2c187b1cead6aa Mon Sep 17 00:00:00 2001 From: Franklin Foko Date: Wed, 3 Apr 2024 12:59:15 +0200 Subject: [PATCH] projet fil rouge ok --- projet-fil-rouge/alb.tf | 14 +++++++++ projet-fil-rouge/asg.tf | 18 +++++++++++ projet-fil-rouge/networks.tf | 61 ++++++++++++++++++++++++++++++++++++ projet-fil-rouge/provider.tf | 14 +++++++++ projet-fil-rouge/rds.tf | 24 ++++++++++++++ projet-fil-rouge/sg.tf | 52 ++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+) create mode 100644 projet-fil-rouge/alb.tf create mode 100644 projet-fil-rouge/asg.tf create mode 100644 projet-fil-rouge/networks.tf create mode 100644 projet-fil-rouge/provider.tf create mode 100644 projet-fil-rouge/rds.tf create mode 100644 projet-fil-rouge/sg.tf diff --git a/projet-fil-rouge/alb.tf b/projet-fil-rouge/alb.tf new file mode 100644 index 0000000..2517625 --- /dev/null +++ b/projet-fil-rouge/alb.tf @@ -0,0 +1,14 @@ +# Create Load Balancer +resource "aws_elb" "my_elb" { + name = "my-elb" + security_groups = [aws_security_group.my_security_group.id] + subnets = [aws_subnet.public_subnet_1.id, aws_subnet.public_subnet_2.id] + cross_zone_load_balancing = true + + listener { + instance_port = 80 + instance_protocol = "HTTP" + lb_port = 80 + lb_protocol = "HTTP" + } +} \ No newline at end of file diff --git a/projet-fil-rouge/asg.tf b/projet-fil-rouge/asg.tf new file mode 100644 index 0000000..7ace9b4 --- /dev/null +++ b/projet-fil-rouge/asg.tf @@ -0,0 +1,18 @@ +# Create Auto Scaling Group +resource "aws_autoscaling_group" "my_asg" { + name = "my-asg" + min_size = 2 + max_size = 3 + desired_capacity = 2 + vpc_zone_identifier = [aws_subnet.public_subnet_1.id, aws_subnet.public_subnet_2.id] + launch_configuration = aws_launch_configuration.my_lc.name + load_balancers = [aws_elb.my_elb.name] +} + +# Create Launch Configuration +resource "aws_launch_configuration" "my_lc" { + name = "my-lc" + image_id = "ami-0d71ca6a78e324f68" # Replace with your desired AMI ID + instance_type = "t3.micro" # Replace with your desired instance type + security_groups = [aws_security_group.my_security_group.id] +} \ No newline at end of file diff --git a/projet-fil-rouge/networks.tf b/projet-fil-rouge/networks.tf new file mode 100644 index 0000000..01cc45a --- /dev/null +++ b/projet-fil-rouge/networks.tf @@ -0,0 +1,61 @@ +# Create VPC +resource "aws_vpc" "my_vpc" { + cidr_block = "10.0.0.0/16" +} + +# Create Internet Gateway +resource "aws_internet_gateway" "my_igw" { + vpc_id = aws_vpc.my_vpc.id +} + +# Create Public Subnets +resource "aws_subnet" "public_subnet_1" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = "us-east-1c" +} + +resource "aws_subnet" "public_subnet_2" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.2.0/24" + availability_zone = "us-east-1b" +} + +# Create Private Subnets +resource "aws_subnet" "private_subnet_1" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.3.0/24" + availability_zone = "us-east-1c" +} + +resource "aws_subnet" "private_subnet_2" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.4.0/24" + availability_zone = "us-east-1b" +} + +# Create a route table for public subnets +resource "aws_route_table" "public_route_table" { + vpc_id = aws_vpc.my_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.my_igw.id + } + + tags = { + Name = "PublicRouteTable" + } +} + +# Associate the route table with public_subnet_1 +resource "aws_route_table_association" "public_subnet_1_association" { + subnet_id = aws_subnet.public_subnet_1.id + route_table_id = aws_route_table.public_route_table.id +} + +# Associate the route table with public_subnet_2 +resource "aws_route_table_association" "public_subnet_2_association" { + subnet_id = aws_subnet.public_subnet_2.id + route_table_id = aws_route_table.public_route_table.id +} \ No newline at end of file diff --git a/projet-fil-rouge/provider.tf b/projet-fil-rouge/provider.tf new file mode 100644 index 0000000..e94fcdf --- /dev/null +++ b/projet-fil-rouge/provider.tf @@ -0,0 +1,14 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} + +provider "aws" { + region = "us-east-1" + # access_key = "PUT YOUR OWN" + # secret_key = "PUT YOUR OWN" +} \ No newline at end of file diff --git a/projet-fil-rouge/rds.tf b/projet-fil-rouge/rds.tf new file mode 100644 index 0000000..c19ae51 --- /dev/null +++ b/projet-fil-rouge/rds.tf @@ -0,0 +1,24 @@ +# Create DB Subnet Group +resource "aws_db_subnet_group" "my_db_subnet_group" { + name = "my-db-subnet-group" + subnet_ids = [aws_subnet.private_subnet_1.id, aws_subnet.private_subnet_2.id] + + tags = { + Name = "my-db-subnet-group" + } +} + +# Create RDS Instance +resource "aws_db_instance" "my_rds" { + identifier = "my-rds" + engine = "mysql" + engine_version = "5.7" + instance_class = "db.t3.micro" + allocated_storage = 20 + storage_type = "gp2" + username = "admin" + password = "password" + vpc_security_group_ids = [aws_security_group.rds_security_group.id] + db_subnet_group_name = aws_db_subnet_group.my_db_subnet_group.name + multi_az = true +} \ No newline at end of file diff --git a/projet-fil-rouge/sg.tf b/projet-fil-rouge/sg.tf new file mode 100644 index 0000000..af5eb49 --- /dev/null +++ b/projet-fil-rouge/sg.tf @@ -0,0 +1,52 @@ +# Create Security Group for EC2 Instances +resource "aws_security_group" "my_security_group" { + vpc_id = aws_vpc.my_vpc.id + + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + from_port = 3306 + to_port = 3306 + 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"] + } +} + +# Create Security Group for RDS Instance +resource "aws_security_group" "rds_security_group" { + vpc_id = aws_vpc.my_vpc.id + + # Inbound rule to accept connections from EC2 security group on port 3306 + ingress { + from_port = 3306 + to_port = 3306 + protocol = "tcp" + security_groups = [aws_security_group.my_security_group.id] + } + + egress { + from_port = 3306 + to_port = 3306 + protocol = "tcp" + security_groups = [aws_security_group.my_security_group.id] + } +} \ No newline at end of file