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

Commit 2ca7b51

Browse files
Correcoes no client SQS;
Ajustes nos scripts de interação com o Localstack;
1 parent 10e3556 commit 2ca7b51

File tree

12 files changed

+64
-34
lines changed

12 files changed

+64
-34
lines changed

examples/lambda_api/lambda_app/events/aws/sqs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def connect(self, retry=False):
2525
try:
2626
endpoint_url = self.config.SQS_ENDPOINT
2727
profile = os.environ['AWS_PROFILE'] if 'AWS_PROFILE' in os.environ else None
28+
queue_name = os.path.basename(os.environ['APP_QUEUE']) if 'APP_QUEUE' in os.environ else None
2829
self.logger.info('SQSEvents - profile: {}'.format(profile))
2930
self.logger.info('SQSEvents - endpoint_url: {}'.format(endpoint_url))
31+
self.logger.info('SQSEvents - queue_name: {}'.format(queue_name))
3032
self.logger.info('SQSEvents - self.config.REGION_NAME: {}'.format(self.config.REGION_NAME))
33+
3134
if profile:
3235
session = boto3.session.Session(profile_name=profile)
3336
connection = session.resource(
@@ -43,10 +46,11 @@ def connect(self, retry=False):
4346
)
4447

4548
try:
46-
connection.get_queue_by_name(QueueName='test')
49+
connection.get_queue_by_name(QueueName=queue_name)
4750
except Exception as err:
4851
if helper.has_attr(err, "response") and err.response['Error']:
4952
self.logger.info('SQSEvents - Connected')
53+
self.logger.error(err)
5054
else:
5155
raise err
5256

@@ -66,7 +70,9 @@ def connect(self, retry=False):
6670
_RETRY_COUNT += 1
6771
# Fix para tratar diff entre docker/local
6872
if self.config.SQS_ENDPOINT == 'http://0.0.0.0:4566' or self.config.SQS_ENDPOINT == 'http://localstack:4566':
69-
self.config.DB_HOST = 'http://localhost:4566'
73+
old_value = self.config.SQS_ENDPOINT
74+
self.config.SQS_ENDPOINT = 'http://localhost:4566'
75+
self.logger.info('Changing the endpoint from {} to {}'.format(old_value, self.config.SQS_ENDPOINT))
7076
connection = self.connect(retry=True)
7177
return connection
7278

examples/lambda_api/scripts/localstack/sqs/receive-message.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
QUEUE=$1
22
if [ -z "$QUEUE" ]
33
then
4-
QUEUE='http://localhost:4566/000000000000/test'
4+
QUEUE='http://localhost:4566/000000000000/test-queue'
5+
else
6+
QUEUE=$(basename -- $QUEUE)
7+
QUEUE="http://localhost:4566/000000000000/${QUEUE}"
58
fi
69

710
echo "aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url $QUEUE"
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
QUEUE=$1
22
if [ -z "$QUEUE" ]
33
then
4-
QUEUE='http://localhost:4566/000000000000/test'
4+
QUEUE='http://localhost:4566/000000000000/test-queue'
5+
else
6+
QUEUE=$(basename -- $QUEUE)
7+
QUEUE="http://localhost:4566/000000000000/${QUEUE}"
58
fi
69
MESSAGE=$2
710
if [ -z "$MESSAGE" ]
811
then
9-
#MESSAGE='test message'
10-
MESSAGE=$(cat tests/datasources/events/sourcing-service-decision-processor/sample.json)
12+
MESSAGE=$(cat samples/localstack/sqs/sample.json)
1113
fi
12-
14+
#echo $QUEUE
15+
#exit
1316
echo "aws --endpoint-url=http://localhost:4566 sqs send-message --queue-url $QUEUE --message-body '$MESSAGE'"
1417
aws --endpoint-url=http://localhost:4566 sqs send-message --queue-url $QUEUE --message-body "'$MESSAGE'"

examples/lambda_sqs/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Example of architecture with AWS SQS and AWS Lambda.
99
There are no routes for this project.
1010

1111
# Prerequisites
12-
- Python 3.6
12+
- Python 3.6 (Attention)
1313
- docker
1414
- docker-compose
1515
- python-dotenv
@@ -54,6 +54,11 @@ Execute the follow command:
5454
```
5555

5656
### Running via docker
57+
Installing dependencies:
58+
```
59+
./scripts/venv.sh
60+
```
61+
5762
To execute the build:
5863
```
5964
./scripts/runenv.sh --build

