View a markdown version of this page

Amazon Cognito Identity examples using AWS CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Amazon Cognito Identity examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with Amazon Cognito Identity.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use create-identity-pool.

AWS CLI

To create an identity pool with Cognito identity pool provider

The following create-identity-pool example creates an identity pool named MyIdentityPool. It has a Cognito identity pool provider. Unauthenticated identities are not allowed.

aws cognito-identity create-identity-pool \ --identity-pool-name MyIdentityPool \ --no-allow-unauthenticated-identities \ --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_aaaaaaaaa",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false

Output:

{ "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "IdentityPoolName": "MyIdentityPool", "AllowUnauthenticatedIdentities": false, "CognitoIdentityProviders": [ { "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111", "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado", "ServerSideTokenCheck": false } ] }

For more information, see Getting started with Amazon Cognito identity pools in the Amazon Cognito Developer Guide.

The following code example shows how to use delete-identities.

AWS CLI

To delete identity pool

The following delete-identities example deletes the specified identity.

aws cognito-identity delete-identities \ --identity-ids-to-delete "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"

Output:

{ "UnprocessedIdentityIds": [] }

For more information, see Amazon Cognito identity pools in the Amazon Cognito Developer Guide.

The following code example shows how to use delete-identity-pool.

AWS CLI

To delete identity pool

The following delete-identity-pool example deletes the specified identity pool.

aws cognito-identity delete-identity-pool \ --identity-pool-id "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"

This command produces no output.

For more information, see Amazon Cognito identity pools in the Amazon Cognito Developer Guide.

The following code example shows how to use describe-identity-pool.

AWS CLI

To describe an identity pool

The following describe-identity-pool example describes the specified identity pool.

aws cognito-identity describe-identity-pool \ --identity-pool-id "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"

Output:

{ "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "IdentityPoolName": "MyIdentityPool", "AllowUnauthenticatedIdentities": false, "CognitoIdentityProviders": [ { "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111", "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado", "ServerSideTokenCheck": false } ] }

For more information, see Amazon Cognito identity pools in the Amazon Cognito Developer Guide.

The following code example shows how to use get-identity-pool-roles.

AWS CLI

To get identity pool roles

The following get-identity-pool-roles example gets the roles for the specified identity pool.

aws cognito-identity get-identity-pool-roles \ --identity-pool-id "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"

Output:

{ "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "Roles": { "authenticated": "arn:aws:iam::123456789012:role/Cognito_MyIdentityPoolAuth_Role", "unauthenticated": "arn:aws:iam::123456789012:role/Cognito_MyIdentityPoolUnauth_Role" } }

For more information, see IAM roles in the Amazon Cognito Developer Guide.

The following code example shows how to use list-identity-pools.

AWS CLI

To list identity pools

The following list-identity-pools example lists identity pools. A maximum of 20 identity pools are listed.

aws cognito-identity list-identity-pools \ --max-results 20

Output:

{ "IdentityPools": [ { "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "IdentityPoolName": "MyIdentityPool" }, { "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE22222", "IdentityPoolName": "AnotherIdentityPool" }, { "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE33333", "IdentityPoolName": "IdentityPoolRegionA" } ] }

For more information, see Amazon Cognito identity pools in the Amazon Cognito Developer Guide.

The following code example shows how to use set-identity-pool-roles.

AWS CLI

To set identity pool roles

The following set-identity-pool-roles example sets an identity pool role.

aws cognito-identity set-identity-pool-roles \ --identity-pool-id "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" \ --roles authenticated="arn:aws:iam::123456789012:role/Cognito_MyIdentityPoolAuth_Role"

This command produces no output.

For more information, see IAM roles in the Amazon Cognito Developer Guide.

The following code example shows how to use update-identity-pool.

AWS CLI

To update an identity pool

The following update-identity-pool example updates an identity pool. It sets the name to MyIdentityPool. It adds Cognito as an identity provider. It disallows unauthenticated identities.

aws cognito-identity update-identity-pool \ --identity-pool-id "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" \ --identity-pool-name "MyIdentityPool" \ --no-allow-unauthenticated-identities \ --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false

Output:

{ "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "IdentityPoolName": "MyIdentityPool", "AllowUnauthenticatedIdentities": false, "CognitoIdentityProviders": [ { "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111", "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado", "ServerSideTokenCheck": false } ] }

For more information, see Amazon Cognito identity pools in the Amazon Cognito Developer Guide.