Search code examples
amazon-web-servicesterraformterraform-provider-aws

how Implement sms_configuration in terraform as optional?


I'm adding sms_configuration to my cognito module but... I still want to use null values... because there are times when a project is not going to use this attribute, it necessarily asks me to give them a value, is it required?

resource "aws_cognito_user_pool" "user_pools" {
  for_each = var.user_pools
  name    = each.value.name    
  .... other atributes....

  sms_configuration {
    external_id    = ""
    sns_caller_arn = ""
    sns_region     = ""
  }
}

After apply:

module.aws_user_pool[0].aws_cognito_user_pool.user_pools["analityc"]: Creating... ╷ │ Error: creating Cognito User Pool: InvalidParameter: 1 validation error(s) found. │ - missing required field, CreateUserPoolInput.SmsConfiguration.SnsCallerArn.

The idea was to use it only when sending values, in the tfvars... but in any case it requests that a value must be put in external_id and in sns_caller_arn.

If I don't use the block, if it does the apply correctly.


Solution

  • What you could do is use dynamic in combination with for_each meta-argument. For example:

    resource "aws_cognito_user_pool" "user_pools" {
      for_each = var.user_pools
      name    = each.value.name    
      .... other atributes....
    
      dynamic "sms_configuration" {
        for_each = var.sms_configuration_enabled ? [1] : []
        content {
          external_id    = ""
          sns_caller_arn = ""
          sns_region     = ""
        }
      }
      .
      .
      .
    }
    

    You would then have to define the sms_configuration_enabled variable (type bool) and decide how to define other parameters required by the sms_configuration block.