examples/lambda_sqs/docker-compose.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
version: "3.2"
22
services:
3-
# template-serverless-lambda-python-lambda-sqs:
4-
# container_name: lambda-sqs
5-
# build:
6-
# context: .
7-
# dockerfile: ./docker/python/Dockerfile
8-
# privileged: true
9-
# environment:
10-
# # Fake credentials for Localstack
11-
# AWS_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE
12-
# AWS_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
13-
# ports:
14-
# - 5001:5000
15-
# networks:
16-
# - service-python
17-
# volumes:
18-
# - ./:/app
193
redis:
204
image: "redis:alpine"
215
ports:
@@ -24,6 +8,7 @@ services:
248
- 6379
259
networks:
2610
- service-python
11+
# hostname: redis
2712
mysql:
2813
image: mysql:5.7.22
2914
ports:
@@ -49,6 +34,7 @@ services:
4934
LAMBDA_EXECUTOR: local
5035
DEBUG: 1
5136
HOSTNAME_EXTERNAL: localstack
37+
hostname: localstack
5238
networks:
5339
- service-python
5440
volumes:

examples/lambda_sqs/lambda_app/events/aws/sqs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def connect(self, retry=False):
2525
try:
2626
endpoint_url = self.config.SQS_ENDPOINT
2727
profile = os.environ['AWS_PROFILE'] if 'AWS_PROFILE' in os.environ else None
28+
queue_name = os.path.basename(os.environ['APP_QUEUE']) if 'APP_QUEUE' in os.environ else None
2829
self.logger.info('SQSEvents - profile: {}'.format(profile))
2930
self.logger.info('SQSEvents - endpoint_url: {}'.format(endpoint_url))
31+
self.logger.info('SQSEvents - queue_name: {}'.format(queue_name))
3032
self.logger.info('SQSEvents - self.config.REGION_NAME: {}'.format(self.config.REGION_NAME))
33+
3134
if profile:
3235
session = boto3.session.Session(profile_name=profile)
3336
connection = session.resource(
@@ -43,10 +46,11 @@ def connect(self, retry=False):
4346
)
4447

4548
try:
46-
connection.get_queue_by_name(QueueName='test')
49+
connection.get_queue_by_name(QueueName=queue_name)
4750
except Exception as err:
4851
if helper.has_attr(err, "response") and err.response['Error']:
4952
self.logger.info('SQSEvents - Connected')
53+
self.logger.error(err)
5054
else:
5155
raise err
5256

@@ -66,7 +70,9 @@ def connect(self, retry=False):
6670
_RETRY_COUNT += 1
6771
# Fix para tratar diff entre docker/local
6872
if self.config.SQS_ENDPOINT == 'http://0.0.0.0:4566' or self.config.SQS_ENDPOINT == 'http://localstack:4566':
69-
self.config.DB_HOST = 'http://localhost:4566'
73+
old_value = self.config.SQS_ENDPOINT
74+
self.config.SQS_ENDPOINT = 'http://localhost:4566'
75+
self.logger.info('Changing the endpoint from {} to {}'.format(old_value, self.config.SQS_ENDPOINT))
7076
connection = self.connect(retry=True)
7177
return connection
7278

examples/lambda_sqs/lambda_app/services/v1/carrier_notifier_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def process(self, sqs_event):
3535
for record in records:
3636
process_counter += 1
3737
event = self._read_event(record)
38+
if event is None:
39+
raise Exception('Event is None')
3840
event_hash = event['hash'] if 'hash' in event else generate_hash(event)
3941

4042
self.logger.info('Event: {}'.format(event))
File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"chavenfe": "32210206107255000134550010001712551245826554",
3+
"ocor": "MOTIVO DO CANCELAMENTO",
4+
"origem": "SAC/EAGLE",
5+
"pedido": "Z1223321"
6+
}

examples/lambda_sqs/scripts/localstack/sqs/receive-message.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
QUEUE=$1
22
if [ -z "$QUEUE" ]
33
then
4-
QUEUE='http://localhost:4566/000000000000/test'
4+
QUEUE='http://localhost:4566/000000000000/test-queue'
5+
else
6+
QUEUE=$(basename -- $QUEUE)
7+
QUEUE="http://localhost:4566/000000000000/${QUEUE}"
58
fi
69

710
echo "aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url $QUEUE"

0 commit comments

Comments
 (0)