• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense Keys
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
LayersManaged InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. Usage
  2. Events
  3. Cognito User Pool

Cognito User Pool Triggers

The cognitoUserPool event runs a Lambda function when an Amazon Cognito user pool reaches a trigger, such as a user signing up or Cognito issuing tokens. By default, the Framework creates the user pool, named after the event's pool key, and attaches the function as the trigger. To attach a function to a user pool that already exists, set existing: true (see Use an existing user pool).

This serverless.yml creates a user pool named MyUserPool and runs preSignUp before each sign-up:

# serverless.yml
service: my-service

provider:
  name: aws
  runtime: nodejs24.x

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: PreSignUp

The handler receives the trigger event and returns it, with any changes in event.response:

// handler.mjs
export const preSignUp = async (event) => {
  if (event.request.userAttributes.email?.endsWith('@example.com')) {
    event.response.autoConfirmUser = true
  }
  return event
}

cognitoUserPool event options

The cognitoUserPool event accepts these keys:

KeyTypeDefaultDescription
poolstring, up to 128 charactersRequiredThe user pool name: letters, digits, spaces and _ + = , . @ -
triggerone of the valid triggersRequiredThe trigger that runs the function
existingbooleanfalseAttach the function to an existing user pool instead of creating one
forceDeploybooleanfalseWith existing: true, reapply the trigger on every deployment
kmsKeyIdKMS key ARN or intrinsic functionRequired for custom sender triggersThe key Cognito encrypts codes with
lambdaVersionV1_0, V2_0 or V3_0V1_0The trigger event version, for PreTokenGeneration, InboundFederation, CustomSMSSender and CustomEmailSender only

Valid triggers

The cognitoUserPool event's trigger accepts these values:

TriggerWhen Cognito invokes the function
PreSignUpBefore it registers a new user
PostConfirmationAfter a user confirms their registration
PreAuthenticationBefore it signs a user in
PostAuthenticationAfter it signs a user in
PreTokenGenerationBefore it issues tokens, to customize claims
CustomMessageBefore it sends a verification, invitation or other message, to customize it
DefineAuthChallenge, CreateAuthChallenge, VerifyAuthChallengeResponseDuring a custom authentication flow
UserMigrationWhen a user who isn't in the pool signs in or resets their password, to migrate them
InboundFederationAfter a user signs in through an external SAML or OIDC identity provider, before it stores the user's attributes, to transform them
CustomSMSSender, CustomEmailSenderInstead of sending an SMS or email itself, so that you send it through another provider

For the event each trigger receives, see Customizing user pool workflows with Lambda triggers.

Several triggers and pools

To attach several triggers to one user pool, use the same pool in each cognitoUserPool event. The Framework creates the pool once:

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: PreSignUp
  customMessage:
    handler: handler.customMessage
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: CustomMessage

A different pool value creates another user pool. To run one function for several pools, add an event for each:

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: CustomersPool
          trigger: PreSignUp
      - cognitoUserPool:
          pool: PartnersPool
          trigger: PreSignUp

Configure the generated user pool

The Framework creates each user pool as an AWS::Cognito::UserPool resource with the logical ID CognitoUserPool followed by the pool name's letters and digits: MyUserPool becomes CognitoUserPoolMyUserPool. To set other user pool properties, declare a resource with that logical ID under resources; the Framework merges it with the generated pool and keeps the triggers:

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: PreSignUp

resources:
  Resources:
    CognitoUserPoolMyUserPool:
      Type: AWS::Cognito::UserPool
      Properties:
        UsernameAttributes:
          - email
        AutoVerifiedAttributes:
          - email

For the available properties, see AWS::Cognito::UserPool. For logical IDs of other generated resources, see the AWS CloudFormation resource reference.

Use an existing user pool

To attach a function to a user pool that another stack or the console created, set the cognitoUserPool event's existing: true. All other keys work the same way:

functions:
  customMessage:
    handler: handler.customMessage
    events:
      - cognitoUserPool:
          pool: legacy-user-pool
          trigger: CustomMessage
          existing: true
  • pool is the user pool's name, not its ID or ARN. The Framework updates the first user pool in the Region with that name.
  • Each function can attach to one existing user pool; several triggers on that pool are fine.
  • The Framework adds a custom resource, backed by a Lambda function and an IAM role, that adds the function to the pool's triggers and grants Cognito permission to invoke it. On serverless remove, it removes the service's functions from the pool's triggers.
  • The custom resource's role gets cognito-idp:ListUserPools, cognito-idp:DescribeUserPool and cognito-idp:UpdateUserPool, lambda:AddPermission and lambda:RemovePermission on the trigger functions, iam:PassRole, and kms:CreateGrant on the key of a custom sender trigger.

