DEV Community

Jon Ashdown
Jon Ashdown

Posted on

2 1

AWS Gotchas

AWS Gotchas

I will highlight AWS gotchas as I find them

Dynamodb

TTL Fields are in Seconds

TTL Fields on dynamodb are great, as they allow a grace period between marking a record for deletion and the actual deletion. Meaning that when you have finished with a record, you can set a TTL field to a time in the future and AWS internal mechanisms will remove the record at some point soon after the that time has passed.

Gotcha:

The TTL field is an integer in seconds from the Unix epoch, all other dates, including explicit date fields in dynamodb are stored as ISO dates, which are stored under the hood as microseconds from the Unix epoch. This is documented at Using DynamoDB Time to Live (TTL). However, it is easy to forget hence the reason for this gotcha.

Workaround:

Calculate the TTL using your standard date library, convert to integer and divide by 1000 to convert milliseconds to seconds.
e.g

import { DocumentClient } from 'aws-sdk/clients/dynamodb'
const dynamoDb = new DocumentClient()

const hoursInTheFuture = 24

const timeInSeconds = () => {
  const time = new Date()
  const hours = time.getHours() + hoursInTheFuture

  time.setHours(hours)

  return Math.round(time.getTime() / 1000)
}

export const deleteInTheFuture = async (id) => {
  const result = await dynamoDb.update({
    TableName: 'myDynamoDbTable',
    Key: {
      id
    },
    UpdateExpression: 'SET ttl = :t',
    ExpressionAttributeValues: {
      ':t': timeInSeconds() //record will be deleted momentarily from now
    }

  }).promise()

  return result
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay