module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations.
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-collaborator/collaborator-report-trigger/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

# Create IAM user to stage in collaborator report trigger jenkins scheduler."
resource "aws_iam_user" "collaborator-report-trigger-user" {
  name = "prod-${var.service_name}"
  tags = {
    application_family = var.application_family
    environment        = var.environment
    service_name       = var.service_name
    role               = "service-user"
    terraformed        = true
  }
}

# Then parse through the list using count
resource "aws_iam_user_policy_attachment" "iam-user-policy-attachment" {
  user       = aws_iam_user.collaborator-report-trigger-user.name
  count      = length(var.iam_policy_arn)
  policy_arn = var.iam_policy_arn[count.index]
}
