# Default region
provider "aws" {
  region = "us-west-2"
}

variable "stage" {
    type = "string"
    default = "dev"
}

variable "name" {
    type = "string"
    default = "cloud"
}

# AMIs
variable "api_ami" {
    type = "string"
    default = "ami-097813820f7fc0809" # accounts_template_feb_2022
}

variable "admin_api_ami" {
    type = "string"
    default = "ami-097813820f7fc0809" # accounts_template_feb_2022
}

variable "cloud_worker_ami" {
    type = "string"
    default = "ami-08887d177482e04ae" # cloud_workerTemplateDec2021
}

variable "cloud_api_ami" {
    type = "string"
    default = "ami-0f19418a7e84de431" # cloud_httpTemplateJan2022
}

variable "cloud_websocket_ami" {
    type = "string"
    default = "ami-03131eec1ff8c057b" # cloud_websocketTemplateDec2021
}

variable "webapp_arda_ami" {
    type = "string"
    default = "ami-0e6b20177a0cd23e1" # webapp_ardaTemplateDec2021
}

variable "webapp_fabricator_ami" {
    type = "string"
    default = "ami-0e6b20177a0cd23e1" # webapp_ardaTemplateDec2021
}

variable "webapp_rohan_ami" {
    type = "string"
    default = "ami-0e6b20177a0cd23e1" # webapp_ardaTemplateDec2021
}

variable "website_ami" {
    type = "string"
    default = "ami-0352c4afd017c9693" # WebsiteTemplateFeb2023
}

# Instance types
variable "api_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "admin_api_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "cloud_worker_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "cloud_api_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "cloud_websocket_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "webapp_arda_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "webapp_fabricator_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "webapp_rohan_instance_type" {
    type = "string"
    default = "t2.micro"
}

variable "website_instance_type" {
    type = "string"
    default = "t2.micro"
}

# Key names - private key file deployed to the server.
variable "api_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "admin_api_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "cloud_worker_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "cloud_api_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "cloud_websocket_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "webapp_arda_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "webapp_fabricator_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "webapp_rohan_key_name" {
    type = "string"
    default = "dev_cloud"
}

variable "website_key_name" {
    type = "string"
    default = "dev_cloud"
}

# Certificate ARNs
variable "api_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "admin_api_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "cloud_api_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "cloud_websocket_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "webapp_arda_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "webapp_fabricator_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "webapp_rohan_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "website_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

variable "website_preview_elb_certificate_arn" {
    type = "string"
    default = "arn:aws:acm:us-west-2:164780942622:certificate/61f977f7-27ff-4538-861c-dfb6535a8f80"
}

# Placement group
resource "aws_placement_group" "cloud_placement_group" {
  name = "${var.stage}_placement_group"
  strategy = "cluster"
}

module "api" {
    source = "./api"
    stage = "${var.stage}"
    ami = "${var.api_ami}"
    key_name = "${var.api_key_name}"
    instance_type = "${var.api_instance_type}"
    elb_certificate_arn = "${var.api_elb_certificate_arn}"
}

module "admin_api" {
    source = "./admin_api"
    stage = "${var.stage}"
    ami = "${var.admin_api_ami}"
    key_name = "${var.admin_api_key_name}"
    instance_type = "${var.admin_api_instance_type}"
    elb_certificate_arn = "${var.admin_api_elb_certificate_arn}"
}

module "cloud_worker" {
    source = "./cloud_worker"
    stage = "${var.stage}"
    name = "cloud_worker"
    ami = "${var.cloud_worker_ami}"
    key_name = "${var.cloud_worker_key_name}"
    instance_type = "${var.cloud_worker_instance_type}"
}

module "cloud_api" {
    source = "./cloud_api"
    stage = "${var.stage}"
    name = "cloud_api"
    ami = "${var.cloud_api_ami}"
    key_name = "${var.cloud_api_key_name}"
    instance_type = "${var.cloud_api_instance_type}"
    elb_certificate_arn = "${var.cloud_api_elb_certificate_arn}"
}

module "cloud_websocket" {
    source = "./cloud_websocket"
    stage = "${var.stage}"
    name = "cloud_websocket"
    ami = "${var.cloud_websocket_ami}"
    key_name = "${var.cloud_websocket_key_name}"
    instance_type = "${var.cloud_websocket_instance_type}"
    cloud_websocket_elb_certificate_arn = "${var.cloud_websocket_elb_certificate_arn}"
}

module "website" {
    source = "./website"
    stage = "${var.stage}"
    ami = "${var.website_ami}"
    key_name = "${var.website_key_name}"
    instance_type = "${var.website_instance_type}"
    elb_certificate_arn = "${var.website_elb_certificate_arn}"
    preview_elb_certificate_arn = "${var.website_preview_elb_certificate_arn}"
}

module "webapp_arda" {
    source = "./webapp_arda"
    stage = "${var.stage}"
    ami = "${var.webapp_arda_ami}"
    key_name = "${var.webapp_arda_key_name}"
    instance_type = "${var.webapp_arda_instance_type}"
    elb_certificate_arn = "${var.webapp_arda_elb_certificate_arn}"
}

module "webapp_fabricator" {
    source = "./webapp_fabricator"
    stage = "${var.stage}"
    ami = "${var.webapp_fabricator_ami}"
    key_name = "${var.webapp_fabricator_key_name}"
    instance_type = "${var.webapp_fabricator_instance_type}"
    elb_certificate_arn = "${var.webapp_fabricator_elb_certificate_arn}"
}

module "webapp_rohan" {
    source = "./webapp_rohan"
    stage = "${var.stage}"
    ami = "${var.webapp_rohan_ami}"
    key_name = "${var.webapp_rohan_key_name}"
    instance_type = "${var.webapp_rohan_instance_type}"
    elb_certificate_arn = "${var.webapp_rohan_elb_certificate_arn}"
}

output "api" {
    value = module.api
}

output "admin_api" {
    value = module.admin_api
}

output "cloud_worker" {
    value = module.cloud_worker
}

output "cloud_api" {
    value = module.cloud_api
}

output "cloud_websocket" {
    value = module.cloud_websocket
}

output "website" {
    value = module.website
}

output "webapp_arda" {
    value = module.webapp_arda
}

output "webapp_fabricator" {
    value = module.webapp_fabricator
}

output "webapp_rohan" {
    value = module.webapp_rohan
}
