Skip to content

Commit

Permalink
add terraform script for staging env
Browse files Browse the repository at this point in the history
  • Loading branch information
ER-Radi committed Feb 23, 2024
1 parent a942ece commit 34d803c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
93 changes: 93 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//provider "aws" {
// region = var.aws_region
// access_key = var.access_key
// secret_key = var.secret_key
//}

# Create Security Group to allow port 80, 443
resource "aws_security_group" "allow_web" {
name = "plm-staging-sg-br-X"
description = "Allow Web inbound traffic"
vpc_id = var.vpc_id
ingress {
description = "HTTPS"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
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 = "plm-staging-sg-br-X"
}
}


# Create a network interface with an ip in the subnet
resource "aws_network_interface" "web-server-nic" {
subnet_id = var.subnet_id
security_groups = [aws_security_group.allow_web.id]
tags = {
Name = "plm-staging-ni-br-X"
}
}


# Assign an Elastic IP to the network interface created
resource "aws_eip" "elastic-ip" {
domain = "vpc"
network_interface = aws_network_interface.web-server-nic.id
associate_with_private_ip = aws_network_interface.web-server-nic.private_ip
tags = {
Name = "plm-staging-eip-br-X"
}
}

output "server_public_ip" {
value = aws_eip.elastic-ip.public_ip
}


# Create an EC2 instance and install Docker
resource "aws_instance" "ec2-staging" {
ami = var.ami_id
instance_type = var.instance_type
availability_zone = var.ec2_availability_zone

network_interface {
device_index = 0
network_interface_id = aws_network_interface.web-server-nic.id
}

user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
EOF

tags = {
Name = "plm-staging-instance-branch-X"
}

}
8 changes: 8 additions & 0 deletions terraform/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//aws_region = "eu-west-3"
//access_key = ${{ secrets.AWS_TF_ACCESS_KEY_ID }}
//secret_key = ${{ secrets.AWS_TF_SECRET_ACCESS_KEY }}
vpc_id = "vpc-d2d538ba"
subnet_id = "subnet-d5a1d598"
ami_id = "ami-072056ff9d3689e7b"
instance_type = "t2.micro"
ec2_availability_zone = "eu-west-3c"
41 changes: 41 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
variable "aws_region" {
description = "AWS Region"
type = string
}
variable "access_key" {
description = "AWS Access Key"
type = string
}
variable "secret_key" {
description = "AWS Secret Key"
type = string
}
*/

variable "vpc_id" {
description = "VPC ID"
type = string
}

variable "subnet_id" {
description = "Subnet ID"
type = string
}

variable "ami_id" {
description = "ID de l'AMI EC2"
type = string
}

variable "instance_type" {
description = "Type d'instance EC2"
type = string
}

variable "ec2_availability_zone" {
description = "EC2 Availability Zone"
type = string
}

0 comments on commit 34d803c

Please sign in to comment.