Reapply triggers on an existing user pool

The custom resource for an existing user pool runs only when its configuration changes. When something outside the service can change the pool's triggers, set forceDeploy: true to reapply them on every deployment:

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: legacy-user-pool
          trigger: PreSignUp
          existing: true
          forceDeploy: true

forceDeploy has an effect only together with existing: true.

Pre token generation versions

The PreTokenGeneration trigger has three event versions, which the cognitoUserPool event's lambdaVersion selects:

  • V1_0 (the default when lambdaVersion is omitted): customizes the ID token.
  • V2_0: customizes the ID and access tokens.
  • V3_0: customizes the ID and access tokens, including for machine-to-machine (client credentials) grants.
functions:
  preTokenGeneration:
    handler: handler.preTokenGeneration
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: PreTokenGeneration
          lambdaVersion: V2_0

V2_0 and V3_0 require a user pool on the Essentials or Plus feature plan. See Pre token generation Lambda trigger.

Inbound federation trigger

The InboundFederation trigger runs when a user signs in through an external identity provider (SAML, OIDC or a social provider) configured on the user pool. The function receives the attributes the provider returned and decides which attributes the user pool stores, for new and returning federated users alike. Use it to shorten attribute values that exceed the user pool's 2,048-character limit, to drop attributes you don't want stored, or to rename the provider's group names:

functions:
  inboundFederation:
    handler: handler.inboundFederation
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: InboundFederation

The handler finds the provider's attributes in event.request.attributes.samlResponse for SAML providers, and in event.request.attributes.idToken and event.request.attributes.userInfo for OIDC and social providers. It returns the attributes to store in event.response.userAttributesToMap:

// handler.mjs
const MAX_ATTRIBUTE_LENGTH = 2048

export const inboundFederation = async (event) => {
  const { providerType, attributes } = event.request
  const providerAttributes =
    providerType === 'SAML'
      ? attributes.samlResponse
      : { ...attributes.userInfo, ...attributes.idToken }

  event.response.userAttributesToMap = Object.fromEntries(
    Object.entries(providerAttributes ?? {}).map(([name, value]) => [
      name,
      typeof value === 'string' ? value.slice(0, MAX_ATTRIBUTE_LENGTH) : value,
    ]),
  )
  return event
}
  • userAttributesToMap replaces the provider's attributes: the user pool drops every attribute the function leaves out, so return the unchanged ones too. An empty object keeps all of the provider's attributes as they are.
  • lambdaVersion accepts only V1_0 for InboundFederation, which is also the default.
  • The trigger works for a user pool the Framework creates and for an existing user pool (existing: true). Cognito invokes it only for sign-ins through an identity provider configured on the user pool.

For the full event and more examples, see Inbound federation Lambda trigger.

Custom sender triggers

The CustomSMSSender and CustomEmailSender triggers let a function send Cognito's messages through another provider. Cognito encrypts the code or temporary password in each message with a symmetric AWS KMS key, so these triggers need the cognitoUserPool event's kmsKeyId:

provider:
  name: aws
  runtime: nodejs24.x
  iam:
    role:
      statements:
        - Effect: Allow
          Action: kms:Decrypt
          Resource: !GetAtt SenderKey.Arn

functions:
  customEmailSender:
    handler: handler.customEmailSender
    events:
      - cognitoUserPool:
          pool: MyUserPool
          trigger: CustomEmailSender
          kmsKeyId: !GetAtt SenderKey.Arn

resources:
  Resources:
    SenderKey:
      Type: AWS::KMS::Key
      Properties:
        Description: Encrypts Cognito codes for the custom email sender
  • kmsKeyId takes a KMS key ARN, such as arn:aws:kms:us-east-1:123456789012:key/12345678-9abc-def0-1234-56789abcdef1, or an intrinsic function that returns one.
  • A user pool uses one key for all its custom sender triggers.
  • lambdaVersion accepts only V1_0 for custom sender triggers, which is also the default.
  • The function decrypts the code with the key, so its role needs kms:Decrypt on the key, as in the preceding example.

