From 32e4d470ee0f1e8d45de69980e7af3aa848bbae4 Mon Sep 17 00:00:00 2001 From: Arnav <48650781+arnavrneo@users.noreply.github.com> Date: Fri, 19 Apr 2024 23:22:24 +0530 Subject: [PATCH 1/4] tf-init --- url-shortener/tf/placeholder | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 url-shortener/tf/placeholder diff --git a/url-shortener/tf/placeholder b/url-shortener/tf/placeholder new file mode 100644 index 0000000..e69de29 From 9ba0d70ede827cc2bee2c9bf0f79d9931cdab5af Mon Sep 17 00:00:00 2001 From: Arnav <48650781+arnavrneo@users.noreply.github.com> Date: Sun, 21 Apr 2024 19:28:52 +0530 Subject: [PATCH 2/4] tf-integration --- .gitignore | 3 + url-shortener/tf/.terraform.lock.hcl | 24 +++++++ url-shortener/tf/main.tf | 59 +++++++++++++++++ url-shortener/tf/placeholder | 0 url-shortener/tf/services/.terraform.lock.hcl | 24 +++++++ url-shortener/tf/services/main.tf | 66 +++++++++++++++++++ 6 files changed, 176 insertions(+) create mode 100644 url-shortener/tf/.terraform.lock.hcl create mode 100644 url-shortener/tf/main.tf delete mode 100644 url-shortener/tf/placeholder create mode 100644 url-shortener/tf/services/.terraform.lock.hcl create mode 100644 url-shortener/tf/services/main.tf diff --git a/.gitignore b/.gitignore index 785ba7c..14f3887 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ *.next *node_modules *package-lock.json +*.terraform +*.tfstate +*.tfstate.backup diff --git a/url-shortener/tf/.terraform.lock.hcl b/url-shortener/tf/.terraform.lock.hcl new file mode 100644 index 0000000..cc735eb --- /dev/null +++ b/url-shortener/tf/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.46.0" + hashes = [ + "h1:GK1y1qAGcPHPZxz01Ko6v+815T7kZPXt6POBsLg9c/k=", + "zh:05ae6180a7f23071435f6e5e59c19af0b6c5da42ee600c6c1568c8660214d548", + "zh:0d878d1565d5e57ce6b34ec5f04b28662044a50c999ec5770c374aa1f1020de2", + "zh:25ef1467af2514d8011c44759307445f7057836ff87dfe4503c3e1c9776d5c1a", + "zh:26c006df6200f0063b827aab05bec94f9f3f77848e82ed72e48a51d1170d1961", + "zh:37cdf4292649a10f12858622826925e18ad4eca354c31f61d02c66895eb91274", + "zh:4315b0433c2fc512666c74e989e2d95240934ef370bea1c690d36cb02d30c4ce", + "zh:75df0b3f631b78aeff1832cc77d99b527c2a5e79d40f7aac40bdc4a66124dac2", + "zh:90693d936c9a556d2bf945de4920ff82052002eb73139bd7164fafd02920f0ef", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:c9177ad09804c60fd2ed25950570407b6bdcdf0fcc309e1673b584f06a827fae", + "zh:ca8e8db24a4d62d92afd8d3d383b81a08693acac191a2e0a110fb46deeff56a3", + "zh:d5fa3a36e13957d63bfe9bbd6df0426a2422214403aac9f20b60c36f8d9ebec6", + "zh:e4ede44a112296c9cc77b15e439e41ee15c0e8b3a0dec94ae34df5ebba840e8b", + "zh:f2d4de8d8cde69caffede1544ebea74e69fcc4552e1b79ae053519a05c060706", + "zh:fc19e9266b1841d4a3aeefa8a5b5ad6988baed6540f85a373b6c2d0dc1ca5830", + ] +} diff --git a/url-shortener/tf/main.tf b/url-shortener/tf/main.tf new file mode 100644 index 0000000..017944b --- /dev/null +++ b/url-shortener/tf/main.tf @@ -0,0 +1,59 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "us-east-1" +} + +resource "aws_instance" "myinstance" { + ami = "ami-080e1f13689e07408" + instance_type = "t2.micro" + vpc_security_group_ids = [aws_security_group.instance.id] + + user_data = <<-EOF + #!/bin/bash + echo "Hello, Arnav!" > index.html + nohup busybox httpd -f -p 8080 & + EOF + + user_data_replace_on_change = true + + tags = { + Name = "terraform-ex" + } +} + +resource "aws_security_group" "instance" { + name = "busybox-sg" + + ingress { + from_port = 8080 + to_port = 8080 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } +} + +output "instance_public_ip" { + description = "Public Ip of instance" + value = aws_instance.myinstance.public_ip +} + +data "aws_caller_identity" "current" {} + +output "account_id" { + value = "${data.aws_caller_identity.current.account_id}" +} + +output "caller_arn" { + value = "${data.aws_caller_identity.current.arn}" +} + +output "caller_user" { + value = "${data.aws_caller_identity.current.user_id}" +} diff --git a/url-shortener/tf/placeholder b/url-shortener/tf/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/url-shortener/tf/services/.terraform.lock.hcl b/url-shortener/tf/services/.terraform.lock.hcl new file mode 100644 index 0000000..cc735eb --- /dev/null +++ b/url-shortener/tf/services/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.46.0" + hashes = [ + "h1:GK1y1qAGcPHPZxz01Ko6v+815T7kZPXt6POBsLg9c/k=", + "zh:05ae6180a7f23071435f6e5e59c19af0b6c5da42ee600c6c1568c8660214d548", + "zh:0d878d1565d5e57ce6b34ec5f04b28662044a50c999ec5770c374aa1f1020de2", + "zh:25ef1467af2514d8011c44759307445f7057836ff87dfe4503c3e1c9776d5c1a", + "zh:26c006df6200f0063b827aab05bec94f9f3f77848e82ed72e48a51d1170d1961", + "zh:37cdf4292649a10f12858622826925e18ad4eca354c31f61d02c66895eb91274", + "zh:4315b0433c2fc512666c74e989e2d95240934ef370bea1c690d36cb02d30c4ce", + "zh:75df0b3f631b78aeff1832cc77d99b527c2a5e79d40f7aac40bdc4a66124dac2", + "zh:90693d936c9a556d2bf945de4920ff82052002eb73139bd7164fafd02920f0ef", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:c9177ad09804c60fd2ed25950570407b6bdcdf0fcc309e1673b584f06a827fae", + "zh:ca8e8db24a4d62d92afd8d3d383b81a08693acac191a2e0a110fb46deeff56a3", + "zh:d5fa3a36e13957d63bfe9bbd6df0426a2422214403aac9f20b60c36f8d9ebec6", + "zh:e4ede44a112296c9cc77b15e439e41ee15c0e8b3a0dec94ae34df5ebba840e8b", + "zh:f2d4de8d8cde69caffede1544ebea74e69fcc4552e1b79ae053519a05c060706", + "zh:fc19e9266b1841d4a3aeefa8a5b5ad6988baed6540f85a373b6c2d0dc1ca5830", + ] +} diff --git a/url-shortener/tf/services/main.tf b/url-shortener/tf/services/main.tf new file mode 100644 index 0000000..2d17428 --- /dev/null +++ b/url-shortener/tf/services/main.tf @@ -0,0 +1,66 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "us-east-1" + profile = "arnav" # enter relevant aws profile +} + +# security group creation +resource "aws_security_group" "urlSG" { + name = "urlshortener-sg" + + ingress { + cidr_blocks = ["0.0.0.0/0"] + from_port = 22 + to_port = 22 + protocol = "tcp" + } + + ingress { + cidr_blocks = ["0.0.0.0/0"] + from_port = 3000 + to_port = 3000 + protocol = "tcp" + } + + tags = { + Name = "proj-sg" + } +} + +# ec2 instance creation +resource "aws_instance" "urlShortener" { + ami = "ami-080e1f13689e07408" + instance_type = "t2.micro" + vpc_security_group_ids = [aws_security_group.urlSG.id] +} + +resource "aws_ec2_tag" "ec2_tag" { + key = "Ansible" + resource_id = aws_instance.urlShortener.id + value = "urlShortenerServer" +} + + +data "aws_caller_identity" "current" {} + +output "caller_user" { + description = "AWS Calling user: " + value = data.aws_caller_identity.current.user_id +} + +output "public_ip" { + description = "Public IP: " + value = aws_instance.urlShortener.public_ip +} + +output "public_dns" { + description = "Public DNS: " + value = aws_instance.urlShortener.public_dns +} \ No newline at end of file From 5fc41705aa65b0478edc47b4e8d3e850aa8d7ab4 Mon Sep 17 00:00:00 2001 From: Arnav <48650781+arnavrneo@users.noreply.github.com> Date: Sun, 21 Apr 2024 19:29:50 +0530 Subject: [PATCH 3/4] tf-update --- url-shortener/tf/main.tf | 59 ---------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 url-shortener/tf/main.tf diff --git a/url-shortener/tf/main.tf b/url-shortener/tf/main.tf deleted file mode 100644 index 017944b..0000000 --- a/url-shortener/tf/main.tf +++ /dev/null @@ -1,59 +0,0 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - } - } -} - -provider "aws" { - region = "us-east-1" -} - -resource "aws_instance" "myinstance" { - ami = "ami-080e1f13689e07408" - instance_type = "t2.micro" - vpc_security_group_ids = [aws_security_group.instance.id] - - user_data = <<-EOF - #!/bin/bash - echo "Hello, Arnav!" > index.html - nohup busybox httpd -f -p 8080 & - EOF - - user_data_replace_on_change = true - - tags = { - Name = "terraform-ex" - } -} - -resource "aws_security_group" "instance" { - name = "busybox-sg" - - ingress { - from_port = 8080 - to_port = 8080 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } -} - -output "instance_public_ip" { - description = "Public Ip of instance" - value = aws_instance.myinstance.public_ip -} - -data "aws_caller_identity" "current" {} - -output "account_id" { - value = "${data.aws_caller_identity.current.account_id}" -} - -output "caller_arn" { - value = "${data.aws_caller_identity.current.arn}" -} - -output "caller_user" { - value = "${data.aws_caller_identity.current.user_id}" -} From 19ac65a38292577fa94180f228d42b3fca58430b Mon Sep 17 00:00:00 2001 From: Arnav <48650781+arnavrneo@users.noreply.github.com> Date: Sun, 21 Apr 2024 22:04:18 +0530 Subject: [PATCH 4/4] project-wrapup --- README.md | 8 ++++---- url-shortener/playbook/README.md | 4 ++++ url-shortener/tf/.terraform.lock.hcl | 24 ------------------------ 3 files changed, 8 insertions(+), 28 deletions(-) delete mode 100644 url-shortener/tf/.terraform.lock.hcl diff --git a/README.md b/README.md index ba6d07c..f2cacd7 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ This project uses: - `MongoDB` as non-relational db for authentication - `Redis` as in-memory key-value db for url keys management -- For scalability and portability, both the frontend and backend are containerized using Docker and can be spun-up easily using docker-compose. -- Ansible playbooks and roles are used for automating the service setup & infrastructure creation of single (or multiple) EC2 on AWS. They are used for: - - Creation of a security group (with relevant properties) for EC2 instances - - Creation of EC2 instances & attaching of the defined security group +- For scalability and portability, both the frontend and backend are containerized using Docker and can be spun-up easily using docker-compose and can be orchestrated using the Kubernetes (the manifest files have been provided). +- Terraform is used for Infrastructure creation and Ansible playbooks and roles are used for configuring EC2 instances created on AWS providing: - Installation of Docker on those instances - Running of the url-shortener service through user input + + diff --git a/url-shortener/playbook/README.md b/url-shortener/playbook/README.md index 61a05ba..5fa567c 100644 --- a/url-shortener/playbook/README.md +++ b/url-shortener/playbook/README.md @@ -11,6 +11,10 @@ Creates a security group & an ubuntu-ami backed EC2 instance using the specified Updates the machine as well as installs the docker required in the next step for running the application. + +> [!IMPORTANT] +> The above two playbooks are now replaced by the `terraform`. Either run the above playbooks or use the provided terraform code for infrastructure setup. + ### `3_docker_book.yaml` This provides functionalities for setting up the application files, running the application in the docker as well as the ability to remove the containers on the remote servers. diff --git a/url-shortener/tf/.terraform.lock.hcl b/url-shortener/tf/.terraform.lock.hcl deleted file mode 100644 index cc735eb..0000000 --- a/url-shortener/tf/.terraform.lock.hcl +++ /dev/null @@ -1,24 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/hashicorp/aws" { - version = "5.46.0" - hashes = [ - "h1:GK1y1qAGcPHPZxz01Ko6v+815T7kZPXt6POBsLg9c/k=", - "zh:05ae6180a7f23071435f6e5e59c19af0b6c5da42ee600c6c1568c8660214d548", - "zh:0d878d1565d5e57ce6b34ec5f04b28662044a50c999ec5770c374aa1f1020de2", - "zh:25ef1467af2514d8011c44759307445f7057836ff87dfe4503c3e1c9776d5c1a", - "zh:26c006df6200f0063b827aab05bec94f9f3f77848e82ed72e48a51d1170d1961", - "zh:37cdf4292649a10f12858622826925e18ad4eca354c31f61d02c66895eb91274", - "zh:4315b0433c2fc512666c74e989e2d95240934ef370bea1c690d36cb02d30c4ce", - "zh:75df0b3f631b78aeff1832cc77d99b527c2a5e79d40f7aac40bdc4a66124dac2", - "zh:90693d936c9a556d2bf945de4920ff82052002eb73139bd7164fafd02920f0ef", - "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", - "zh:c9177ad09804c60fd2ed25950570407b6bdcdf0fcc309e1673b584f06a827fae", - "zh:ca8e8db24a4d62d92afd8d3d383b81a08693acac191a2e0a110fb46deeff56a3", - "zh:d5fa3a36e13957d63bfe9bbd6df0426a2422214403aac9f20b60c36f8d9ebec6", - "zh:e4ede44a112296c9cc77b15e439e41ee15c0e8b3a0dec94ae34df5ebba840e8b", - "zh:f2d4de8d8cde69caffede1544ebea74e69fcc4552e1b79ae053519a05c060706", - "zh:fc19e9266b1841d4a3aeefa8a5b5ad6988baed6540f85a373b6c2d0dc1ca5830", - ] -}