provider "aws" {
  region = var.aws_region
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "backup-orcd-terraform-state"
    key     = "prod/orcd-secrets-manager-secrets/lambda/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=1.0.0"

  environment = var.environment
}

data "aws_iam_policy_document" "lambda_trigger_ecs_task_policy_document" {
  # checkov:skip=CKV_AWS_109:Silencing because this Lambda role needs read permissions to EC2
  statement {
    effect = "Allow"
    actions = [
      "ecs:RunTask"
    ]

    resources = [
      "arn:aws:ecs:us-east-1:${data.aws_caller_identity.current.account_id}:task-definition/backup-orcd-secrets-manager-secrets:*",
    ]
  }
  statement {
    effect = "Allow"
    actions = [
      "ec2:DescribeSubnets",
      "ec2:DescribeVpcs",
      "ec2:DescribeSecurityGroups",
      "ecs:DescribeTaskDefinition"
    ]

    resources = ["*"]
  }
  statement {
    effect = "Allow"
    actions = [
      "iam:PassRole"
    ]

    resources = [
      "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/backup-orcd-secrets-manager-secrets-task-role",
      "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/backup-orcd-secrets-manager-secrets-task-execution-role"
    ]
  }
}

resource "aws_iam_policy" "lambda_trigger_ecs_task_policy" {
  name        = "${var.environment}-${var.service_name}-trigger-ecs-task-policy"
  description = "${var.environment}-${var.service_name}-trigger-ecs-task-policy"
  policy      = data.aws_iam_policy_document.lambda_trigger_ecs_task_policy_document.json
}

module "lambda_ecs_trigger" {
  source = "git@github.com:theorchard/terraform-lambda.git?ref=3.1.4"

  environment                                    = var.environment
  lambda_name                                    = "lambda-${var.service_name}-trigger"
  application_family                             = var.application_family
  lambda_description                             = "Triggers secrets manager backup ECS task"
  use_container_image                            = true
  datadog_advanced_enabled                       = true
  vpc_enabled                                    = true
  vpc_id                                         = module.vpc_info.vpc_id
  vpc_subnet_ids                                 = module.vpc_info.default_private_subnet_ids
  lambda_function_timeout                        = 900
  lambda_function_reserved_concurrent_executions = 1
  zappa_s3_policy_enabled                        = false
  cloudwatch_event_enabled                       = true
  cloudwatch_event_schedule                      = "rate(1 day)"
  iam_managed_policy_attachments = [
    aws_iam_policy.lambda_trigger_ecs_task_policy.arn,
  ]

  lambda_function_environment_variables = {
    Environment = var.environment
    SENTRY_DSN  = module.sentry_project.sentry_key_dsn_public_output
  }
}

module "sentry_project" {
  source = "git@github.com:theorchard/terraform-sentry.git?ref=4.0.2"

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  teams              = [var.environment]
  application_family = var.application_family
}
