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

Commit cb45965

Browse files
Melhorias para execução de script focados no build local das lambdas e tarefas relacionadas;
Troca de nome the tarefa de workflow build para sonar; Inclusão de exemplos de buildspec; Adição de exemplos de rotas de api no serverless; Inclusão de handler no projectrc dos projetos para o correto funcionamento das lambdas no localstack; Ajustes nos scripts para invocar lambdas;
1 parent 93ff6d9 commit cb45965

File tree

112 files changed

+2262
-22
lines changed

Some content is hidden

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

112 files changed

+2262
-22
lines changed
File renamed without changes.

examples/lambda_api/.projectrc

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ NETWORK_NAME=service-python
44
APP_QUEUE=test-queue
55
APP_LAMBDA_NAME=lambda_api
66
APP_LAMBDA_EVENT_SOURCE=false
7+
# APP_LAMBDA_HANDLER=app.APP
8+
APP_LAMBDA_HANDLER=localstack.handler
79
APP_PORT=5000
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: 0.2
2+
3+
phases:
4+
install:
5+
runtime-versions:
6+
python: 3.8
7+
pre_build:
8+
run-as: root
9+
commands:
10+
# Install Serverless Framework CLI and plugins
11+
- curl -L -o /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/v4.9.6/yq_linux_amd64 && chmod +x /usr/local/bin/yq
12+
- npm install -g serverless@2.72.2 $(yq eval '.plugins | join(" ")' serverless.yaml)
13+
- export IAC_VERSION="serverless-$(serverless --version | grep -i framework | cut -d ' ' -f 3)"
14+
- yq eval '.provider.stackTags.Iac = strenv(IAC_VERSION) | .custom.cloudWatchLogsTags.Iac = strenv(IAC_VERSION)' --inplace serverless.yaml
15+
build:
16+
commands:
17+
- pip install --upgrade pip
18+
# Install project dependencies locally
19+
- pip install -r ./requirements.txt -t .
20+
- pip install -r ./requirements-vendor.txt -t ./vendor
21+
post_build:
22+
commands:
23+
# Install test requirements
24+
# - pip install -r ./requirements-tests.txt
25+
# # Run unit tests
26+
# - ./bin/tests/unit-coverage-no-report-html.sh
27+
# # Clean vendor folder (let the zip light because the layers)
28+
# # - rm -Rf ./vendor/*
29+
# # Uninstall test dependencies
30+
# - pip uninstall -r requirements-tests.txt -y
31+
# Deploy application
32+
- serverless deploy --verbose --stage $ENVIRONMENT_NAME
33+
#reports:
34+
# UnitTestsReports:
35+
# files:
36+
# - 'target/unit/clover.xml'
37+
# file-format: 'CLOVERXML'

examples/lambda_api/flambda_app/repositories/v1/mysql/product_repository.py

+3
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,6 @@ def soft_delete(self, value, key=None):
250250
self._close()
251251

252252
return result
253+
254+
def delete(self):
255+
pass

examples/lambda_api/localstack.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import app
2+
import serverless_wsgi
3+
4+
def handler(event, context):
5+
"""Lambda event handler, invokes the WSGI wrapper and handles command invocation"""
6+
return serverless_wsgi.handle_request(app.APP, event, context)

examples/lambda_api/requirements-tests.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
serverless-wsgi
21
unittest-data-provider
32
coverage
43
coverage2clover

