variable "aws_region" {
  type        = string
  default     = "us-east-1"
  description = "AWS region"
}

variable "environment" {
  type        = string
  description = "Available values: dev, qa, prod."
  default     = "prod"
}

variable "service_name" {
  type    = string
  default = "datadog-forwarder"
}

variable "team_name" {
  description = "The name of the team which owns the resources"
  default     = "devops"
}

variable "application_family" {
  type    = string
  default = "devops"
}

variable "bucket_name" {
  type        = string
  description = "Bucket name for forwarder content (e.g. function source code, tags cache)"
  default     = "supply-chain-datadog-forwarder"
}

variable "lambda_memory_size" {
  type    = number
  default = 1024
}

variable "lambda_timeout" {
  type    = number
  default = 120
}

variable "lambda_reserved_concurrent_executions" {
  type    = number
  default = 250
}

variable "lambda_environment_variables_merge" {
  type        = map(string)
  description = "Add new or update existing environment variables for the lambda function."
  default     = {}
}

variable "lambda_cloudwatch_logs_integration_enabled" {
  type        = bool
  description = "Allow triggers from cloudwatch logs."
  default     = true
}

variable "lambda_cloudwatch_events_integration_enabled" {
  type        = bool
  description = "Allow triggers from cloudwatch events."
  default     = true
}

variable "lambda_s3_integration_enabled" {
  type        = bool
  description = "Allow triggers from S3."
  default     = true
}

variable "lambda_sns_integration_enabled" {
  type        = bool
  description = "Allow triggers from SNS."
  default     = true
}

variable "tags_caching_enabled" {
  type        = bool
  description = "Use AWS resources tags caching. Requires an S3 bucket to store the cache."
  default     = true
}

variable "tags_caching_ttl_seconds" {
  type        = number
  description = "Tags caching time in seconds."
  default     = 300
}

locals {
  lambda_environment_variables = merge(
    {
      "DD_SITE"                      = "datadoghq.com"
      "DD_API_KEY_SECRET_ARN"        = "arn:aws:secretsmanager:${var.aws_region}:${data.aws_caller_identity.current.account_id}:secret:${var.environment}/datadog/DD_API_KEY"
      "DD_FETCH_LAMBDA_TAGS"         = "true"
      "DD_S3_BUCKET_NAME"            = module.datadog_forwarder_content_s3_bucket.s3_bucket_name_output
      "DD_TAGS_CACHE_TTL_SECONDS"    = var.tags_caching_ttl_seconds
      "DD_ENHANCED_METRICS"          = "false"
      "DD_USE_PRIVATE_LINK"          = "false"
      "DD_USE_VPC"                   = "false"
      "DD_FETCH_STEP_FUNCTIONS_TAGS" = "true"
    },
    var.lambda_environment_variables_merge
  )

  lambda_policy_attachments = flatten([
    aws_iam_policy.lambda_secretsmanager_override_policy.arn,
    aws_iam_policy.lambda_tags_policy[*].arn,
  ])
}
