Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize slack channel per included account #76

Open
nitrocode opened this issue Dec 14, 2023 · 0 comments
Open

Customize slack channel per included account #76

nitrocode opened this issue Dec 14, 2023 · 0 comments

Comments

@nitrocode
Copy link
Contributor

nitrocode commented Dec 14, 2023

I have a client setup like this

current setup - single channel for multiple accounts
module "clickops_notifier" {
  source  = "cloudandthings/clickops-notifier/aws"
  version = "5.0.4"

  cloudtrail_bucket_name = "org-cloudtrail"

  included_accounts = [
    module.account_map["production"],
    module.account_map["corp"],
  ]

  webhooks_for_slack_notifications = {
    "clickops" = jsondecode(data.aws_secretsmanager_secret_version.webhook.secret_string)["webhook"]
  }
}

I want to set this up so I can do a separate slack channel per account, which can be done with a for_each per account which results in duplicating a lot of infrastructure.

per account for separate channel using for_each
module "clickops_notifier" {
  source  = "cloudandthings/clickops-notifier/aws"
  version = "5.0.4"

  for_each = toset([
    "production",
    "corp",
  ])

  cloudtrail_bucket_name = "org-cloudtrail"

  included_accounts = [
    module.account_map[each.key],
  ]

  webhooks_for_slack_notifications = {
    "clickops-${each.key}" = jsondecode(data.aws_secretsmanager_secret_version.webhook[each.key].secret_string)["webhook"]
  }
}

What's more ideal is if we can do something like this

per account for separate channel using multiple hooks

Using the key as the account instead of the channel name

module "clickops_notifier" {
  source  = "cloudandthings/clickops-notifier/aws"
  version = "5.0.4"

  cloudtrail_bucket_name = "org-cloudtrail"

  included_accounts = [
    module.account_map["production"],
    module.account_map["corp"],
  ]

  # written out without a for loop to show mapping is
  # account = slack-web-hook
  webhooks_slack_notifications_per_account = {
    module.account_map["production"] = jsondecode(data.aws_secretsmanager_secret_version.webhook["production"].secret_string)["webhook"]
    module.account_map["corp"]       = jsondecode(data.aws_secretsmanager_secret_version.webhook["corp"].secret_string)["webhook"]
  }

  # or
  # webhooks_slack_notifications_per_account = {
  #   for account in data.aws_secretsmanager_secret_version.webhook:
  #   module.account_map[account] = jsondecode(data.aws_secretsmanager_secret_version.webhook[account].secret_string)["webhook"]
  # }
}

resource "aws_ssm_parameter" "webhooks_for_slack" {
for_each = nonsensitive(toset(keys(var.webhooks_for_slack_notifications)))
name = "/${var.naming_prefix}/webhooks-for-slack/${each.key}"
description = "Webhook \"${each.key}\" for clickops notifications via Slack."
type = "SecureString"
value = var.webhooks_for_slack_notifications[each.key]

environment_variables = {
PARAMETER_NAMES_FOR_SLACK_WEBHOOKS = jsonencode([for p in aws_ssm_parameter.webhooks_for_slack : p.name])

logging.info("Configuring Slack messengers...")
for parameter_name in PARAMETER_NAMES_FOR_SLACK_WEBHOOKS:
webhook_url = get_webhook_url(parameter_name)
messenger = Messenger(
webhook_type="slack",
webhook_url=webhook_url,
parameter_name=parameter_name,
)
_MESSENGERS.append(messenger)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant