# Defines an SNS topic with a subscribed lambda function.
# Includes a resource policy allowing SNS to invoke the lambda.

# SNS topic definition
module "proxied_sns_topic" {
  source = "git@github.com:theorchard/terraform-sns.git//?ref=1.1.4"

  application_family              = var.application_family
  environment                     = var.environment
  sns_topic_name                  = var.sns_topic_name
  sns_lambda_subscription_enabled = true
  sns_lambda_function_name        = var.lambda_arn
}

# Resource policy to allow the SNS topic to trigger the lambda function
resource "aws_lambda_permission" "allow_proxy_sns_messages" {
  statement_id  = "${var.environment}-trigger-${var.lambda_name}-from-${var.sns_topic_name}"
  action        = "lambda:InvokeFunction"
  function_name = var.lambda_name
  principal     = "sns.amazonaws.com"
  source_arn    = module.proxied_sns_topic.topic_arn
}