examples/lambda_api/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
serverless-wsgi
12
python-dotenv
23
jsonformatter
34
requests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"body": "",
3+
"resource": "/{proxy+}",
4+
"path": "/alive",
5+
"httpMethod": "GET",
6+
"isBase64Encoded": true,
7+
"queryStringParameters": {
8+
},
9+
"multiValueQueryStringParameters": {
10+
},
11+
"pathParameters": {
12+
"proxy": "/alive"
13+
},
14+
"stageVariables": {
15+
},
16+
"headers": {
17+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
18+
"Accept-Encoding": "gzip, deflate, sdch",
19+
"Accept-Language": "en-US,en;q=0.8",
20+
"Cache-Control": "max-age=0",
21+
"CloudFront-Forwarded-Proto": "https",
22+
"CloudFront-Is-Desktop-Viewer": "true",
23+
"CloudFront-Is-Mobile-Viewer": "false",
24+
"CloudFront-Is-SmartTV-Viewer": "false",
25+
"CloudFront-Is-Tablet-Viewer": "false",
26+
"CloudFront-Viewer-Country": "US",
27+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
28+
"Upgrade-Insecure-Requests": "1",
29+
"User-Agent": "Custom User Agent String",
30+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
31+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
32+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
33+
"X-Forwarded-Port": "443",
34+
"X-Forwarded-Proto": "https"
35+
},
36+
"multiValueHeaders": {
37+
"Accept": [
38+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
39+
],
40+
"Accept-Encoding": [
41+
"gzip, deflate, sdch"
42+
],
43+
"Accept-Language": [
44+
"en-US,en;q=0.8"
45+
],
46+
"Cache-Control": [
47+
"max-age=0"
48+
],
49+
"CloudFront-Forwarded-Proto": [
50+
"https"
51+
],
52+
"CloudFront-Is-Desktop-Viewer": [
53+
"true"
54+
],
55+
"CloudFront-Is-Mobile-Viewer": [
56+
"false"
57+
],
58+
"CloudFront-Is-SmartTV-Viewer": [
59+
"false"
60+
],
61+
"CloudFront-Is-Tablet-Viewer": [
62+
"false"
63+
],
64+
"CloudFront-Viewer-Country": [
65+
"US"
66+
],
67+
"Host": [
68+
"0123456789.execute-api.us-east-1.amazonaws.com"
69+
],
70+
"Upgrade-Insecure-Requests": [
71+
"1"
72+
],
73+
"User-Agent": [
74+
"Custom User Agent String"
75+
],
76+
"Via": [
77+
"1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)"
78+
],
79+
"X-Amz-Cf-Id": [
80+
"cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA=="
81+
],
82+
"X-Forwarded-For": [
83+
"127.0.0.1, 127.0.0.2"
84+
],
85+
"X-Forwarded-Port": [
86+
"443"
87+
],
88+
"X-Forwarded-Proto": [
89+
"https"
90+
]
91+
},
92+
"requestContext": {
93+
"accountId": "123456789012",
94+
"resourceId": "123456",
95+
"stage": "prod",
96+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
97+
"requestTime": "09/Apr/2015:12:34:56 +0000",
98+
"requestTimeEpoch": 1428582896000,
99+
"identity": {
100+
"cognitoIdentityPoolId": null,
101+
"accountId": null,
102+
"cognitoIdentityId": null,
103+
"caller": null,
104+
"accessKey": null,
105+
"sourceIp": "127.0.0.1",
106+
"cognitoAuthenticationType": null,
107+
"cognitoAuthenticationProvider": null,
108+
"userArn": null,
109+
"userAgent": "Custom User Agent String",
110+
"user": null
111+
},
112+
"path": "/alive",
113+
"resourcePath": "/{proxy+}",
114+
"httpMethod": "GET",
115+
"apiId": "1234567890",
116+
"protocol": "HTTP/1.1"
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "11d6ee51-4cc7-4302-9e22-7cd8afdaadf5",
5+
"receiptHandle": "AQEBBX8nesZEXmkhsmZeyIE8iQAMig7qw...",
6+
"body": "{\n \"chavenfe\": \"32210206107255000134550010001712551245826554\",\n \"ocor\": \"MOTIVO DO CANCELAMENTO\",\n \"origem\": \"SAC/EAGLE\",\n \"pedido\": \"Z1223321\"\n}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1573251510774",
10+
"SequenceNumber": "18849496460467696128",
11+
"MessageGroupId": "1",
12+
"SenderId": "AIDAIO23YVJENQZJOL4VO",
13+
"MessageDeduplicationId": "1",
14+
"ApproximateFirstReceiveTimestamp": "1573251510774"
15+
},
16+
"messageAttributes": {},
17+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
18+
"eventSource": "aws:sqs",
19+
"eventSourceARN": "arn:aws:sqs:us-east-2:000000000000:dummy-delivery-time-simulator-queue.fifo",
20+
"awsRegion": "us-east-2"
21+
}
22+
]
23+
}

examples/lambda_api/scripts/boot-lambda.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Boot Lambda
4+
# Version: 1.0.0
5+
# **************************
26
# -----------------------------------------------------------------------------
37
# Current file variables
48
# -----------------------------------------------------------------------------
@@ -58,6 +62,10 @@ if test -f ${current_parent_folder}requirements-vendor.txt; then
5862
echo "requirements vendor..."
5963
fi
6064

65+
echo 'Flask compatibility with Python 3.8'
66+
python3 -m pip uninstall dataclasses -y
67+
rm -Rf ${current_parent_folder}vendor/dataclasses-0.8.dist-info/ ${current_parent_folder}vendor/dataclasses.py
68+
6169
read -p "Press enter to continue..."
6270

