

There are more AWS SDK examples available in the [AWS Doc SDK Examples](https://github.com/awsdocs/aws-doc-sdk-examples) GitHub repo.

# Amazon Cognito Identity examples using AWS CLI
<a name="cli_2_cognito-identity_code_examples"></a>

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](#actions)

## Actions
<a name="actions"></a>

### `create-identity-pool`
<a name="cognito-identity_CreateIdentityPool_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/getting-started-with-identity-pools.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [CreateIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/create-identity-pool.html) in *AWS CLI Command Reference*. 

### `delete-identities`
<a name="cognito-identity_DeleteIdentities_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [DeleteIdentities](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/delete-identities.html) in *AWS CLI Command Reference*. 

### `delete-identity-pool`
<a name="cognito-identity_DeleteIdentityPool_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [DeleteIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/delete-identity-pool.html) in *AWS CLI Command Reference*. 

### `describe-identity-pool`
<a name="cognito-identity_DescribeIdentityPool_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [DescribeIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/describe-identity-pool.html) in *AWS CLI Command Reference*. 

### `get-identity-pool-roles`
<a name="cognito-identity_GetIdentityPoolRoles_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/iam-roles.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [GetIdentityPoolRoles](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/get-identity-pool-roles.html) in *AWS CLI Command Reference*. 

### `list-identity-pools`
<a name="cognito-identity_ListIdentityPools_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [ListIdentityPools](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/list-identity-pools.html) in *AWS CLI Command Reference*. 

### `set-identity-pool-roles`
<a name="cognito-identity_SetIdentityPoolRoles_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/iam-roles.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [SetIdentityPoolRoles](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/set-identity-pool-roles.html) in *AWS CLI Command Reference*. 

### `update-identity-pool`
<a name="cognito-identity_UpdateIdentityPool_cli_2_topic"></a>

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](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html) in the *Amazon Cognito Developer Guide*.  
+  For API details, see [UpdateIdentityPool](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cognito-identity/update-identity-pool.html) in *AWS CLI Command Reference*. 