diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 000000000..133480260 --- /dev/null +++ b/terraform/main.tf @@ -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" + } + +} diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars new file mode 100644 index 000000000..2e75fe860 --- /dev/null +++ b/terraform/terraform.tfvars @@ -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" diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 000000000..a009ba853 --- /dev/null +++ b/terraform/variables.tf @@ -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 +}