lambda_decorators
includes the following decorators to avoid boilerplate
for common usecases when using AWS Lambda with Python.
async_handler
- support for async handlerscors_headers
- automatic injection of CORS headersdump_json_body
- auto-serialization of http body to JSONload_json_body
- auto-deserialize of http body from JSONjson_http_resp
- automatic serialization of python object to HTTP JSON responsejson_schema_validator
- use JSONSchema to validate request&response payloadsload_urlencoded_body
- auto-deserialize of http body from a querystring encoded bodyno_retry_on_failure
- detect and stop retry attempts for scheduled lambdasssm_parameter_store
- fetch parameters from the AWS SSM Parameter Storesecret_manager
- fetch secrets from the AWS Secrets Managerlog_resquest_response
- log request and response
pip install Log-Api
from aws_handler_decorators import load_json_body
@load_json_body
def handler(event, context):
# event['body'] is now a python object
return event['body']['foo']
handler({'body': '{"foo": "bar"}'}, object())
You can use to validate the request. The schema is a JSON object or a path to a JSON file.
from aws_handler_decorators import json_schema_validator
@json_schema_validator(request_schema={
'type': 'object',
'properties': {
'username': {'type': 'string'}
},
'required': ['username']
})
def handler(event, context):
# event['body'] is now a python object
return event['body']['username']
handler({'body': '{"username": "f4b14n"}'}, object())
@json_schema_validator(request_schema='path/to/schema.json', in_file=True)
def handler(event, context):
# event['body'] is now a python object
return event['body']['username']
handler({'body': '{"username": "f4b14n"}'}, object())
I was inspired by dschep <https://github.com/dschep>
_
- Python 3.6+
- A secret for database connection in AWS Secrets Manager with the following structure:
stage/dbr{APP}
(stage is the environment, for example: dev, qa, prod)
stage/dbw{APP}
(stage is the environment, for example: dev, qa, prod)
APP
(name of the application)
Example: dev/dbr-myapp, pdn/dbw-myapp
The APP is equal to -myapp and environment variable must be set in the serverless environment section.
{
"username": "user",
"password": "password",
"host": "host",
"port": "port",
"bd_name": "database"
}
CREATE TABLE `LOG_APIS` (
`ID` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'Id del registro',
`USERNAME` VARCHAR(15) DEFAULT NULL COMMENT 'Nombre del usuario que consume el servicio',
`PATH` VARCHAR(50) NOT NULL COMMENT 'Ruta de la api',
`DOMAIN_NAME` VARCHAR(200) DEFAULT NULL COMMENT 'Host de la api',
`METHOD` VARCHAR(10) NOT NULL COMMENT 'Metodo de solicitud',
`STATUS_CODE` SMALLINT DEFAULT NULL COMMENT 'Codigo de estado de la respuesta',
`HEADERS_RESPONSE` LONGTEXT COMMENT 'Cabecera de la respuesta',
`BODY_RESPONSE` LONGTEXT COMMENT 'Payload de la respuesta',
`HEADERS` LONGTEXT COMMENT 'Cabecera de la solicitud',
`BODY` LONGTEXT COMMENT 'Payload de la solicitud',
`QUERY_STR_PARAMETERS` TEXT COMMENT 'Query string parameters',
`PATH_PARAMETERS` TEXT,
`COOKIES` TEXT,
`RAW_QUERY_STR` TEXT,
`REQUEST_CONTEXT` LONGTEXT,
`AWS_CONTEXT` TEXT,
`IP` VARCHAR(15) NOT NULL COMMENT 'Ip del cliente',
`USER_AGENT` TEXT,
`TIME` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'Fecha en la que se ejecuto la api',
`VERSION` VARCHAR(50) DEFAULT NULL COMMENT 'Version de la respuesta',
`CREATED_AT` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Fecha de creacion del registro',
`UPDATED_AT` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Fecha en la que se actualizo el registro',
PRIMARY KEY (`ID`)
) ENGINE=INNODB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
from Log_Api import log_resquest_response
@log_resquest_response be to used before the other decorators like this:
json_schema_validator