6371
#echo 'Creating resource dependencies...'
@@ -76,7 +84,7 @@ if test -f "${current_parent_folder}scripts/localstack/lambda/create-function-fr
7684
echo '----------------------------------------'
7785
echo "$0 - Creating the lambda: $APP_LAMBDA_NAME"
7886
echo '----------------------------------------'
79-
${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh $current_filename_path $APP_LAMBDA_NAME
87+
${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh $current_filename_path $APP_LAMBDA_NAME $APP_LAMBDA_HANDLER
8088

8189
read -p "Press enter to continue..."
8290

examples/lambda_api/scripts/localstack/lambda/create-event-source-mapping.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Create Event Source Mapping Tool
4+
# Version: 1.0.0
5+
# **************************
26
if [ -z "$1" ]; then
37
echo 'Function name must be informed'
48
exit 1

examples/lambda_api/scripts/localstack/lambda/create-function-from-s3.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Create Function From S3 Tool
4+
# Version: 1.0.0
5+
# **************************
26
# -----------------------------------------------------------------------------
37
# Current file variables
48
# -----------------------------------------------------------------------------
@@ -105,7 +109,7 @@ else
105109
echo '----------------------------------------'
106110
LAST_PWD=$(pwd)
107111
cd ${FUNCTION_PATH}
108-
zip -r ./lambda-full.zip ./ -x '*.git*' -x "./zip.sh*" -x "./venv/*" -x "./.idea/*" -x "./lambda-full.zip"
112+
zip -q -r ./lambda-full.zip ./ -x '*.git*' -x "./zip.sh*" -x "./venv/*" -x "./.idea/*" -x "./lambda-full.zip"
109113
echo "zip file created in ${FUNCTION_PATH}lambda-full.zip"
110114
cd ${LAST_PWD}
111115

examples/lambda_api/scripts/localstack/lambda/create-layer-from-vendor.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Create Layer From Vendor Tool
4+
# Version: 1.0.0
5+
# **************************
26
# https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/
37
if [ -z "$1" ]; then
48
echo 'Function path must be informed'

examples/lambda_api/scripts/localstack/lambda/create-layer.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Create Layer Tool
4+
# Version: 1.0.0
5+
# **************************
26
# https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/
37
if [ -z "$1" ]; then
48
echo 'Function path must be informed'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Invoke API Function Tool
4+
# Version: 1.0.0
5+
# **************************
6+
if [ -z "$1" ]; then
7+
echo 'Function name must be informed'
8+
exit 1
9+
else
10+
11+
if [ $RUNNING_IN_CONTAINER ]; then
12+
HOST=localstack
13+
else
14+
HOST=0.0.0.0
15+
fi
16+
FUNCTION_PATH=$1
17+
FUNCTION_NAME=$1
18+
if test -d ./$FUNCTION_PATH; then
19+
PAYLOAD=./$FUNCTION_PATH/samples/localstack/api_request_sample.json
20+
else
21+
PAYLOAD=./samples/localstack/api_request_sample.json
22+
fi
23+
24+
25+
echo "Function name: $FUNCTION_NAME"
26+
echo "Function path: $FUNCTION_PATH"
27+
echo "Function ARN arn:aws:lambda:us-east-1:000000000000:$FUNCTION_NAME"
28+
29+
echo "aws --endpoint-url=http://$HOST:4566 lambda invoke \
30+
--function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \
31+
--invocation-type RequestResponse \
32+
--payload file://$PAYLOAD ./output/response.json \
33+
--log-type Tail --query 'LogResult' --output text | base64 -d"
34+
35+
if ! test -d ./output; then
36+
echo 'creating dir ./output'
37+
mkdir ./output
38+
fi
39+
40+
aws --endpoint-url=http://$HOST:4566 lambda invoke \
41+
--function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \
42+
--invocation-type RequestResponse \
43+
--payload file://$PAYLOAD ./output/response.json \
44+
--log-type Tail --query 'LogResult' --output text | base64 -d
45+
46+
echo "\nResponse"
47+
echo 'cat ./output/response.json'
48+
cat ./output/response.json
49+
50+
# echo $PAYLOAD
51+
# cat $PAYLOAD
52+
fi
53+

examples/lambda_api/scripts/localstack/lambda/invoke-function.sh

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
# **************************
3+
# Localstack Lambda Invoke API Function Tool
4+
# Version: 1.0.0
5+
# **************************
26
if [ -z "$1" ]; then
37
echo 'Function name must be informed'
48
exit 1
@@ -26,6 +30,11 @@ else
2630
--payload $PAYLOAD ./output/response.json \
2731
--log-type Tail --query 'LogResult' --output text | base64 -d"
2832

33+
if ! test -d ./output; then
34+
echo 'creating dir ./output'
35+
mkdir ./output
36+
fi
37+
2938
aws --endpoint-url=http://$HOST:4566 lambda invoke \
3039
--function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \
3140
--payload "$PAYLOAD" ./output/response.json \

0 commit comments

Comments
 (0)