From b179e92bb586bcdaee5c9f0791eba2e9506952da Mon Sep 17 00:00:00 2001 From: Souvik Date: Fri, 19 Sep 2025 01:12:30 +0530 Subject: [PATCH] Added GCP and EC2_AMI --- terraform/AMI_EC2/ec2.tf | 92 ++++++++++++++++++++++++++++++ terraform/AMI_EC2/output.tf | 21 +++++++ terraform/AMI_EC2/sg.tf | 27 +++++++++ terraform/AMI_EC2/terraform.tfvars | 8 +++ terraform/AMI_EC2/variables.tf | 29 ++++++++++ terraform/EC2/ec2.tf | 2 +- terraform/GCP/install_tooljet.sh | 12 ++++ terraform/GCP/instance.tf | 85 +++++++++++++++++++++++++++ terraform/GCP/outputs.tf | 25 ++++++++ terraform/GCP/terraform.tfvars | 5 ++ terraform/GCP/variables.tf | 34 +++++++++++ 11 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 terraform/AMI_EC2/ec2.tf create mode 100644 terraform/AMI_EC2/output.tf create mode 100644 terraform/AMI_EC2/sg.tf create mode 100644 terraform/AMI_EC2/terraform.tfvars create mode 100644 terraform/AMI_EC2/variables.tf create mode 100644 terraform/GCP/install_tooljet.sh create mode 100644 terraform/GCP/instance.tf create mode 100644 terraform/GCP/outputs.tf create mode 100644 terraform/GCP/terraform.tfvars create mode 100644 terraform/GCP/variables.tf diff --git a/terraform/AMI_EC2/ec2.tf b/terraform/AMI_EC2/ec2.tf new file mode 100644 index 0000000000..20129494e3 --- /dev/null +++ b/terraform/AMI_EC2/ec2.tf @@ -0,0 +1,92 @@ +# Define provider +provider "aws" { + region = var.region +} + +# Generate a TLS private key for EC2 access +resource "tls_private_key" "tooljet_key" { + algorithm = "RSA" + rsa_bits = 2048 +} + +# Define the key pair for EC2 access +resource "aws_key_pair" "tooljet_key" { + key_name = "tooljet-key" + public_key = tls_private_key.tooljet_key.public_key_openssh # file("~/.ssh/tooljet.pub") +} + +# Create a VPC +resource "aws_vpc" "tooljet_vpc" { + cidr_block = "10.0.0.0/16" + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + Name = "TooljetVPC" + } +} + +# Create an Internet Gateway for the VPC +resource "aws_internet_gateway" "tooljet_igw" { + vpc_id = aws_vpc.tooljet_vpc.id + + tags = { + Name = "TooljetInternetGateway" + } +} + +# Create a public subnet +resource "aws_subnet" "tooljet_public_subnet" { + vpc_id = aws_vpc.tooljet_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = var.availability_zone + map_public_ip_on_launch = true + + tags = { + Name = "TooljetPublicSubnet" + } +} + +# Create a route table for the public subnet +resource "aws_route_table" "tooljet_public_route_table" { + vpc_id = aws_vpc.tooljet_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.tooljet_igw.id + } + + tags = { + Name = "TooljetPublicRouteTable" + } +} + +# Associate the public route table with the public subnet +resource "aws_route_table_association" "tooljet_public_subnet_assoc" { + subnet_id = aws_subnet.tooljet_public_subnet.id + route_table_id = aws_route_table.tooljet_public_route_table.id +} + +# Define the EC2 instance using ToolJet AMI +resource "aws_instance" "tooljet_instance" { + ami = var.tooljet_ami_id + instance_type = var.instance_type + key_name = aws_key_pair.tooljet_key.key_name + subnet_id = aws_subnet.tooljet_public_subnet.id + vpc_security_group_ids = [aws_security_group.tooljet_sg.id] + associate_public_ip_address = true + availability_zone = var.availability_zone + + # Root EBS volume configuration + root_block_device { + volume_size = 20 + volume_type = "gp3" + } + + tags = { + Name = "TooljetAppServer" + } + + depends_on = [aws_security_group.tooljet_sg] +} + diff --git a/terraform/AMI_EC2/output.tf b/terraform/AMI_EC2/output.tf new file mode 100644 index 0000000000..99a6677c3a --- /dev/null +++ b/terraform/AMI_EC2/output.tf @@ -0,0 +1,21 @@ +# Outputs +output "tooljet_private_key" { + value = tls_private_key.tooljet_key.private_key_pem + sensitive = true +} + +output "instance_ip" { + value = aws_instance.tooljet_instance.public_ip +} + +output "instance_id" { + value = aws_instance.tooljet_instance.id +} + +output "ami_id" { + value = var.tooljet_ami_id +} + +output "ami_description" { + value = "Using ToolJet AMI ID: ${var.tooljet_ami_id}" +} \ No newline at end of file diff --git a/terraform/AMI_EC2/sg.tf b/terraform/AMI_EC2/sg.tf new file mode 100644 index 0000000000..807ffac1d1 --- /dev/null +++ b/terraform/AMI_EC2/sg.tf @@ -0,0 +1,27 @@ +# Define the security group +resource "aws_security_group" "tooljet_sg" { + vpc_id = aws_vpc.tooljet_vpc.id + name = "tooljet-sg" + description = "Allow SSH, HTTP, HTTPS and ToolJet ports" + + dynamic "ingress" { + for_each = var.ingress_ports + content { + from_port = ingress.value + to_port = ingress.value + 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 = "TooljetSecurityGroup" + } +} \ No newline at end of file diff --git a/terraform/AMI_EC2/terraform.tfvars b/terraform/AMI_EC2/terraform.tfvars new file mode 100644 index 0000000000..bcfe0c8b4e --- /dev/null +++ b/terraform/AMI_EC2/terraform.tfvars @@ -0,0 +1,8 @@ +region = "" +availability_zone = "" +instance_type = "" +tooljet_ami_id = "" +ingress_ports = [22, 80, 443, 3000] + +# terraform output -raw tooljet_private_key_pem > tooljet-key.pem +# chmod 600 tooljet-key.pem diff --git a/terraform/AMI_EC2/variables.tf b/terraform/AMI_EC2/variables.tf new file mode 100644 index 0000000000..e56f36298f --- /dev/null +++ b/terraform/AMI_EC2/variables.tf @@ -0,0 +1,29 @@ + +# Variables +variable "region" { + description = "AWS region" + type = string + default = "us-west-2" +} + +variable "availability_zone" { + description = "Availability zone for the subnet and instance" + type = string + default = "us-west-2a" +} + +variable "instance_type" { + description = "EC2 instance type" + type = string + default = "t3.medium" # Recommended for ToolJet +} + +variable "tooljet_ami_id" { + description = "ToolJet AMI ID - contact ToolJet team for the specific AMI ID in your region" + type = string + +} + +variable "ingress_ports" { + default = [22, 80, 443, 3000] +} diff --git a/terraform/EC2/ec2.tf b/terraform/EC2/ec2.tf index 840eeb3656..d7aa8fb857 100644 --- a/terraform/EC2/ec2.tf +++ b/terraform/EC2/ec2.tf @@ -79,7 +79,7 @@ resource "aws_instance" "tooljet_instance" { depends_on = [aws_security_group.tooljet_sg] # Root EBS volume configuration root_block_device { - volume_size = 16 + volume_size = 20 volume_type = "gp3" } # Load the shell script using file() function diff --git a/terraform/GCP/install_tooljet.sh b/terraform/GCP/install_tooljet.sh new file mode 100644 index 0000000000..e73597ac27 --- /dev/null +++ b/terraform/GCP/install_tooljet.sh @@ -0,0 +1,12 @@ +#!/bin/bash +sudo apt upgrade -y +sudo apt update -y +sudo apt install -y apt-transport-https ca-certificates curl software-properties-common + +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - +sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" +sudo apt update -y + +sudo apt install -y docker-ce +sudo systemctl start docker +sudo systemctl enable docker diff --git a/terraform/GCP/instance.tf b/terraform/GCP/instance.tf new file mode 100644 index 0000000000..9811138a43 --- /dev/null +++ b/terraform/GCP/instance.tf @@ -0,0 +1,85 @@ +# Define provider +provider "google" { + project = var.project_id + region = var.region + zone = var.zone +} + +# Generate a TLS private key for SSH access +resource "tls_private_key" "tooljet_key" { + algorithm = "RSA" + rsa_bits = 2048 +} + +# Create VPC network +resource "google_compute_network" "tooljet_vpc" { + name = "tooljet-vpc" + auto_create_subnetworks = false + description = "VPC network for Tooljet application" +} + +# Create subnet +resource "google_compute_subnetwork" "tooljet_subnet" { + name = "tooljet-subnet" + ip_cidr_range = "10.0.1.0/24" + region = var.region + network = google_compute_network.tooljet_vpc.id +} + +# Create firewall rules +resource "google_compute_firewall" "tooljet_firewall" { + name = "tooljet-firewall" + network = google_compute_network.tooljet_vpc.name + + allow { + protocol = "tcp" + ports = var.firewall_ports + } + + source_ranges = ["0.0.0.0/0"] + target_tags = ["tooljet-server"] +} + +# Get the latest Ubuntu image +data "google_compute_image" "ubuntu" { + family = "ubuntu-2404-lts-amd64" + project = "ubuntu-os-cloud" +} + +# Create the compute instance +resource "google_compute_instance" "tooljet_instance" { + name = "tooljet-instance" + machine_type = var.machine_type + zone = var.zone + + tags = ["tooljet-server"] + + boot_disk { + initialize_params { + image = data.google_compute_image.ubuntu.self_link + size = 20 + type = "pd-standard" + } + } + + network_interface { + network = google_compute_network.tooljet_vpc.id + subnetwork = google_compute_subnetwork.tooljet_subnet.id + + access_config { + // Ephemeral public IP + } + } + + metadata = { + ssh-keys = "${var.ssh_username}:${tls_private_key.tooljet_key.public_key_openssh}" + } + + metadata_startup_script = file("${path.module}/install_tooljet.sh") + + service_account { + scopes = ["cloud-platform"] + } + + depends_on = [google_compute_firewall.tooljet_firewall] +} \ No newline at end of file diff --git a/terraform/GCP/outputs.tf b/terraform/GCP/outputs.tf new file mode 100644 index 0000000000..9a827e8b3a --- /dev/null +++ b/terraform/GCP/outputs.tf @@ -0,0 +1,25 @@ +output "tooljet_private_key" { + description = "The private SSH key for accessing the instance" + value = tls_private_key.tooljet_key.private_key_pem + sensitive = true +} + +output "instance_ip" { + description = "The external IP address of the instance" + value = google_compute_instance.tooljet_instance.network_interface[0].access_config[0].nat_ip +} + +output "instance_id" { + description = "The ID of the compute instance" + value = google_compute_instance.tooljet_instance.id +} + +output "instance_name" { + description = "The name of the compute instance" + value = google_compute_instance.tooljet_instance.name +} + +output "ssh_command" { + description = "SSH command to connect to the instance" + value = "ssh -i private_key.pem ${var.ssh_username}@${google_compute_instance.tooljet_instance.network_interface[0].access_config[0].nat_ip}" +} \ No newline at end of file diff --git a/terraform/GCP/terraform.tfvars b/terraform/GCP/terraform.tfvars new file mode 100644 index 0000000000..67ed7e19f5 --- /dev/null +++ b/terraform/GCP/terraform.tfvars @@ -0,0 +1,5 @@ +project_id = "" +region = "us-central1" +zone = "us-central1-a" +machine_type = "e2-medium" +ssh_username = "ubuntu" \ No newline at end of file diff --git a/terraform/GCP/variables.tf b/terraform/GCP/variables.tf new file mode 100644 index 0000000000..a8e42f79d9 --- /dev/null +++ b/terraform/GCP/variables.tf @@ -0,0 +1,34 @@ +variable "project_id" { + description = "The GCP project ID" + type = string +} + +variable "region" { + description = "The GCP region" + type = string + default = "us-central1" +} + +variable "zone" { + description = "The GCP zone" + type = string + default = "us-central1-a" +} + +variable "machine_type" { + description = "The machine type for the compute instance" + type = string + default = "e2-medium" +} + +variable "firewall_ports" { + description = "List of ports for firewall ingress" + type = list(string) + default = ["22", "80", "443", "3000"] +} + +variable "ssh_username" { + description = "Username for SSH access" + type = string + default = "ubuntu" +} \ No newline at end of file