Sample code for 30+ languages & platforms
SQL Server 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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  Private configuration, not a file in public web content or source control.
    --  Load current IAM credentials authorized for the DynamoDB operation.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @credentials, 'Strict', 1
    EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/dynamodb.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private AWS credentials.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid access_key.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    --  Set this once to the region containing the table.
    DECLARE @region nvarchar(4000)
    SELECT @region = 'us-west-2'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'dynamodb.'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
    DECLARE @aws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT

    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
    EXEC sp_OASetProperty @aws, 'Region', @region
    EXEC sp_OASetProperty @aws, 'ServiceName', 'dynamodb'
    EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure AWS signing.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    --  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.
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'session_token must be a string.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            RETURN
          END
        EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
        IF @sTmp0 <> ''
          BEGIN
            EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not set the session-token parameter.'
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                RETURN
              END
          END
      END
    --  Supply a Socket to explicitly enable TLS certificate verification.
    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
    EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
    IF @success <> 1
      BEGIN

        PRINT 'TLS connection failed; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
    IF @success <> 1
      BEGIN

        PRINT 'Could not attach the TLS connection.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.0'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the content type.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END

    DECLARE @body int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT

    DECLARE @result int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT

    EXEC sp_OASetProperty @result, 'Strict', 1
    DECLARE @requestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @requestBody OUT

    DECLARE @responseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @responseBody OUT

    DECLARE @errorType int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @errorType OUT

    DECLARE @tableName nvarchar(4000)
    SELECT @tableName = 'ExampleItems'
    --  Choose "status", "enable", or "expire-item". Expiration eventually deletes the item.
    DECLARE @action nvarchar(4000)
    SELECT @action = 'status'
    IF (@action <> 'status') and (@action <> 'enable') and (@action <> 'expire-item')
      BEGIN

        PRINT 'Choose status, enable, or expire-item.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.DescribeTimeToLive'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the DynamoDB operation.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
    --  EmitSb appends. Clear the request buffer before each request/page.
    EXEC sp_OAMethod @requestBody, 'Clear', NULL
    EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
    IF @success <> 1
      BEGIN

        PRINT 'Could not encode the request.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
    IF @success <> 1
      BEGIN

        PRINT 'Request did not complete; inspect diagnostics privately.'
        --  A write may already have succeeded. Reconcile its outcome before retrying.
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'DynamoDB HTTP status: ' + @iTmp0
        EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
        IF @success = 1
          BEGIN
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
            IF @iTmp0 = 1
              BEGIN
                EXEC sp_OAMethod @errorType, 'Clear', NULL
                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0

                EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
                PRINT 'AWS error type: ' + @sTmp0
              END
          END
        --  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
        --  Keep full error messages, item data, and signed request diagnostics private.
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
    IF @success <> 1
      BEGIN

        PRINT 'Expected a well-formed JSON response.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'TimeToLiveDescription.TimeToLiveStatus'
    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'TimeToLiveDescription.TimeToLiveStatus'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid TimeToLiveDescription.TimeToLiveStatus.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    DECLARE @ttlStatus nvarchar(4000)
    EXEC sp_OAMethod @result, 'StringOf', @ttlStatus OUT, 'TimeToLiveDescription.TimeToLiveStatus'
    DECLARE @ttlAttribute nvarchar(4000)
    SELECT @ttlAttribute = ''
    EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'TimeToLiveDescription.AttributeName'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @result, 'StringOf', @ttlAttribute OUT, 'TimeToLiveDescription.AttributeName'
      END


    PRINT 'TTL status: ' + @ttlStatus + '; attribute: ' + @ttlAttribute
    IF @action = 'status'
      BEGIN
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    IF @action = 'enable'
      BEGIN
        IF (@ttlStatus = 'ENABLED') and (@ttlAttribute = 'ExpiresAt')
          BEGIN

            PRINT 'TTL is already enabled for ExpiresAt.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        IF @ttlStatus <> 'DISABLED'
          BEGIN

            PRINT 'TTL is enabled for another attribute or transitioning; no configuration change sent.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        EXEC sp_OAMethod @body, 'Clear', NULL
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.UpdateTimeToLive'
        IF @success <> 1
          BEGIN

            PRINT 'Could not set the DynamoDB operation.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TimeToLiveSpecification.AttributeName', 'ExpiresAt'
        EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'TimeToLiveSpecification.Enabled', 1
        --  EmitSb appends. Clear the request buffer before each request/page.
        EXEC sp_OAMethod @requestBody, 'Clear', NULL
        EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
        IF @success <> 1
          BEGIN

            PRINT 'Could not encode the request.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
        IF @success <> 1
          BEGIN

            PRINT 'Request did not complete; inspect diagnostics privately.'
            --  A write may already have succeeded. Reconcile its outcome before retrying.
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'DynamoDB HTTP status: ' + @iTmp0
            EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
            IF @success = 1
              BEGIN
                EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
                IF @iTmp0 = 1
                  BEGIN
                    EXEC sp_OAMethod @errorType, 'Clear', NULL
                    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                    EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0

                    EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
                    PRINT 'AWS error type: ' + @sTmp0
                  END
              END
            --  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
            --  Keep full error messages, item data, and signed request diagnostics private.
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END
        EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
        IF @success <> 1
          BEGIN

            PRINT 'Expected a well-formed JSON response.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @errorType
            RETURN
          END

        PRINT 'TTL enable request accepted; check status again later.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    IF (@ttlStatus <> 'ENABLED') or (@ttlAttribute <> 'ExpiresAt')
      BEGIN

        PRINT 'Enable ExpiresAt TTL and wait until ENABLED before this demonstration.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @body, 'Clear', NULL
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.UpdateItem'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the DynamoDB operation.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        RETURN
      END
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
    --  Both keys and their DynamoDB types must match the table schema.
    DECLARE @partitionKey nvarchar(4000)
    SELECT @partitionKey = 'group-1'
    DECLARE @sortKey nvarchar(4000)
    SELECT @sortKey = 'item-001'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Key.PK.S', @partitionKey
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Key.SK.S', @sortKey
    --  UTC epoch seconds as a decimal string, not milliseconds and not a local date string.
    DECLARE @expires int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @expires OUT

    EXEC sp_OAMethod @expires, 'SetFromCurrentSystemTime', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Could not read the current time.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @expires, 'AddDays', @success OUT, 7
    IF @success <> 1
      BEGIN

        PRINT 'Could not calculate expiration.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    DECLARE @expirySeconds nvarchar(4000)
    EXEC sp_OAMethod @expires, 'GetAsUnixTimeStr', @expirySeconds OUT, 0
    DECLARE @expiryText int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @expiryText OUT

    EXEC sp_OAMethod @expiryText, 'Append', @success OUT, @expirySeconds
    EXEC sp_OAGetProperty @expiryText, 'Length', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'Could not format expiration seconds.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UpdateExpression', 'SET #ttl = :expiry'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ConditionExpression', 'attribute_exists(#pk)'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpressionAttributeNames.#ttl', 'ExpiresAt'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpressionAttributeNames.#pk', 'PK'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ReturnValues', 'UPDATED_NEW'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpressionAttributeValues.:expiry.N', @expirySeconds
    --  EmitSb appends. Clear the request buffer before each request/page.
    EXEC sp_OAMethod @requestBody, 'Clear', NULL
    EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
    IF @success <> 1
      BEGIN

        PRINT 'Could not encode the request.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END
    EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
    IF @success <> 1
      BEGIN

        PRINT 'Request did not complete; inspect diagnostics privately.'
        --  A write may already have succeeded. Reconcile its outcome before retrying.
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'DynamoDB HTTP status: ' + @iTmp0
        EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
        IF @success = 1
          BEGIN
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
            IF @iTmp0 = 1
              BEGIN
                EXEC sp_OAMethod @errorType, 'Clear', NULL
                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0

                EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
                PRINT 'AWS error type: ' + @sTmp0
                EXEC sp_OAMethod @errorType, 'EndsWith', @iTmp0 OUT, 'ConditionalCheckFailedException', 1
                IF @iTmp0 = 1
                  BEGIN

                    PRINT 'Condition failed; this request made no change. Re-read the item before deciding what to do.'
                  END
              END
          END
        --  Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
        --  Keep full error messages, item data, and signed request diagnostics private.
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END
    EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
    IF @success <> 1
      BEGIN

        PRINT 'Expected a well-formed JSON response.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END
    EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Attributes.ExpiresAt.N'
    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Attributes.ExpiresAt.N'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid Attributes.ExpiresAt.N.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        EXEC @hr = sp_OADestroy @errorType
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @expiryText
        RETURN
      END

    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Attributes.ExpiresAt.N'
    PRINT 'Expiration epoch seconds: ' + @sTmp0

    PRINT 'TTL deletion is asynchronous; expiry is not an immediate delete.'

    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @aws
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @requestBody
    EXEC @hr = sp_OADestroy @responseBody
    EXEC @hr = sp_OADestroy @errorType
    EXEC @hr = sp_OADestroy @expires
    EXEC @hr = sp_OADestroy @expiryText


END
GO