The handler finds the reason for the message in event.triggerSource (such as CustomEmailSender_SignUp or CustomSMSSender_ForgotPassword), the request type in event.request.type (customEmailSenderRequestV1 or customSMSSenderRequestV1) and the encrypted code in event.request.code:

// handler.mjs
export const customEmailSender = async (event) => {
  if (event.triggerSource === 'CustomEmailSender_SignUp') {
    // decrypt event.request.code with the AWS Encryption SDK, then send the email
  }
}

For decrypting the code, see Custom sender Lambda triggers, Custom email sender and Custom SMS sender.

Custom message trigger handlers

One CustomMessage function handles every message type; event.triggerSource says which message Cognito is about to send:

// handler.mjs
export const customMessage = async (event) => {
  if (event.triggerSource === 'CustomMessage_SignUp') {
    event.response.emailSubject = 'Welcome'
    event.response.emailMessage = `Your code is ${event.request.codeParameter}`
  }
  return event
}

Cognito sends the emailSubject and emailMessage a custom message function returns only when the user pool sends email through Amazon SES, with EmailSendingAccount: DEVELOPER. For a user pool the Framework creates, set the email configuration as in Configure the generated user pool:

resources:
  Resources:
    CognitoUserPoolMyUserPool:
      Type: AWS::Cognito::UserPool
      Properties:
        EmailConfiguration:
          EmailSendingAccount: DEVELOPER
          SourceArn: arn:aws:ses:us-east-1:123456789012:identity/no-reply@example.com

SourceArn is the ARN of an SES verified identity. See Amazon SES email configuration.

For all trigger sources and the response fields, see Custom message Lambda trigger.

Permissions for Cognito triggers

For a user pool the Framework creates, it adds a lambda:InvokeFunction permission for cognito-idp.amazonaws.com, limited to that user pool, for each trigger function. For an existing user pool, the custom resource adds the permission. The trigger functions' own role needs no extra permissions, except kms:Decrypt for custom sender triggers.

Troubleshooting Cognito user pool triggers

Only one Cognito User Pool can be configured per function. ...

This happens when one function has existing: true events for two different user pools. Use one function per existing user pool; the functions can share a handler.

KMS Key must be set when using a Custom Sender Source Trigger (CustomSMSSender and/or CustomEmailSender). ...

This happens when a CustomSMSSender or CustomEmailSender event has no kmsKeyId. Add the key's ARN as shown in Custom sender triggers.

Only one KMS Key for can be configured per Cognito User Pool. ...

This happens when custom sender events for the same user pool set different kmsKeyId values. Use one key for all custom sender triggers of the pool.

lambdaVersion is only supported for CustomSMSSender, CustomEmailSender, PreTokenGeneration, and InboundFederation triggers. ...

This happens when an event with another trigger sets lambdaVersion. Remove lambdaVersion from that event. A lambdaVersion that the trigger doesn't accept, such as V2_0 on CustomEmailSender, stops packaging with Invalid lambdaVersion "V2_0" for "CustomEmailSender". Allowed: V1_0.

Related

  • Amazon Cognito user pool Lambda triggers
  • Cognito settings in the serverless.yml reference
  • IAM permissions for functions
Edit this page
Prev CloudFrontNextEventBridge Event

Contents

  • Cognito User Pool Triggers
  • cognitoUserPool event options
  • Valid triggers
  • Several triggers and pools
  • Configure the generated user pool
  • Use an existing user pool
  • Reapply triggers on an existing user pool
  • Pre token generation versions
  • Inbound federation trigger
  • Custom sender triggers
  • Custom message trigger handlers
  • Permissions for Cognito triggers
  • Troubleshooting Cognito user pool triggers
  • Only one Cognito User Pool can be configured per function. ...
  • KMS Key must be set when using a Custom Sender Source Trigger (CustomSMSSender and/or CustomEmailSender). ...
  • Only one KMS Key for can be configured per Cognito User Pool. ...
  • lambdaVersion is only supported for CustomSMSSender, CustomEmailSender, PreTokenGeneration, and InboundFederation triggers. ...
  • Related

Related

GuidesPluginsExamplesSlack CommunitySupport