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

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "shared-orcd-terraform-state"
    key     = "shared/aws-account-ids/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

locals {
  accounts      = jsondecode(file("${path.module}/accounts.json"))
  accounts_json = file("${path.module}/accounts.json")

  common_tags = {
    account_group = var.account_group
    environment   = var.environment
    terraformed   = true
  }
}

provider "aws" {
  region = var.default_region

  default_tags {
    tags = module.default_tags.tags
  }
}

module "default_region_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals
}


provider "aws" {
  region = "ca-central-1"
  alias  = "ca_central_1"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "ca_central_1_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.ca_central_1
  }
}

provider "aws" {
  region = "eu-central-1"
  alias  = "eu_central_1"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "eu_central_1_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.eu_central_1
  }
}

provider "aws" {
  region = "eu-west-1"
  alias  = "eu_west_1"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "eu_west_1_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.eu_west_1
  }
}

provider "aws" {
  region = "sa-east-1"
  alias  = "sa_east_1"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "sa_east_1_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.sa_east_1
  }
}

provider "aws" {
  region = "us-east-2"
  alias  = "us_east_2"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "us_east_2_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.us_east_2
  }
}

provider "aws" {
  region = "us-west-1"
  alias  = "us_west_1"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "us_west_1_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.us_west_1
  }
}

provider "aws" {
  region = "us-west-2"
  alias  = "us_west_2"

  default_tags {
    tags = module.default_tags.tags
  }
}

module "us_west_2_ssm" {
  source                    = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/create_accounts_map?ref=3.1.1"
  accounts                  = local.accounts
  resource_share_principals = var.resource_share_principals

  providers = {
    aws = aws.us_west_2
  }
}
