Skip to content

Commit

Permalink
feat: auto create sms role
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtoj committed Sep 22, 2023
1 parent 95d4075 commit 7d39ca6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
53 changes: 51 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ resource "aws_cognito_user_pool" "this" {
}

dynamic "sms_configuration" {
for_each = coalesce(var.sms_config.sns_caller_arn, "__UNSET__") != "__UNSET__" ? [true] : []
for_each = var.sms_config.enabled ? [true] : []
content {
external_id = var.sms_config.external_id
sns_caller_arn = var.sms_config.sns_caller_arn
sns_caller_arn = coalesce(var.sms_config.sns_caller_arn, aws_iam_role.sms.arn)
}
}

Expand All @@ -161,3 +161,52 @@ resource "aws_cognito_user_pool" "this" {

tags = module.cognito_userpool_label.tags
}

# ---------------------------------------------------------------------- iam ---

module "cognito_userpool_sms_label" {
source = "cloudposse/label/null"
version = "0.25.0"

attattributes = "sms"
context = module.cognito_userpool_label.context
}

resource "random_uuid" "sms_role_external_id" {}

data "aws_iam_policy_document" "sms" {
statement {
effect = "Allow"

actions = [
"sns:publish",
]

resources = [
"*",
]
}
}

resource "aws_iam_role" "sms" {
count = local.enabled ? 1 : 0

name = module.component.id
description = ""
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow"
Principal = { "Service" : "cognito-idp.amazonaws.com" }
Action = ["sts:AssumeRole", "sts:TagSession"]
condition = { "StringEquals" = { "sts:ExternalId" = random_uuid.sms_role_external_id.result } }
}]
})

inline_policy {
name = "access"
policy = data.aws_iam_policy_document.sms.json
}

tags = module.cognito_userpool_sms_label.tags
}
1 change: 1 addition & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ variable "recovery_mechanisms" {

variable "sms_config" {
type = object({
enabled = optional(bool, false)
external_id = optional(string, "")
sns_caller_arn = optional(string, "")
})
Expand Down

0 comments on commit 7d39ca6

Please sign in to comment.