Amazon Cognito Identity examples using Amazon CLI
The following code examples show you how to perform actions and implement common scenarios by using the Amazon 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.
- Amazon CLI
-
To create an identity pool with Cognito identity pool provider
The following
create-identity-poolexample 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-nameMyIdentityPool\ --no-allow-unauthenticated-identities \ --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_aaaaaaaaa",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=falseOutput:
{ "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. -
For API details, see CreateIdentityPool
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-identities.
- Amazon CLI
-
To delete identity pool
The following
delete-identitiesexample 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. -
For API details, see DeleteIdentities
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-identity-pool.
- Amazon CLI
-
To delete identity pool
The following
delete-identity-poolexample 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. -
For API details, see DeleteIdentityPool
in Amazon CLI Command Reference.
-
The following code example shows how to use describe-identity-pool.
- Amazon CLI
-
To describe an identity pool
The following
describe-identity-poolexample 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. -
For API details, see DescribeIdentityPool
in Amazon CLI Command Reference.
-
The following code example shows how to use get-identity-pool-roles.
- Amazon CLI
-
To get identity pool roles
The following
get-identity-pool-rolesexample 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. -
For API details, see GetIdentityPoolRoles
in Amazon CLI Command Reference.
-
The following code example shows how to use list-identity-pools.
- Amazon CLI
-
To list identity pools
The following
list-identity-poolsexample lists identity pools. A maximum of 20 identity pools are listed.aws cognito-identity list-identity-pools \ --max-results20Output:
{ "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. -
For API details, see ListIdentityPools
in Amazon CLI Command Reference.
-
The following code example shows how to use set-identity-pool-roles.
- Amazon CLI
-
To set identity pool roles
The following
set-identity-pool-rolesexample 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. -
For API details, see SetIdentityPoolRoles
in Amazon CLI Command Reference.
-
The following code example shows how to use update-identity-pool.
- Amazon CLI
-
To update an identity pool
The following
update-identity-poolexample 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=falseOutput:
{ "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. -
For API details, see UpdateIdentityPool
in Amazon CLI Command Reference.
-