provider "aws" {
  region = var.aws_region
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "dev-orcd-terraform-state"
    key     = "dev/kafka-infra/rest_proxy/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "rest_proxy_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=3.10.1"

  environment        = var.environment
  service_name       = var.service_name
  aws_region         = var.aws_region
  application_family = var.application_family
  non_ecr_image      = "confluentinc/cp-kafka-rest:7.5.0"

  desired_task_count                = 1
  minimum_capacity                  = 1
  maximum_capacity                  = 1
  task_cpu                          = 1024
  task_memory                       = 2048
  container_port                    = 8082
  container_protocol_version        = "HTTP1"
  health_check_grace_period_seconds = 300
  health_check_path                 = "/brokers"
  vpc_id                            = "vpc-34dbfd51"


  environment_variables = [
    {
      Environment = var.environment
    },
    {
      KAFKA_REST_HOST_NAME = "${var.environment}-${var.service_name}"
    },
    {
      KAFKA_REST_LISTENERS = "http://0.0.0.0:8082"
    },
    {
      KAFKA_REST_BOOTSTRAP_SERVERS = "SSL://b-1.dev-managed-kafka-cdc.rk4es0.c11.kafka.us-east-1.amazonaws.com:9094,SSL://b-2.dev-managed-kafka-cdc.rk4es0.c11.kafka.us-east-1.amazonaws.com:9094,SSL://b-3.dev-managed-kafka-cdc.rk4es0.c11.kafka.us-east-1.amazonaws.com:9094"
    },
    {
      KAFKA_REST_CLIENT_SECURITY_PROTOCOL = "SSL"
    },
    {
      KAFKA_REST_SCHEMA_REGISTRY_URL = "https://dev-schema-registry.dev.theorchard.io"
    }
  ]

}

# This rule will allow kafka-connect tasks to communicate with each other in distributed mode
resource "aws_security_group_rule" "allow_intra_cluster_communication" {
  from_port         = 8088
  to_port           = 8088
  protocol          = "tcp"
  security_group_id = module.rest_proxy_fargate_environment.fargate_security_group_id
  self              = true
  type              = "ingress"
}
