Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit dc82442

Browse files
Merge pull request #16 from madeiramadeirabr/lambda_sqs_light
Lambda sqs light
2 parents cd9a926 + ac4d0db commit dc82442

File tree

156 files changed

+7997
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+7997
-108
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ Example of architecture of this project stack.
1010
Example of components of the architecture of this project.
1111
![Service-Stack](docs/service-stack.png)
1212

13-
## Single projects
14-
You can find single examples for:
13+
## Build environment script workflow
14+
Example of the workflow to create the environment.
15+
![Service-Stack](docs/runenv-workflow.drawio.png)
16+
17+
## Single project examples
18+
19+
You can find light examples:
20+
* [Lambda API Light](./examples/lambda_api_light)
21+
* [Lambda CRON Light](./examples/lambda_cron_light)
22+
* [Lambda SQS Light](./examples/lambda_sqs_light)
23+
* [Lambda SNS Light](./examples/lambda_sns_light)
24+
* [Lambda S3 Light](./examples/lambda_s3_light)
25+
*
26+
You can find complex examples:
1527
* [Lambda API](./examples/lambda_api)
28+
* [Lambda API RESTful](./examples/lambda_api_restful)
1629
* [Lambda CRON](./examples/lambda_cron)
1730
* [Lambda SQS](./examples/lambda_sqs)
1831
* [Lambda SNS](./examples/lambda_sns)

docs/runenv-workflow.drawio.png

77 KB
Loading

examples/lambda_api/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ config/integration.en
6060
/node_modules/
6161

6262
/lambda-full.zip
63+
64+
#output lambda
65+
output/response.json
66+
/output/
67+
/include/

examples/lambda_api/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
# template-serverless-lambda-python - Lambda API
1+
# Lambda API
22
Template for build flexible API with AWS Lambda.
33

44
## Service Architecture
55
Example of architecture with Kong API Gateway.
66
![Service-Arch](docs/service-arch.png)
77

8+
## Build environment script workflow
9+
Example of the workflow to create the environment.
10+
![Service-Stack](docs/runenv-workflow.drawio.png)
11+
812
## General Service Routes Architecture
913
Service routes.
1014
```

examples/lambda_api/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import boot
66
import os
77
import base64
8-
from lambda_app.services.v1.healthcheck import HealthCheckSchema
8+
from lambda_app.services.v1.healthcheck import HealthCheckSchema, HealthCheckResult
99
from lambda_app.services.v1.healthcheck.resources import \
1010
MysqlConnectionHealthCheck, RedisConnectionHealthCheck, \
1111
SQSConnectionHealthCheck, SelfConnectionHealthCheck
@@ -85,6 +85,7 @@ def alive():
8585
LOGGER, CONFIG), ["redis"])
8686
service.add_check("queue", SQSConnectionHealthCheck(
8787
LOGGER, CONFIG), ["queue"])
88+
service.add_check("internal", lambda: HealthCheckResult.unhealthy("connect"), ["example"])
8889

8990
return service.get_response()
9091

77 KB
Loading
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import ast
2+
import json
3+
4+
from lambda_app import helper
5+
from lambda_app.decorators import SQSEvent
6+
from lambda_app.decorators.events import SQSRecord
7+
from lambda_app.logging import get_logger
8+
9+
10+
def read_event(record, logger=None):
11+
if logger is None:
12+
logger = get_logger()
13+
logger.info('try to reading event form record: {}'.format(record))
14+
logger.info('Getting type of data: {}'.format(type(record)))
15+
try:
16+
logger.info('dump: {}'.format(json.dumps(record)))
17+
except Exception as err:
18+
logger.error(err)
19+
event_body = None
20+
try:
21+
if isinstance(record, dict):
22+
try:
23+
event_body = json.loads(record['body'])
24+
except Exception as err:
25+
logger.error(err)
26+
unescaped_str = ast.literal_eval(record['body'])
27+
event_body = json.loads(unescaped_str)
28+
elif isinstance(record, str):
29+
record = json.loads(record)
30+
event_body = json.loads(record.body)
31+
elif isinstance(record.body, str):
32+
event_body = json.loads(record.body)
33+
else:
34+
event_body = record.body
35+
except Exception as err:
36+
logger.error(err)
37+
logger.info('event_body: {}'.format(event_body))
38+
return event_body
39+
40+
41+
def get_records_from_sqs_event(sqs_event, logger=None):
42+
if logger is None:
43+
logger = get_logger()
44+
records = []
45+
try:
46+
if isinstance(sqs_event, SQSEvent):
47+
logger.info("SQSEvent instance")
48+
if not helper.empty(sqs_event.to_dict()):
49+
try:
50+
sqs_event_dict = sqs_event.to_dict()
51+
if 'Records' in sqs_event_dict:
52+
sqs_event_dict = sqs_event_dict['Records']
53+
for record in sqs_event_dict:
54+
records.append(record)
55+
except Exception as err:
56+
logger.error(err)
57+
records.append(sqs_event.to_dict())
58+
elif isinstance(sqs_event, SQSRecord):
59+
logger.info("SQSRecord instance")
60+
if not helper.empty(sqs_event.to_dict()):
61+
records.append(sqs_event)
62+
except Exception as err:
63+
logger.error(err)
64+
if isinstance(sqs_event, SQSEvent) or isinstance(sqs_event, SQSRecord):
65+
logger.error(sqs_event.__dict__)
66+
else:
67+
try:
68+
logger.error(json.dumps(sqs_event))
69+
except Exception as err:
70+
logger.error(err)
71+
logger.error(str(sqs_event))
72+
return records

examples/lambda_api/lambda_app/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def to_dict(obj, force_str=False):
7777

7878

7979
def to_json(obj):
80-
return json.dumps(obj)
80+
return json.dumps(obj, default=str)
8181

8282

8383
def debug_mode():

examples/lambda_api/scripts/boot-db.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1-
python3 -m venv venv
2-
source ./venv/bin/activate
3-
bash $1
1+
#!/bin/bash
2+
current_path=$(basename $(pwd))
3+
current_filename=$(basename -- "$0")
4+
current_filename_path=$0
5+
# echo $current_filename_path
6+
current_filename_path="${current_filename_path/$current_filename/''}"
7+
# echo $current_filename_path
8+
current_filename_path="${current_filename_path/scripts\//''}"
9+
# echo $current_filename_path
10+
current_filename_path_basename=$(basename -- "$current_filename_path")
11+
12+
echo "current_path: $current_path"
13+
echo "current_filename: $current_filename"
14+
echo "current_filename_path: $current_filename_path"
15+
echo "current_filename_path_basename: $current_filename_path_basename"
16+
17+
if test -f "${current_filename_path}venv/bin/activate"; then
18+
python3 -m venv venv
19+
source ${current_filename_path}venv/bin/activate
20+
else
21+
echo "Unable to find ${current_filename_path}venv/bin/activate"
22+
exit 1
23+
fi
24+
25+
if test -f "$1"; then
26+
bash $1
27+
else
28+
echo "Unable to find $1"
29+
exit 1
30+
fi

0 commit comments

Comments
 (0)