Skip to content

Instantly share code, notes, and snippets.

@djg07
Created November 22, 2019 18:09
Show Gist options
  • Save djg07/301b24c519fcf537dee5567d477f784c to your computer and use it in GitHub Desktop.
Save djg07/301b24c519fcf537dee5567d477f784c to your computer and use it in GitHub Desktop.
import json
import boto3
from boto3.dynamodb.conditions import Key
def lambda_handler(event, context):
client = boto3.resource('dynamodb')
table = client.Table('Transactions')
#1. Example - Get Item By Id
response = table.get_item(
Key={
'TransactionType_OriginCountry': 'PURCHASE_USA',
'Date': '2019-11-17'
}
)
print(response['Item'])
print('\n\n\n-----\n\n\n')
#2. Example 2 - Query by Partition Key / Sort Key Criteria
response = table.query(
KeyConditionExpression=Key('TransactionType_OriginCountry').eq('PURCHASE_USA') & Key('Date').gt('2019-11-15')
)
items = response['Items']
for item in items:
print(item)
@chandanshiva
Copy link

[import json
import boto3
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):
client = boto3.resource('dynamodb')
table = client.Table('voters')

#1. Example - Get Item By Id

response = table.get_item(
	Key={
		"id" : event["id"],
	}
)

print(response['Item'])](url)

I'm getting error for this

Response:
{
"errorMessage": "An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema",
"errorType": "ClientError",
"stackTrace": [
" File "/var/task/lambda_function.py", line 11, in lambda_handler\n response = table.get_item(\n",
" File "/var/runtime/boto3/resources/factory.py", line 520, in do_action\n response = action(self, *args, **kwargs)\n",
" File "/var/runtime/boto3/resources/action.py", line 83, in call\n response = getattr(parent.meta.client, operation_name)(**params)\n",
" File "/var/runtime/botocore/client.py", line 272, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File "/var/runtime/botocore/client.py", line 576, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}

I have the table created with name 'voters' , partition key 'id' , I'm giving the key vales in the test configuration, but I'm getting this error.

I tried with returning the vale as well. I get the same error with this,

return(response['Item'])

@chandanshiva
Copy link

I checked by changing event["id"] to event.id
The get this error.

Response:
{
"errorMessage": "'dict' object has no attribute 'id'",
"errorType": "AttributeError",
"stackTrace": [
" File "/var/task/lambda_function.py", line 13, in lambda_handler\n "id" : event.id\n"
]
}

@gasconc
Copy link

gasconc commented Feb 26, 2020

[import json
import boto3
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):
client = boto3.resource('dynamodb')
table = client.Table('voters')

#1. Example - Get Item By Id

response = table.get_item(
	Key={
		"id" : event["id"],
	}
)

print(response['Item'])](url)

I'm getting error for this

Response:
{
"errorMessage": "An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema",
"errorType": "ClientError",
"stackTrace": [
" File "/var/task/lambda_function.py", line 11, in lambda_handler\n response = table.get_item(\n",
" File "/var/runtime/boto3/resources/factory.py", line 520, in do_action\n response = action(self, *args, **kwargs)\n",
" File "/var/runtime/boto3/resources/action.py", line 83, in call\n response = getattr(parent.meta.client, operation_name)(**params)\n",
" File "/var/runtime/botocore/client.py", line 272, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File "/var/runtime/botocore/client.py", line 576, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}

I have the table created with name 'voters' , partition key 'id' , I'm giving the key vales in the test configuration, but I'm getting this error.

I tried with returning the vale as well. I get the same error with this,

return(response['Item'])

Hi , did u resolve this ?

i have the same issue

@VinciGit00
Copy link

also me :(

@danocolombo
Copy link

I was getting the same error as above. Found out since I was using index on my table, I had to specify...

response = table.query(
IndexName='clientId-meetingDate-index',
KeyConditionExpression=Key('clientId').eq('abc') & Key('meetingDate').gte('2022-05-05')
)

Of course change to your index, and attribute values, but after adding IndexName, all worked well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment