Create a Test User in Cognito

If you’re using AWS CDK and Cognito, probably you want to have a test user account. I use one mainly for testing GraphQL queries and mutations in the AppSync console which requires you to provide a userpool username and password.

Here it is, using an AWS CloudFormation Custom Resource:

      new CfnUserPoolUser(this, "TestUser", {
        userPoolId: this.userPool.userPoolId,
        username: TEST_USER_EMAIL,
        userAttributes: [
          { name: "email", value: TEST_USER_EMAIL },
          { name: "email_verified", value: "true" },
        ],
        desiredDeliveryMediums: ["EMAIL"],
      })
      // set test user password
      new AwsCustomResource(this, "SetTestUserPassword", {
        onCreate: {
          service: "CognitoIdentityServiceProvider",
          action: "adminSetUserPassword",
          parameters: {
            UserPoolId: this.userPool.userPoolId,
            Username: TEST_USER_EMAIL,
            Password: TEST_USER_PASSWORD,
            Permanent: true,
          },
          physicalResourceId: PhysicalResourceId.of("SetTestUserPassword"),
        },
        policy: AwsCustomResourcePolicy.fromSdkCalls({
          resources: AwsCustomResourcePolicy.ANY_RESOURCE,
        }),
      })
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s