TIM02144
TIM02144

Reputation: 615

Update DynamoDB Table then get new value

I'm new to Python. I have a bit of code in Python within a Lambda function that updates the a value in a DynamoDB table (ebsDaysToExpire). That works. I get stuck when I want to the get that new updated value so I can pass it later in the script as part of a send_mail function.

I've tried adding in response = table.get_item statements but I just can't get that to work.

            if response['Count'] == 0: #volume not being tracked in table
                try:
                    response = table.put_item(
                        Item={
                            'volID': vid,
                            'ebsDaysToExpire': 7,
                            'snapshotStatus': 'incomplete',
                            'snapshotDate': 'incomplete',
                            'lifecycleStatus': 'start_7',
                            'snapshotID': 'incomplete',
                            'snapshotDaysToExpire': '30'
                        },
                        ConditionExpression='attribute_not_exists(volID)'
                        )
                except ClientError as e:
                    print(e.response['Error']['Message'])
            else:
                try:
                    response = table.update_item(
                        Key={
                            'volID': vid
                        },
                        UpdateExpression='set ebsDaysToExpire = ebsDaysToExpire + :val',
                        ExpressionAttributeValues={
                            ':val': decimal.Decimal(-1)
                        },
                        ReturnValues='UPDATED_NEW'
                    )
                except ClientError as e:
                    print(e.response['Error']['Message'])

Upvotes: 0

Views: 1654

Answers (1)

TIM02144
TIM02144

Reputation: 615

This is what my code looks like now and this returns the new value from the DynamoDB table after the 'table_put.item' updates the table (the returned value). This is getting passed as 'xdays'. Thanks to ohlr for his assistance.

            if response['Count'] == 0: #volume not being tracked in table
                try:
                    response = table.put_item(
                        Item={
                            'volID': vid,
                            'ebsDaysToExpire': 7,
                            'snapshotStatus': 'incomplete',
                            'snapshotDate': 'incomplete',
                            'lifecycleStatus': 'start_7',
                            'snapshotID': 'incomplete',
                            'snapshotDaysToExpire': '30'
                        },
                        ConditionExpression='attribute_not_exists(volID)'
                        )
                except ClientError as e:
                    print(e.response['Error']['Message'])
            else:
                try:
                    response = table.update_item(
                        Key={
                            'volID': vid
                        },
                        UpdateExpression='set ebsDaysToExpire = ebsDaysToExpire + :val',
                        ExpressionAttributeValues={
                            ':val': decimal.Decimal(-1)
                        },
                        ReturnValues='UPDATED_NEW'
                    )
                    xdays = response['Attributes']['ebsDaysToExpire']
                    print xdays
                except ClientError as e:
                    print(e.response['Error']['Message'])

Upvotes: 1

Related Questions