

# AWS SDK または CLI で `DescribeTimeToLive` を使用する
<a name="example_dynamodb_DescribeTimeToLive_section"></a>

次のサンプルコードは、`DescribeTimeToLive` を使用する方法を説明しています。

アクション例は、より大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。次のコード例で、このアクションのコンテキストを確認できます。
+  [Streams と Time-to-Live を操作する](example_dynamodb_Scenario_StreamsAndTTL_section.md) 

------
#### [ CLI ]

**AWS CLI**  
**テーブルの Time to Live 設定を表示するには**  
次の `describe-time-to-live` の例は、`MusicCollection` テーブルの Time to Live 設定を表示します。  

```
aws dynamodb describe-time-to-live \
    --table-name MusicCollection
```
出力:  

```
{
    "TimeToLiveDescription": {
        "TimeToLiveStatus": "ENABLED",
        "AttributeName": "ttl"
    }
}
```
詳細については、「**Amazon DynamoDB デベロッパーガイド」の「[Time to Live (TTL)](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html)」を参照してください。  
+  API の詳細については、「**AWS CLI Command Reference」の「[DescribeTimeToLive](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/describe-time-to-live.html)」を参照してください。

------
#### [ Java ]

**SDK for Java 2.x**  
AWS SDK for Java 2.x を使用して既存の DynamoDB テーブルの TTL 設定を記述します。  

```
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DescribeTimeToLiveRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTimeToLiveResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;

import java.util.logging.Level;
import java.util.logging.Logger;

    public DescribeTimeToLiveResponse describeTTL(final String tableName, final Region region) {
        final DescribeTimeToLiveRequest request =
            DescribeTimeToLiveRequest.builder().tableName(tableName).build();

        try (DynamoDbClient ddb = dynamoDbClient != null
            ? dynamoDbClient
            : DynamoDbClient.builder().region(region).build()) {
            return ddb.describeTimeToLive(request);
        } catch (ResourceNotFoundException e) {
            System.err.format(TABLE_NOT_FOUND_ERROR, tableName);
            throw e;
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            throw e;
        }
    }
```
+  API の詳細については、「*AWS SDK for Java 2.x API リファレンス*」の「[DescribeTimeToLive](https://docs.aws.amazon.com/goto/SdkForJavaV2/dynamodb-2012-08-10/DescribeTimeToLive)」を参照してください。

------
#### [ JavaScript ]

**SDK for JavaScript (v3)**  
AWS SDK for JavaScript を使用して既存の DynamoDB テーブルの TTL 設定を記述します。  

```
import { DynamoDBClient, DescribeTimeToLiveCommand } from "@aws-sdk/client-dynamodb";

export const describeTTL = async (tableName, region) => {
    const client = new DynamoDBClient({
        region: region,
        endpoint: `https://dynamodb.${region}.amazonaws.com`
    });

    try {
        const ttlDescription = await client.send(new DescribeTimeToLiveCommand({ TableName: tableName }));

        if (ttlDescription.TimeToLiveDescription.TimeToLiveStatus === 'ENABLED') {
            console.log("TTL is enabled for table %s.", tableName);
        } else {
            console.log("TTL is not enabled for table %s.", tableName);
        }

        return ttlDescription;
    } catch (e) {
        console.error(`Error describing table: ${e}`);
        throw e;
    }
}

// Example usage (commented out for testing)
// describeTTL('your-table-name', 'us-east-1');
```
+  API の詳細については、*AWS SDK for JavaScript API リファレンス*の「[DescribeTimeToLive](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/DescribeTimeToLiveCommand)」を参照してください。

------
#### [ Python ]

**SDK for Python (Boto3)**  
AWS SDK for Python (Boto3) を使用して既存の DynamoDB テーブルの TTL 設定を記述します。  

```
import boto3


def describe_ttl(table_name, region):
    """
    Describes TTL on an existing table, as well as a region.

    :param table_name: String representing the name of the table
    :param region: AWS Region of the table - example `us-east-1`
    :return: Time to live description.
    """
    try:
        dynamodb = boto3.resource("dynamodb", region_name=region)
        ttl_description = dynamodb.describe_time_to_live(TableName=table_name)
        print(
            f"TimeToLive for table {table_name} is status {ttl_description['TimeToLiveDescription']['TimeToLiveStatus']}"
        )

        return ttl_description
    except Exception as e:
        print(f"Error describing table: {e}")
        raise


# Enter your own table name and AWS region
describe_ttl("your-table-name", "us-east-1")
```
+  API の詳細については、「**AWS SDK for Python (Boto3) API Reference」の「[DescribeTimeToLive](https://docs.aws.amazon.com/goto/boto3/dynamodb-2012-08-10/DescribeTimeToLive)」を参照してください。

------

AWS SDK デベロッパーガイドとコード例の詳細なリストについては、[AWS SDK で DynamoDB を使用する](sdk-general-information-section.md) を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。