【问题标题】:Set account recovery preference for AWS Cognito User Pool with Terraform使用 Terraform 为 AWS Cognito 用户池设置帐户恢复首选项
【发布时间】:2020-03-25 22:51:28
【问题描述】:

本着基础设施即代码的精神,我通过 Terraform 使用有用的 aws_cognito_user_pool 资源配置了一个 AWS Cognito 用户池。

但是,我似乎无法在 MFA 和验证 部分下找到帐户恢复首选项的参数/配置映射。

如果没有说明,这似乎是我的默认选择:

(不推荐)电话(如果可用),否则发送电子邮件,并且允许用户通过电话重置密码(如果他们也将其用于 MFA)。


目标

我想将其设置为 Email only,如下图红色矩形所示:

请问有谁知道我需要使用什么 Terraform 参数来实现这一点? aws_cognito_user_pool 资源中记录的选项似乎都没有映射到此。

【问题讨论】:

    标签: amazon-web-services terraform amazon-cognito terraform-provider-aws infrastructure-as-code


    【解决方案1】:

    一年过去了,由于aws_cognito_user_pool 资源的新引入设置account_recovery_setting,我现在可以回答自己的问题了。

    例如,要将帐户恢复偏好设置为仅电子邮件,我们可以执行以下操作:

    resource "aws_cognito_user_pool" "mypool" {
      name = "mypool"
    
      account_recovery_setting {
        recovery_mechanism {
          name     = "verified_email"
          priority = 1
        }
      }
    }
    

    这从 AWS 提供商的v3.19.0 开始可用,作为此merged PR 的一部分。

    【讨论】:

      【解决方案2】:

      您好Peter,我正在使用 CloudFormation 模板来创建 Cognito 配置。

      稍作修改并转换为 YAML。我们可以将恢复设置设置为仅电子邮件选项。请找到下面的代码sn-p。

      UserPool:
          Type: "AWS::Cognito::UserPool"
          Properties:
            UserPoolName: "test-pool"
            UsernameAttributes: [email]
            AccountRecoverySetting:
              RecoveryMechanisms:
                - Name: "verified_email"
                  Priority: 1
            AutoVerifiedAttributes:
              - email
      

      这似乎对我有用:)

      注意:在尝试为 "admin_only" 合并其他选项时,AWS 会生成错误 Invalid account recovery setting parameter。帐户恢复设置不能将 admin_only 设置与任何其他恢复机制一起使用。

      【讨论】:

      • 嘿@DHEERAJ。根据我的理解:您的意思是说您在aws_cloudformation_stack 资源上取得了成功?如果是这样,你能提供一个更完整的sn-p吗?
      • 嗨@Peter J Langley ,,Resources:UserPool:Type:"AWS::Cognito::UserPool"Properties:UserPoolName:"test"UsernameAttributes:[email]AccountRecoverySetting:RecoveryMechanisms:Name:"verified_email “Priority:1AutoVerifiedAttributes:-emailVerificationMessageTemplate:DefaultEmailOption:CONFIRM_WITH_LINKEmailVerificationSubject:SubjectEmailVerificationMessage:verifyemail {####}Schema:-Name:emailAttributeDataType:StringMutable:falseRequired:trueMfaConfiguration:"OFF"UserPoolTags:Tag:"x",,UserPoolClient:Type: "AWS::Cognito::UserPoolClient"Properties:ClientName:"x"GenerateSecret: falseUserPoolId:!RefUserPool
      【解决方案3】:

      Terraform 还不支持它。 但是您可以改用本地 exec:

      resource "null_resource" "setup_account_recovery_settings" {
        triggers = {
          version = "${var.version_local_exec_account_recovery_settings}"
        }
      
        provisioner "local-exec" {
          command = "aws cognito-idp update-user-pool --user-pool-id ${aws_cognito_user_pool.userpool.id} --account-recovery-setting 'RecoveryMechanisms=[{Priority=1,Name=verified_email},{Priority=2,Name=verified_phone_number}]' --region ${var.region}"
        }
      }
      

      但它会清除您的整个配置。相反,您可以将完整配置作为 json 提供,但为什么要使用 terraform 而不是

      【讨论】:

      • Terraform 还不支持它 - 我认为可能是这种情况。我不想使用这个本地 exec 逃生舱口。我现在为此功能请求提出了issue on GitHub
      • 我可以知道,选项None - users will have to contact an administrator to reset their passwords的操作方法
      • Terraform AWS Provider 现在支持此功能:请参阅此处的answer
      【解决方案4】:

      按照大卫的想法,如果你想启用“仅电子邮件”选项,你应该设置

      --account-recovery-setting 'RecoveryMechanisms=[{Priority=1,Name=verified_email}]'
      

      问候,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        • 2020-06-21
        • 2022-11-28
        • 2018-05-11
        • 2018-03-25
        • 1970-01-01
        • 2022-01-13
        相关资源
        最近更新 更多