Ben Miner
Ben Miner

Reputation: 104

CloudFormation removing AWS Cognito Lambda Triggers on update stack operations

I️ have noticed whenever a new CloudFormation stack change is deployed, my User Pool triggers are removed and have to be manually re-added within the AWS dashboard or programmatically. This is a bit of a concern as these triggers conduct some crucial operations with communication between Cognito and the backend system.

At first I️ thought it was the deployment framework we are using, but here is a barebones example of a CF template I️ was able to replicate it with:

Updated to reflect Lambda attachment to User Pool

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "UserPool": {
      "Type": "AWS::Cognito::UserPool",
      "Properties": {
        "UserPoolName": "test",
        "UsernameAttributes": [
          "email"
        ],
        "EmailVerificationMessage": "Your verification code is {####}.",
        "EmailVerificationSubject": "Your verification code",
        "Policies": {
          "PasswordPolicy": {
            "MinimumLength": 8,
            "RequireLowercase": true,
            "RequireNumbers": true
          }
        }
      }
    },
    "UserPoolClient": {
      "Type": "AWS::Cognito::UserPoolClient",
      "Properties": {
        "ClientName": "Test Client",
        "UserPoolId": {
          "Ref": "UserPool"
        },
        "ExplicitAuthFlows": [
          "ALLOW_REFRESH_TOKEN_AUTH",
          "ALLOW_USER_PASSWORD_AUTH",
          "ALLOW_USER_SRP_AUTH"
        ],
        "GenerateSecret": false
      }
    },
    "PreSignUpHandlerLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Role": "arn:aws:iam::...",
        "Code": {
          "S3Bucket": "code-bucket",
          "S3Key": "code-bucket/functions.zip"
        },
        "Handler": "handlers/pre-sign-up.default",
        "Runtime": "nodejs12.x",
        "FunctionName": "test-preSignUpHandler",
        "MemorySize": 1024,
        "Timeout": 6
      }
    },
    "PreSignUpHandlerCustomCognitoUserPool1": {
      "Type": "Custom::CognitoUserPool",
      "Version": 1,
      "DependsOn": [
        "PreSignUpHandlerLambdaFunction"
      ],
      "Properties": {
        "ServiceToken": "arn:aws:lambda:...",
        "FunctionName": "test-preSignUpHandler",
        "UserPoolName": "test",
        "UserPoolConfigs": [
          {
            "Trigger": "PreSignUp"
          }
        ]
      }
    }
  }
}

I️ have dug into CloudWatch logs generated by the update, but nothing is transparent regarding the User Pool update and the removal of the triggers. Has anyone else experienced this and are there any work-arounds?

Upvotes: 2

Views: 722

Answers (1)

Andrew Gillis
Andrew Gillis

Reputation: 3865

This is the expected behavior of CloudFormation. When config drift is detected on stack update it will bring it back in line with your stack template. If you want to retain the changes you should specify the triggers in your CFN template. Be sure to grant cognito access in the resource policy:

{
    "Version": "2012-10-17",
    "Id": "default",
    "Statement": [
        {
            "Sid": "lambda-allow-cognito-my-function",
            "Effect": "Allow",
            "Principal": {
              "Service": "cognito-idp.amazonaws.com"
            },
            "Action": "lambda:InvokeFunction",
            "Resource":  "arn:aws:lambda:us-east-1:123456789012:function:my-function",
            "Condition": {
              "StringEquals": {
                "AWS:SourceAccount": "123456789012"
              },
              "ArnLike": {
                "AWS:SourceArn": "arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_myUserPoolId"
              }
            }
        }
     ]
}

Upvotes: 1

Related Questions