Inbound federation Lambda trigger
The inbound federation trigger transforms federated user attributes during the authentication process with external identity providers. When users authenticate through configured identity providers, this trigger allows you to modify responses from external SAML and OIDC providers by intercepting and transforming data in the authentication process, providing programmatic control over how Amazon Cognito user pools handle federated users and their attributes.
Use this trigger to add, override, or suppress attributes before creating new users or updating existing federated user profiles. This trigger receives raw identity provider attributes as input and returns modified attributes that Amazon Cognito applies to the user profile.
Topics
Flow overview
When a user authenticates with an external identity provider, Amazon Cognito invokes the inbound federation trigger before creating or updating the user profile. The trigger receives the raw attributes from the identity provider and can transform them before Amazon Cognito stores them. This flow occurs for both new federated users and existing users who sign in again through federation.
Inbound federation Lambda trigger parameters
The request that Amazon Cognito passes to this Lambda function is a combination of the parameters below and the common parameters that Amazon Cognito adds to all requests.
Inbound federation request parameters
- providerName
-
The name of the external identity provider.
- providerType
-
The type of the external identity provider. Valid values:
OIDC,SAML,Facebook,Google,SignInWithApple,LoginWithAmazon. - attributes
-
The raw attributes received from the identity provider before processing. The structure varies based on provider type.
- attributes.tokenResponse
-
OAuth token response data from the
/tokenendpoint. Available for OIDC and social providers only. Containsaccess_token,id_token,refresh_token,token_type,expires_in, andscope. - attributes.idToken
-
Decoded and validated ID token JWT claims. Available for OIDC and social providers only. Contains verified user identity information including
sub(unique user identifier),email,name,iss(issuer),aud(audience),exp(expiration), andiat(issued time). - attributes.userInfo
-
Extended user profile information from the UserInfo endpoint. Available for OIDC and social providers only. Contains detailed profile attributes such as
given_name,family_name,picture,address, and other provider-specific fields. May be empty if the IdP doesn't support the UserInfo endpoint or if the endpoint call fails. - attributes.samlResponse
-
SAML assertion attributes. Available for SAML providers only. Contains attributes from the SAML response.
Inbound federation response parameters
- userAttributesToMap
-
The user attributes to apply to the user profile.
Important
You must include ALL user attributes you want to retain in the response,
including attributes you are not modifying. Any attributes not included in the
userAttributesToMap response will be dropped and not stored in
the user profile. This applies to both modified and unmodified
attributes.
Empty response behavior
If you return an empty object {} for
userAttributesToMap, all original attributes from the identity
provider are retained unchanged. This acts as a no-op, as if the Lambda function
was never executed. This is different from omitting attributes, which drops
them.
Provider-specific attributes
The structure of request.attributes varies based on the
providerType. OIDC and social providers include
tokenResponse, idToken, and userInfo
objects. SAML providers include only the samlResponse
object.
Inbound federation example: Group membership management
This example shows how to map federated identity provider groups to Amazon Cognito user pools groups. This function extracts group membership from the federated response and automatically adds users to corresponding Amazon Cognito groups, eliminating the need for post-authentication triggers.
Amazon Cognito passes event information to your Lambda function. The function then returns the same event object to Amazon Cognito, with any changes in the response. In the Lambda console, you can set up a test event with data that is relevant to your Lambda trigger. The following is a test event for this code sample:
Inbound federation example: Truncate large attributes
This example shows how to truncate attribute values that exceed Amazon Cognito's storage limits. This function checks each attribute from the identity provider. If an attribute value exceeds 2048 characters, it truncates the value and adds ellipsis to indicate truncation. All other attributes pass through unchanged.
Amazon Cognito passes event information to your Lambda function. The function then returns the same event object to Amazon Cognito, with any changes in the response. In the Lambda console, you can set up a test event with data that is relevant to your Lambda trigger. The following is a test event for this code sample:
Inbound federation example: Logging federation events
This example shows how to log federated authentication events for monitoring and debugging. This example function captures detailed information about federated users and their attributes, providing visibility into the authentication process.
Amazon Cognito passes event information to your Lambda function. The function then returns the same event object to Amazon Cognito, with any changes in the response. In the Lambda console, you can set up a test event with data that is relevant to your Lambda trigger. The following is a test event for this code sample:
Expected CloudWatch Logs output: