-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathlambda_function.py
More file actions
47 lines (34 loc) · 1.3 KB
/
Copy pathlambda_function.py
File metadata and controls
47 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import boto3
import json
import os
import logging
from boto3.dynamodb.conditions import Key
# CAMBIOOOOOOOOO
logger = logging.getLogger()
logger.setLevel(os.environ.get('LOG_LEVEL', 'INFO'))
DYNAMO_BD = os.environ['DYNAMO_BD']
DYNAMO_KEY = os.environ.get('DYNAMO_KEY', 'cc')
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(DYNAMO_BD)
def lambda_handler(event, context):
try:
body = event if isinstance(event, dict) and DYNAMO_KEY in event else json.loads(event.get('body', '{}'))
cc = body.get(DYNAMO_KEY)
if not cc:
return response(400, {'error': f'Campo "{DYNAMO_KEY}" es requerido'})
result = table.query(KeyConditionExpression=Key(DYNAMO_KEY).eq(str(cc)))
items = result.get('Items', [])
if not items:
return response(404, {'error': 'Registro no encontrado'})
return response(200, items[0])
except json.JSONDecodeError:
return response(400, {'error': 'JSON inválido en el body'})
except Exception as e:
logger.error(f'Error: {e}')
return response(500, {'error': 'Error interno del servidor'})
def response(status_code, body):
return {
'statusCode': status_code,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(body, default=str)
}