Skip to content

Resource: awsCognitoUserPoolClient

Provides a Cognito User Pool Client resource.

To manage a User Pool Client created by another service, such as when configuring an OpenSearch Domain to use Cognito authentication, use the awsCognitoManagedUserPoolClient resource instead.

Example Usage

Create a basic user pool client

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsCognitoUserPoolPool = new aws.cognitoUserPool.CognitoUserPool(
  this,
  "pool",
  {
    name: "pool",
  }
);
new aws.cognitoUserPoolClient.CognitoUserPoolClient(this, "client", {
  name: "client",
  userPoolId: awsCognitoUserPoolPool.id,
});

Create a user pool client with no SRP authentication

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsCognitoUserPoolPool = new aws.cognitoUserPool.CognitoUserPool(
  this,
  "pool",
  {
    name: "pool",
  }
);
new aws.cognitoUserPoolClient.CognitoUserPoolClient(this, "client", {
  explicitAuthFlows: ["ADMIN_NO_SRP_AUTH"],
  generateSecret: true,
  name: "client",
  userPoolId: awsCognitoUserPoolPool.id,
});

Create a user pool client with pinpoint analytics

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsCognitoUserPoolTest = new aws.cognitoUserPool.CognitoUserPool(
  this,
  "test",
  {
    name: "pool",
  }
);
const awsPinpointAppTest = new aws.pinpointApp.PinpointApp(this, "test_1", {
  name: "pinpoint",
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
awsPinpointAppTest.overrideLogicalId("test");
const dataAwsCallerIdentityCurrent =
  new aws.dataAwsCallerIdentity.DataAwsCallerIdentity(this, "current", {});
const dataAwsIamPolicyDocumentAssumeRole =
  new aws.dataAwsIamPolicyDocument.DataAwsIamPolicyDocument(
    this,
    "assume_role",
    {
      statement: [
        {
          actions: ["sts:AssumeRole"],
          effect: "Allow",
          principals: [
            {
              identifiers: ["cognito-idp.amazonaws.com"],
              type: "Service",
            },
          ],
        },
      ],
    }
  );
const dataAwsIamPolicyDocumentTest =
  new aws.dataAwsIamPolicyDocument.DataAwsIamPolicyDocument(this, "test_4", {
    statement: [
      {
        actions: [
          "mobiletargeting:UpdateEndpoint",
          "mobiletargeting:PutEvents",
        ],
        effect: "Allow",
        resources: [
          `arn:aws:mobiletargeting:*:\${${dataAwsCallerIdentityCurrent.accountId}}:apps/\${${awsPinpointAppTest.applicationId.fqn}}*`,
        ],
      },
    ],
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
dataAwsIamPolicyDocumentTest.overrideLogicalId("test");
const awsIamRoleTest = new aws.iamRole.IamRole(this, "test_5", {
  assumeRolePolicy: dataAwsIamPolicyDocumentAssumeRole.json,
  name: "role",
});
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
awsIamRoleTest.overrideLogicalId("test");
const awsIamRolePolicyTest = new aws.iamRolePolicy.IamRolePolicy(
  this,
  "test_6",
  {
    name: "role_policy",
    policy: dataAwsIamPolicyDocumentTest.json,
    role: awsIamRoleTest.id,
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
awsIamRolePolicyTest.overrideLogicalId("test");
const awsCognitoUserPoolClientTest =
  new aws.cognitoUserPoolClient.CognitoUserPoolClient(this, "test_7", {
    analyticsConfiguration: [
      {
        applicationId: awsPinpointAppTest.applicationId,
        externalId: "some_id",
        roleArn: awsIamRoleTest.arn,
        userDataShared: true,
      },
    ],
    name: "pool_client",
    userPoolId: awsCognitoUserPoolTest.id,
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
awsCognitoUserPoolClientTest.overrideLogicalId("test");

Create a user pool client with Cognito as the identity provider

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const awsCognitoUserPoolPool = new aws.cognitoUserPool.CognitoUserPool(
  this,
  "pool",
  {
    name: "pool",
  }
);
new aws.cognitoUserPoolClient.CognitoUserPoolClient(this, "userpool_client", {
  allowedOauthFlows: ["code", "implicit"],
  allowedOauthFlowsUserPoolClient: true,
  allowedOauthScopes: ["email", "openid"],
  callbackUrls: ["https://example.com"],
  name: "client",
  supportedIdentityProviders: ["COGNITO"],
  userPoolId: awsCognitoUserPoolPool.id,
});

Argument Reference

The following arguments are required:

  • name - (Required) Name of the application client.
  • userPoolId - (Required) User pool the client belongs to.

The following arguments are optional:

  • accessTokenValidity - (Optional) Time limit, between 5 minutes and 1 day, after which the access token is no longer valid and cannot be used. This value will be overridden if you have entered a value in tokenValidityUnits.
  • allowedOauthFlowsUserPoolClient - (Optional) Whether the client is allowed to follow the OAuth protocol when interacting with Cognito user pools.
  • allowedOauthFlows - (Optional) List of allowed OAuth flows (code, implicit, client_credentials).
  • allowedOauthScopes - (Optional) List of allowed OAuth scopes (phone, email, openid, profile, and aws.cognito.signin.user.admin).
  • analyticsConfiguration - (Optional) Configuration block for Amazon Pinpoint analytics for collecting metrics for this user pool. Detailed below.
  • authSessionValidity - (Optional) Amazon Cognito creates a session token for each API request in an authentication flow. AuthSessionValidity is the duration, in minutes, of that session token. Your user pool native user must respond to each authentication challenge before the session expires. Valid values between 3 and 15. Default value is 3.
  • callbackUrls - (Optional) List of allowed callback URLs for the identity providers.
  • defaultRedirectUri - (Optional) Default redirect URI. Must be in the list of callback URLs.
  • enableTokenRevocation - (Optional) Enables or disables token revocation.
  • enablePropagateAdditionalUserContextData - (Optional) Activates the propagation of additional user context data.
  • explicitAuthFlows - (Optional) List of authentication flows (ADMIN_NO_SRP_AUTH, CUSTOM_AUTH_FLOW_ONLY, USER_PASSWORD_AUTH, ALLOW_ADMIN_USER_PASSWORD_AUTH, ALLOW_CUSTOM_AUTH, ALLOW_USER_PASSWORD_AUTH, ALLOW_USER_SRP_AUTH, ALLOW_REFRESH_TOKEN_AUTH).
  • generateSecret - (Optional) Should an application secret be generated.
  • idTokenValidity - (Optional) Time limit, between 5 minutes and 1 day, after which the ID token is no longer valid and cannot be used. This value will be overridden if you have entered a value in tokenValidityUnits.
  • logoutUrls - (Optional) List of allowed logout URLs for the identity providers.
  • preventUserExistenceErrors - (Optional) Choose which errors and responses are returned by Cognito APIs during authentication, account confirmation, and password recovery when the user does not exist in the user pool. When set to enabled and the user does not exist, authentication returns an error indicating either the username or password was incorrect, and account confirmation and password recovery return a response indicating a code was sent to a simulated destination. When set to legacy, those APIs will return a userNotFoundException exception if the user does not exist in the user pool.
  • readAttributes - (Optional) List of user pool attributes the application client can read from.
  • refreshTokenValidity - (Optional) Time limit in days refresh tokens are valid for.
  • supportedIdentityProviders - (Optional) List of provider names for the identity providers that are supported on this client. Uses the providerName attribute of awsCognitoIdentityProvider resource(s), or the equivalent string(s).
  • tokenValidityUnits - (Optional) Configuration block for units in which the validity times are represented in. Detailed below.
  • writeAttributes - (Optional) List of user pool attributes the application client can write to.

analyticsConfiguration

Either applicationArn or applicationId is required.

  • applicationArn - (Optional) Application ARN for an Amazon Pinpoint application. Conflicts with externalId and roleArn.
  • applicationId - (Optional) Application ID for an Amazon Pinpoint application.
  • externalId - (Optional) ID for the Analytics Configuration. Conflicts with applicationArn.
  • roleArn - (Optional) ARN of an IAM role that authorizes Amazon Cognito to publish events to Amazon Pinpoint analytics. Conflicts with applicationArn.
  • userDataShared (Optional) If set to true, Amazon Cognito will include user data in the events it publishes to Amazon Pinpoint analytics.

tokenValidityUnits

Valid values for the following arguments are: seconds, minutes, hours or days.

  • accessToken - (Optional) Time unit in for the value in accessTokenValidity, defaults to hours.
  • idToken - (Optional) Time unit in for the value in idTokenValidity, defaults to hours.
  • refreshToken - (Optional) Time unit in for the value in refreshTokenValidity, defaults to days.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • clientSecret - Client secret of the user pool client.
  • id - ID of the user pool client.

Import

Cognito User Pool Clients can be imported using the id of the Cognito User Pool, and the id of the Cognito User Pool Client, e.g.,

$ terraform import aws_cognito_user_pool_client.client us-west-2_abc123/3ho4ek12345678909nh3fmhpko