title: How to insert to DynamoDb just if the key does not exist
body: <p>I want to add id + some values to a DynamoDb just once. If the id exists already it should do nothing or update </p>
  
  <p>I can go with </p>
  
  <pre><code>search 
  
  if not found &gt; insert
  
  if found &gt; do nothing or update (for now do nothing is fine)
  </code></pre>
  
  <p>But hopfully there is a better way to do it. The id should be the key to check for.</p>
  
  <p>That's the code in node:</p>
  
  <pre><code>const dynamodbParams = {
          TableName: process.env.DYNAMODB_TABLE_BLICKANALYTICS,
          Item: {
            id: userId,
            createdAt: timestamp
          },
        };
  
        dynamoDb.put(dynamodbParams).promise()
        .then(data =&gt; {
          console.log('saved: ', dynamodbParams);
        })
        .catch(err =&gt; {
          console.error(err);
        });  
  </code></pre>
  
  <p>I use this in yml. Don't know if there are options to set this up in yml</p>
  
  <pre><code>resources:
    Resources:
      DynamoDbTableExpenses:
        Type: 'AWS::DynamoDB::Table'
        DeletionPolicy: Retain
        Properties:
          AttributeDefinitions:
            -
              AttributeName: id
              AttributeType: S
            -  
              AttributeName: createdAt
              AttributeType: N
          KeySchema:
            -
              AttributeName: id
              KeyType: HASH
            -
              AttributeName: createdAt
              KeyType: RANGE            
          ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
          TableName: ${self:provider.environment.DYNAMODB_TABLE_BLICKANALYTICS}
  
  </code></pre>