Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.6.0+

Amazon DynamoDB: Configure TTL and Set an Item Expiration

See more Amazon DynamoDB Examples

Choose status, enable, or expire-item. Enable configures the ExpiresAt attribute on ExampleItems, but does not disable or replace an existing TTL configuration. Wait for ENABLED. Expire-item updates only an existing item and sets a Number attribute to UTC epoch seconds seven days ahead. The timestamp remains a string in the N wrapper; no int64 is needed.

Eventual deletion: enabling TTL can also expire existing items that already carry past ExpiresAt values. Configuration can take up to an hour; avoid repeated configuration requests during a transition. Expired items are removed asynchronously and may remain visible for days. Applications must exclude logically expired items themselves if required. TTL is not an exact scheduler, immediate-delete operation, or substitute for access checks. Updating an expiration is a live write.

The item examples use the ExampleItems table from Create a Table, with string partition key PK and string sort key SK. Wait for ACTIVE. DynamoDB AttributeValue JSON uses wrappers such as {"S":"text"}, {"N":"123.45"}, and {"BOOL":true}. Numeric attributes travel as strings; preserve their precision rather than converting them to int or floating point. Names and IDs remain strings.

Uses DynamoDB's regional JSON API: POST to /, application/x-amz-json-1.0, and an operation-specific X-Amz-Target: DynamoDB_20120810... header. The 20120810 API identifier is still current. Chilkat.Rest and AuthAws sign each request with AWS Signature Version 4, service dynamodb. Set the region once; the example builds the matching commercial AWS endpoint. Other AWS partitions use their own endpoint suffixes.

Read current IAM credentials from a private qa_data/aws/dynamodb.json containing string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. This code explicitly reads the file; it does not discover an AWS profile or refresh credentials automatically. The session token is included in the signed X-Amz-Security-Token query parameter, so keep request URLs and diagnostics private. A Socket explicitly enables TLS certificate verification and is supplied to Rest. Reuse the configured Rest object for subsequent requests.

IAM permissions for the illustrated actions: dynamodb:DescribeTimeToLive, dynamodb:UpdateTimeToLive, dynamodb:UpdateItem. Limit access to the appropriate table, index, backup, and restore-target resources. Restoring customer-managed-key encrypted data can also require KMS permissions.

Checks transport completion, HTTP 200, and JSON validity separately. DynamoDB errors include __type and message. HTTP 400 can mean throttling, failed conditions, invalid input, or expired credentials. Use bounded exponential backoff with jitter for retryable failures; renew expired credentials. A failed condition requires an application decision, not an automatic retry. For writes with an uncertain outcome, read/reconcile before retrying. Only the explicitly described batch retries are automatic; other writes are not automatically replayed.

DescribeTimeToLive reference, UpdateTimeToLive reference, UpdateItem reference

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = 0
    ;  Private configuration, not a file in public web content or source control.
    ;  Load current IAM credentials authorized for the DynamoDB operation.
    credentials.i = CkJsonObject::ckCreate()
    If credentials.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(credentials, 1)
    success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/dynamodb.json")
    If success <> 1
        Debug "Could not load private AWS credentials."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"access_key") = "")
        Debug "Missing or invalid access_key."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"secret_key") = "")
        Debug "Missing or invalid secret_key."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    ;  Set this once to the region containing the table.
    region.s = "us-west-2"
    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(endpoint,"dynamodb.")
    CkStringBuilder::ckAppend(endpoint,region)
    CkStringBuilder::ckAppend(endpoint,".amazonaws.com")
    aws.i = CkAuthAws::ckCreate()
    If aws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(aws, CkJsonObject::ckStringOf(credentials,"access_key"))
    CkAuthAws::setCkSecretKey(aws, CkJsonObject::ckStringOf(credentials,"secret_key"))
    CkAuthAws::setCkRegion(aws, region)
    CkAuthAws::setCkServiceName(aws, "dynamodb")
    CkAuthAws::setCkSignatureVersion(aws, 4)
    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::setCkConnectTimeoutMs(rest, 20000)
    CkRest::setCkIdleTimeoutMs(rest, 60000)
    success = CkRest::ckSetAuthAws(rest,aws)
    If success <> 1
        Debug "Could not configure AWS signing."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Temporary IAM/STS credentials require their session token on every signed request.
    ;  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
    ;  Leave session_token absent or empty only for credentials that do not have one.
    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        If CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1
            Debug "session_token must be a string."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(credentials,"session_token") <> ""
            success = CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject::ckStringOf(credentials,"session_token"))
            If success <> 1
                Debug "Could not set the session-token parameter."
                CkJsonObject::ckDispose(credentials)
                CkStringBuilder::ckDispose(endpoint)
                CkAuthAws::ckDispose(aws)
                CkRest::ckDispose(rest)
                ProcedureReturn
            EndIf

        EndIf

    EndIf

    ;  Supply a Socket to explicitly enable TLS certificate verification.
    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    CkSocket::setCkMaxReadIdleMs(connection, 60000)
    CkSocket::setCkMaxSendIdleMs(connection, 60000)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
    If success <> 1
        Debug "TLS connection failed; inspect diagnostics privately."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkRest::ckUseConnection(rest,connection,1)
    If success <> 1
        Debug "Could not attach the TLS connection."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.0")
    If success <> 1
        Debug "Could not set the content type."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(result, 1)
    requestBody.i = CkStringBuilder::ckCreate()
    If requestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    responseBody.i = CkStringBuilder::ckCreate()
    If responseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    errorType.i = CkStringBuilder::ckCreate()
    If errorType.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    tableName.s = "ExampleItems"
    ;  Choose "status", "enable", or "expire-item". Expiration eventually deletes the item.
    action.s = "status"
    If (action <> "status") AND (action <> "enable") AND (action <> "expire-item")
        Debug "Choose status, enable, or expire-item."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    success = CkRest::ckAddHeader(rest,"X-Amz-Target","DynamoDB_20120810.DescribeTimeToLive")
    If success <> 1
        Debug "Could not set the DynamoDB operation."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"TableName",tableName)
    ;  EmitSb appends. Clear the request buffer before each request/page.
    CkStringBuilder::ckClear(requestBody)
    success = CkJsonObject::ckEmitSb(body,requestBody)
    If success <> 1
        Debug "Could not encode the request."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/",requestBody,responseBody)
    If success <> 1
        Debug "Request did not complete; inspect diagnostics privately."
        ;  A write may already have succeeded. Reconcile its outcome before retrying.
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "DynamoDB HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
        success = CkJsonObject::ckLoadSb(result,responseBody)
        If success = 1
            If CkJsonObject::ckJsonTypeOf(result,"__type") = 1
                CkStringBuilder::ckClear(errorType)
                CkStringBuilder::ckAppend(errorType,CkJsonObject::ckStringOf(result,"__type"))
                Debug "AWS error type: " + CkStringBuilder::ckGetAsString(errorType)
            EndIf

        EndIf

        ;  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
        ;  Keep full error messages, item data, and signed request diagnostics private.
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadSb(result,responseBody)
    If success <> 1
        Debug "Expected a well-formed JSON response."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"TimeToLiveDescription.TimeToLiveStatus") <> 1) OR (CkJsonObject::ckStringOf(result,"TimeToLiveDescription.TimeToLiveStatus") = "")
        Debug "Missing or invalid TimeToLiveDescription.TimeToLiveStatus."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    ttlStatus.s = CkJsonObject::ckStringOf(result,"TimeToLiveDescription.TimeToLiveStatus")
    ttlAttribute.s = ""
    If CkJsonObject::ckJsonTypeOf(result,"TimeToLiveDescription.AttributeName") = 1
        ttlAttribute = CkJsonObject::ckStringOf(result,"TimeToLiveDescription.AttributeName")
    EndIf

    Debug "TTL status: " + ttlStatus + "; attribute: " + ttlAttribute
    If action = "status"
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    If action = "enable"
        If (ttlStatus = "ENABLED") AND (ttlAttribute = "ExpiresAt")
            Debug "TTL is already enabled for ExpiresAt."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        If ttlStatus <> "DISABLED"
            Debug "TTL is enabled for another attribute or transitioning; no configuration change sent."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        CkJsonObject::ckClear(body)
        success = CkRest::ckAddHeader(rest,"X-Amz-Target","DynamoDB_20120810.UpdateTimeToLive")
        If success <> 1
            Debug "Could not set the DynamoDB operation."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        CkJsonObject::ckUpdateString(body,"TableName",tableName)
        CkJsonObject::ckUpdateString(body,"TimeToLiveSpecification.AttributeName","ExpiresAt")
        CkJsonObject::ckUpdateBool(body,"TimeToLiveSpecification.Enabled",1)
        ;  EmitSb appends. Clear the request buffer before each request/page.
        CkStringBuilder::ckClear(requestBody)
        success = CkJsonObject::ckEmitSb(body,requestBody)
        If success <> 1
            Debug "Could not encode the request."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        success = CkRest::ckFullRequestSb(rest,"POST","/",requestBody,responseBody)
        If success <> 1
            Debug "Request did not complete; inspect diagnostics privately."
            ;  A write may already have succeeded. Reconcile its outcome before retrying.
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        If CkRest::ckResponseStatusCode(rest) <> 200
            Debug "DynamoDB HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
            success = CkJsonObject::ckLoadSb(result,responseBody)
            If success = 1
                If CkJsonObject::ckJsonTypeOf(result,"__type") = 1
                    CkStringBuilder::ckClear(errorType)
                    CkStringBuilder::ckAppend(errorType,CkJsonObject::ckStringOf(result,"__type"))
                    Debug "AWS error type: " + CkStringBuilder::ckGetAsString(errorType)
                EndIf

            EndIf

            ;  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
            ;  Keep full error messages, item data, and signed request diagnostics private.
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        success = CkJsonObject::ckLoadSb(result,responseBody)
        If success <> 1
            Debug "Expected a well-formed JSON response."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            ProcedureReturn
        EndIf

        Debug "TTL enable request accepted; check status again later."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    If (ttlStatus <> "ENABLED") OR (ttlAttribute <> "ExpiresAt")
        Debug "Enable ExpiresAt TTL and wait until ENABLED before this demonstration."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    CkJsonObject::ckClear(body)
    success = CkRest::ckAddHeader(rest,"X-Amz-Target","DynamoDB_20120810.UpdateItem")
    If success <> 1
        Debug "Could not set the DynamoDB operation."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"TableName",tableName)
    ;  Both keys and their DynamoDB types must match the table schema.
    partitionKey.s = "group-1"
    sortKey.s = "item-001"
    CkJsonObject::ckUpdateString(body,"Key.PK.S",partitionKey)
    CkJsonObject::ckUpdateString(body,"Key.SK.S",sortKey)
    ;  UTC epoch seconds as a decimal string, not milliseconds and not a local date string.
    expires.i = CkDateTime::ckCreate()
    If expires.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkDateTime::ckSetFromCurrentSystemTime(expires)
    If success <> 1
        Debug "Could not read the current time."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        ProcedureReturn
    EndIf

    success = CkDateTime::ckAddDays(expires,7)
    If success <> 1
        Debug "Could not calculate expiration."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        ProcedureReturn
    EndIf

    expirySeconds.s = CkDateTime::ckGetAsUnixTimeStr(expires,0)
    expiryText.i = CkStringBuilder::ckCreate()
    If expiryText.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(expiryText,expirySeconds)
    If CkStringBuilder::ckLength(expiryText) = 0
        Debug "Could not format expiration seconds."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"UpdateExpression","SET #ttl = :expiry")
    CkJsonObject::ckUpdateString(body,"ConditionExpression","attribute_exists(#pk)")
    CkJsonObject::ckUpdateString(body,"ExpressionAttributeNames.#ttl","ExpiresAt")
    CkJsonObject::ckUpdateString(body,"ExpressionAttributeNames.#pk","PK")
    CkJsonObject::ckUpdateString(body,"ReturnValues","UPDATED_NEW")
    CkJsonObject::ckUpdateString(body,"ExpressionAttributeValues.:expiry.N",expirySeconds)
    ;  EmitSb appends. Clear the request buffer before each request/page.
    CkStringBuilder::ckClear(requestBody)
    success = CkJsonObject::ckEmitSb(body,requestBody)
    If success <> 1
        Debug "Could not encode the request."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/",requestBody,responseBody)
    If success <> 1
        Debug "Request did not complete; inspect diagnostics privately."
        ;  A write may already have succeeded. Reconcile its outcome before retrying.
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "DynamoDB HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
        success = CkJsonObject::ckLoadSb(result,responseBody)
        If success = 1
            If CkJsonObject::ckJsonTypeOf(result,"__type") = 1
                CkStringBuilder::ckClear(errorType)
                CkStringBuilder::ckAppend(errorType,CkJsonObject::ckStringOf(result,"__type"))
                Debug "AWS error type: " + CkStringBuilder::ckGetAsString(errorType)
                If CkStringBuilder::ckEndsWith(errorType,"ConditionalCheckFailedException",1) = 1
                    Debug "Condition failed; this request made no change. Re-read the item before deciding what to do."
                EndIf

            EndIf

        EndIf

        ;  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
        ;  Keep full error messages, item data, and signed request diagnostics private.
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadSb(result,responseBody)
    If success <> 1
        Debug "Expected a well-formed JSON response."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(result,"Attributes.ExpiresAt.N") <> 1) OR (CkJsonObject::ckStringOf(result,"Attributes.ExpiresAt.N") = "")
        Debug "Missing or invalid Attributes.ExpiresAt.N."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        CkDateTime::ckDispose(expires)
        CkStringBuilder::ckDispose(expiryText)
        ProcedureReturn
    EndIf

    Debug "Expiration epoch seconds: " + CkJsonObject::ckStringOf(result,"Attributes.ExpiresAt.N")
    Debug "TTL deletion is asynchronous; expiry is not an immediate delete."


    CkJsonObject::ckDispose(credentials)
    CkStringBuilder::ckDispose(endpoint)
    CkAuthAws::ckDispose(aws)
    CkRest::ckDispose(rest)
    CkSocket::ckDispose(connection)
    CkJsonObject::ckDispose(body)
    CkJsonObject::ckDispose(result)
    CkStringBuilder::ckDispose(requestBody)
    CkStringBuilder::ckDispose(responseBody)
    CkStringBuilder::ckDispose(errorType)
    CkDateTime::ckDispose(expires)
    CkStringBuilder::ckDispose(expiryText)


    ProcedureReturn
EndProcedure