Skip to content

Commit

Permalink
Terraform changes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinxValkyria committed Feb 22, 2024
1 parent 719b3d8 commit d7dd290
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 119 deletions.
38 changes: 38 additions & 0 deletions terraform/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# terraform {
# backend "s3" {
# bucket = "project-3-bucket"
# key = "terraform.tfstate"
# region = "us-east-1"
# }
# }

resource "aws_s3_bucket" "my_bucket" {
bucket = "s3backendstate"
versioning {
enabled = true
}
# server_side_encryption_configuration {
# rule {
# apply_server_side_encryption_by_default {
# sse_algorithm = "AES256"
# }
# }
# }

lifecycle {
prevent_destroy = true
}

}

resource "aws_dynamodb_table" "statelock" {
name = "state-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"

attribute {
name = "LockID"
type = "S"
}
}
229 changes: 110 additions & 119 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,113 @@ provider "aws" {
region = "us-east-1" # Set your desired AWS region
}

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>3.27"
}
}

backend "s3" {
bucket = "project-3-bucket"
key = "terraform.tfstate"
region = "us-east-1"
}
}

resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true

tags = {
Name = "Mediaverse-tf-vpc"
}
}

data "aws_availability_zones" "available" {}

variable "availability_zone_count" {
type = number
default = 1
}

resource "aws_subnet" "public_sub" {
vpc_id = aws_vpc.main.id
count = var.availability_zone_count
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true

tags = {
Name = "mediaverse-tf-pub-sn-${count.index + 1}"
}
}


resource "aws_security_group" "web_sc_group" {
name = "ec2-security-group"
description = "ec2 security group with inbound rules"
vpc_id = aws_vpc.main.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH traffic"
}

ingress {
from_port = 80
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH traffic"
}


ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow custom TCP traffic on port 3000"
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
description = "Allow custom TCP traffic on port 3000"
}
}

resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.example.id
}
}
resource "aws_route_table_association" "rt_asc" {
subnet_id = aws_subnet.public_sub[0].id
route_table_id = aws_route_table.public_rt.id
}

resource "aws_instance" "MediaVerse_instance" {
ami = "ami-0c7217cdde317cfec" # Replace with your desired AMI ID
instance_type = "t2.micro"
subnet_id = aws_subnet.public_sub[0].id # Assuming you want the first subnet
vpc_security_group_ids = [aws_security_group.web_sc_group.id] # Corrected line key_name = "mediaverse-tf-key" # Replace with your EC2 key pair name
key_name = "mediaverse-tf-key"
# Replace with your EC2 key pair name
user_data = <<-EOF
#!/bin/bash
sudo -i
sudo apt-get update -y
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo docker run -d -p 3000:3000 zinx666/mediaverse:latest
EOF

tags = {
Name = "my-project-3-instance"
}
}

resource "aws_internet_gateway" "example" {
vpc_id = aws_vpc.main.id
}
# terraform {
# backend "s3" {
# bucket = "project-3-bucket"
# key = "terraform.tfstate"
# region = "us-east-1"
# }
# }


# resource "aws_vpc" "main" {
# cidr_block = var.vpc_cidr
# enable_dns_hostnames = true

# tags = {
# Name = "Mediaverse-tf-vpc"
# }
# }

# variable "availability_zone_count" {
# type = number
# default = 1
# }

# resource "aws_subnet" "public_sub" {
# vpc_id = aws_vpc.main.id
# count = var.availability_zone_count
# cidr_block = "10.0.0.0/24"
# availability_zone = "us-east-1a"
# map_public_ip_on_launch = true

# tags = {
# Name = "mediaverse-tf-pub-sn-${count.index + 1}"
# }
# }

# resource "aws_security_group" "web_sc_group" {
# name = "ec2-security-group"
# description = "ec2 security group with inbound rules"
# vpc_id = aws_vpc.main.id

# ingress {
# from_port = 22
# to_port = 22
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"]
# description = "Allow SSH traffic"
# }

# ingress {
# from_port = 80
# to_port = 443
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"]
# description = "Allow HTTP/HTTPS traffic"
# }

# ingress {
# from_port = 3000
# to_port = 3000
# protocol = "tcp"
# cidr_blocks = ["0.0.0.0/0"]
# description = "Allow custom TCP traffic on port 3000"
# }

# egress {
# from_port = 0
# to_port = 0
# protocol = -1
# cidr_blocks = ["0.0.0.0/0"]
# description = "Allow all outbound traffic"
# }
# }

# resource "aws_route_table" "public_rt" {
# vpc_id = aws_vpc.main.id

# route {
# cidr_block = "0.0.0.0/0"
# gateway_id = aws_internet_gateway.example.id
# }
# }

# resource "aws_route_table_association" "rt_asc" {
# subnet_id = aws_subnet.public_sub[0].id
# route_table_id = aws_route_table.public_rt.id
# }

# resource "aws_instance" "MediaVerse_instance" {
# ami = "ami-0c7217cdde317cfec" # Replace with your desired AMI ID
# instance_type = "t2.micro"
# subnet_id = aws_subnet.public_sub[0].id
# vpc_security_group_ids = [aws_security_group.web_sc_group.id]
# key_name = "mediaverse-tf-key"
# user_data = <<-EOF
# #!/bin/bash
# sudo -i
# sudo apt-get update -y
# sudo apt-get install -y docker.io
# sudo systemctl start docker
# sudo docker run -d -p 3000:3000 zinx666/mediaverse:latest
# EOF

# tags = {
# Name = "my-project-3-instance"
# }
# }

# resource "aws_internet_gateway" "example" {
# vpc_id = aws_vpc.main.id
# }
9 changes: 9 additions & 0 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# terraform {
# backend "s3" {
# bucket = "s3backendstate"
# dynamodb_table = "state-lock"
# key = "global/mystatefile/terraform.tfstate"
# region = "us-east-1"
# encrypt = true
# }
# }

0 comments on commit d7dd290

Please sign in to comment.