73

Following the documentation, I'm trying to create an update statement that will update or add if not exists only one attribute in a dynamodb table.

I'm trying this

response = table.update_item(
    Key={'ReleaseNumber': '1.0.179'},
    UpdateExpression='SET',
    ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')',
    ExpressionAttributeNames={'attr1': 'val1'},
    ExpressionAttributeValues={'val1': 'false'}
)

The error I'm getting is:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "attr1"

If anyone has done anything similar to what I'm trying to achieve please share example.

Dmitry R
  • 2,956
  • 4
  • 25
  • 45
  • I know is a very old question, but I'm wondering.... if you item already exist the "put_item" will overwrite it. Why should I use update_item if I can't change any index_key ? – Claudiu Sep 07 '21 at 12:53
  • @Claudiu I guess to update _not all_ fields – mccc Nov 29 '21 at 12:32

10 Answers10

85

Found working example here, very important to list as Keys all the indexes of the table, this will require additional query before update, but it works.

response = table.update_item(
    Key={
        'ReleaseNumber': releaseNumber,
        'Timestamp': result[0]['Timestamp']
    },
    UpdateExpression="set Sanity = :r",
    ExpressionAttributeValues={
        ':r': 'false',
    },
    ReturnValues="UPDATED_NEW"
)
Dmitry R
  • 2,956
  • 4
  • 25
  • 45
  • 1
    It should be noted that your original error was referring to ExpressionAttributeNames which has been excluded from the sample provided in this answer.... while you do have to include all the values for the key to update an item this was not directly the error you originally had. – James Mar 23 '16 at 03:44
  • 1
    Worth mentioning, `Key` here should have all the keys for that `index` whether `Local Secondary Index` or `Global Secondary Index`. As here, `ReleaseNumber` is `primary key` and `Timestamp` is a `sort key` by default. – Abhishake Gupta Jun 28 '19 at 17:48
  • 1
    This solution did not worked for me - I had to add `ExpressionAttributeNames` along with `ExpressionAttributeValues` while updating the item. – AADProgramming Oct 24 '20 at 14:51
  • i tried the above but didn't work. tried using `ExpressionAttributeNames` but still didn't work. Getting the error `botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema` – Harsh Math Dec 03 '20 at 19:43
  • This accepted answer does not work. Edit queue for this answer is full, hence unable to edit. Another answer below should be marked as accepted instead, which uses "ExpressionAttributeNames". https://stackoverflow.com/a/53201316/5600195 – Utkarsh Feb 05 '22 at 23:38
50

Details on dynamodb updates using boto3 seem incredibly sparse online, so I'm hoping these alternative solutions are useful.

get / put

import boto3

table = boto3.resource('dynamodb').Table('my_table')

# get item
response = table.get_item(Key={'pkey': 'asdf12345'})
item = response['Item']

# update
item['status'] = 'complete'

# put (idempotent)
table.put_item(Item=item)

actual update

import boto3

table = boto3.resource('dynamodb').Table('my_table')

table.update_item(
    Key={'pkey': 'asdf12345'},
    AttributeUpdates={
        'status': 'complete',
    },
)
ryantuck
  • 6,146
  • 10
  • 57
  • 71
  • 9
    Although using AttributeUpdates like this is correct and should still work (and I like the syntax more than UpdateExpression) , the documentation mentions that this method is legacy (and therefore might not work anymore at some point in time). Source: https://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Table.update_item – Aart Goossens Apr 25 '18 at 12:17
  • AttributeUpdates is a legacy param in boto3, so probably better to use UpdateExpression in Dmitry R's answer above – gimbel0893 May 23 '18 at 14:26
  • 18
    AttributeUpdates seems like a much cleaner API. :( – Andy Hayden Jun 25 '18 at 17:26
  • @AndyHayden AttributeUpdates is simpler, but Expressions are more powerful because they can be parameterized for handling reserved word collisions, code injection and other issues. – Davos Nov 08 '18 at 03:56
  • 1
    The fact that they're taking AttributeUpdates away pisses me off significantly. – Captain Jack Sparrow Oct 14 '22 at 20:09
  • you have no idea how the first get/put method was useful for me! – ERJAN Jan 09 '23 at 09:48
28

If you don't want to check parameter by parameter for the update I wrote a cool function that would return the needed parameters to perform a update_item method using boto3.

def get_update_params(body):
    """Given a dictionary we generate an update expression and a dict of values
    to update a dynamodb table.

    Params:
        body (dict): Parameters to use for formatting.

    Returns:
        update expression, dict of values.
    """
    update_expression = ["set "]
    update_values = dict()

    for key, val in body.items():
        update_expression.append(f" {key} = :{key},")
        update_values[f":{key}"] = val

    return "".join(update_expression)[:-1], update_values

Here is a quick example:

def update(body):
    a, v = get_update_params(body)
    response = table.update_item(
        Key={'uuid':str(uuid)},
        UpdateExpression=a,
        ExpressionAttributeValues=dict(v)
        )
    return response
22

The original code example:

response = table.update_item(
    Key={'ReleaseNumber': '1.0.179'},
    UpdateExpression='SET',
    ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')',
    ExpressionAttributeNames={'attr1': 'val1'},
    ExpressionAttributeValues={'val1': 'false'}
)

Fixed:

response = table.update_item(
    Key={'ReleaseNumber': '1.0.179'},
    UpdateExpression='SET #attr1 = :val1',
    ConditionExpression=Attr('ReleaseNumber').eq('1.0.179'),
    ExpressionAttributeNames={'#attr1': 'val1'},
    ExpressionAttributeValues={':val1': 'false'}
)

In the marked answer it was also revealed that there is a Range Key so that should also be included in the Key. The update_item method must seek to the exact record to be updated, there's no batch updates, and you can't update a range of values filtered to a condition to get to a single record. The ConditionExpression is there to be useful to make updates idempotent; i.e. don't update the value if it is already that value. It's not like a sql where clause.

Regarding the specific error seen.

ExpressionAttributeNames is a list of key placeholders for use in the UpdateExpression, useful if the key is a reserved word.

From the docs, "An expression attribute name must begin with a #, and be followed by one or more alphanumeric characters". The error is because the code hasn't used an ExpressionAttributeName that starts with a # and also not used it in the UpdateExpression.

ExpressionAttributeValues are placeholders for the values you want to update to, and they must start with :

Davos
  • 5,066
  • 42
  • 66
  • 1
    I have tried all other options mentioned in other posts on this but none of them work if we don't mention `ExpressionAttributeNames` - thanks! – AADProgramming Oct 24 '20 at 14:50
  • The documentation suggests that values for eg Key should be specified as Key={'ReleaseNumber': {'S': '1.0.179'}} – MikeW May 31 '23 at 09:50
5

Based on the official example, here's a simple and complete solution which could be used to manually update (not something I would recommend) a table used by a terraform S3 backend.

Let's say this is the table data as shown by the AWS CLI:

$ aws dynamodb scan --table-name terraform_lock --region us-east-1
{
    "Items": [
        {
            "Digest": {
                "S": "2f58b12ae16dfb5b037560a217ebd752"
            },
            "LockID": {
                "S": "tf-aws.tfstate-md5"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

You could update it to a new digest (say you rolled back the state) as follows:

import boto3

dynamodb = boto3.resource('dynamodb', 'us-east-1')


try:
    table = dynamodb.Table('terraform_lock')
    response = table.update_item(
        Key={
            "LockID": "tf-aws.tfstate-md5"
        },
        UpdateExpression="set Digest=:newDigest",
        ExpressionAttributeValues={
            ":newDigest": "50a488ee9bac09a50340c02b33beb24b"
        },
        ReturnValues="UPDATED_NEW"
    )
except Exception as msg:
    print(f"Oops, could not update: {msg}")

Note the : at the start of ":newDigest": "50a488ee9bac09a50340c02b33beb24b" they're easy to miss or forget.

Nagev
  • 10,835
  • 4
  • 58
  • 69
3

Small update of Jam M. Hernandez Quiceno's answer, which includes ExpressionAttributeNames to prevent encoutering errors such as:

"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: 
Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: timestamp",

def get_update_params(body):
    """
    Given a dictionary of key-value pairs to update an item with in DynamoDB,
    generate three objects to be passed to UpdateExpression, ExpressionAttributeValues, 
    and ExpressionAttributeNames respectively.
    """
    update_expression = []
    attribute_values = dict()
    attribute_names = dict()

    for key, val in body.items():
        update_expression.append(f" #{key.lower()} = :{key.lower()}")
        attribute_values[f":{key.lower()}"] = val
        attribute_names[f"#{key.lower()}"] = key

    return "set " + ", ".join(update_expression), attribute_values, attribute_names

Example use:

update_expression, attribute_values, attribute_names = get_update_params(
    {"Status": "declined", "DeclinedBy": "username"}
)

response = table.update_item(
    Key={"uuid": "12345"},
    UpdateExpression=update_expression,
    ExpressionAttributeValues=attribute_values,
    ExpressionAttributeNames=attribute_names,
    ReturnValues="UPDATED_NEW"
)
print(response)
Florian
  • 24,425
  • 4
  • 49
  • 80
2

Simple example with multiple fields:

import boto3
dynamodb_client = boto3.client('dynamodb')

dynamodb_client.update_item(
    TableName=table_name,
    Key={
        'PK1': {'S': 'PRIMARY_KEY_VALUE'},
        'SK1': {'S': 'SECONDARY_KEY_VALUE'}
    }
    UpdateExpression='SET #field1 = :field1, #field2 = :field2',
    ExpressionAttributeNames={
        '#field1': 'FIELD_1_NAME',
        '#field2': 'FIELD_2_NAME',
    },
    ExpressionAttributeValues={
        ':field1': {'S': 'FIELD_1_VALUE'},
        ':field2': {'S': 'FIELD_2_VALUE'},
    }
)
James Shapiro
  • 4,805
  • 3
  • 31
  • 46
1

An example to update any number of attributes given as a dict, and keep track of the number of updates. Works with reserved words (i.e name).

The following attribute names shouldn't be used as we will overwrite the value: _inc, _start.

from typing import Dict
from boto3 import Session


def getDynamoDBSession(region: str = "eu-west-1"):
    """Connect to DynamoDB resource from boto3."""
    return Session().resource("dynamodb", region_name=region)


DYNAMODB = getDynamoDBSession()


def updateItemAndCounter(db_table: str, item_key: Dict, attributes: Dict) -> Dict:
    """
    Update item or create new. If the item already exists, return the previous value and
    increase the counter: update_counter.
    """
    table = DYNAMODB.Table(db_table)

    # Init update-expression
    update_expression = "SET"

    # Build expression-attribute-names, expression-attribute-values, and the update-expression
    expression_attribute_names = {}
    expression_attribute_values = {}
    for key, value in attributes.items():
        update_expression += f' #{key} = :{key},'  # Notice the "#" to solve issue with reserved keywords
        expression_attribute_names[f'#{key}'] = key
        expression_attribute_values[f':{key}'] = value

    # Add counter start and increment attributes
    expression_attribute_values[':_start'] = 0
    expression_attribute_values[':_inc'] = 1

    # Finish update-expression with our counter
    update_expression += " update_counter = if_not_exists(update_counter, :_start) + :_inc"

    return table.update_item(
        Key=item_key,
        UpdateExpression=update_expression,
        ExpressionAttributeNames=expression_attribute_names,
        ExpressionAttributeValues=expression_attribute_values,
        ReturnValues="ALL_OLD"
    )

Hope it might be useful to someone!

eltbus
  • 435
  • 1
  • 7
  • 15
1

In a simple way you can use below code to update item value with new one:

    response = table.update_item(
       Key={"my_id_name": "my_id_value"}, # to get record
            
       UpdateExpression="set item_key_name=:item_key_value", # Operation action (set)
       ExpressionAttributeValues={":value": "new_value"}, # item that you need to update
       
       ReturnValues="UPDATED_NEW" # optional for declarative message
       )
Islam Salah
  • 1,691
  • 1
  • 6
  • 7
-1

using previous answer from eltbus , it worked for me , except for minor bug,

You have to delete the extra comma using update_expression[:-1]

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 23 '21 at 12:39