Skip to content

Commit 54546d3

Browse files
authored
Do trigger with ALB instead of API GW (#19)
1 parent 4d1146a commit 54546d3

Some content is hidden

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

63 files changed

+1757
-1076
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ max_line_length = 0
2323
trim_trailing_whitespace = false
2424

2525
[*.py]
26-
max_line_length = 120
26+
max_line_length = 200
2727
indent_style = space
2828
indent_size = 4
2929
ignore_frosted_errors = E103

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[flake8]
22
ignore = W504, F401
3-
max-line-length = 150
3+
exclude = tests/*
4+
max-line-length = 200
45
max-complexity = 18
56
select = B,C,E,F,W,T4,B9

.github/workflows/main.yml

+18-40
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,41 @@
1-
# https://github.com/yildizozan/tenserflow-lambda/blob/2c29535b9af0a396ca13e53aada10e46b0579791/.github/workflows/deploy.yml
2-
# https://github.com/destination-unknown/destination-unknown-backend-serverless/blob/master/.github/workflows/main.yml
3-
# https://github.com/search?utf8=%E2%9C%93&q=extension%3Ayml+path%3A.github%2Fworkflows%2F+serverless+python&type=Code&ref=advsearch&l=&l=
4-
5-
name: Verify and autodeploy to staging environment
1+
name: Autodeploy to dev environment
62

73
on:
84
push:
95
branches:
10-
- deploy-alb-external
116
- master
127

138
jobs:
149
deploy:
1510
runs-on: ubuntu-latest
1611

1712
steps:
18-
- uses: actions/checkout@v1
13+
- uses: actions/checkout@v2
1914

2015
- name: Set up Python 3.7
21-
uses: actions/setup-python@v1
16+
uses: actions/setup-python@v1.1.1
2217
with:
2318
python-version: 3.7
2419

25-
- name: Install dependencies
20+
- name: Install Python dependencies
2621
run: |
2722
python -m pip install --upgrade pip
2823
pip install --user pipenv
29-
# PATH="$PATH:/home/runner/.local/bin"
30-
# pipenv install
31-
# - name: Update backend
32-
# run: |
33-
# PATH="$PATH:/home/runner/.local/bin"
34-
# pipenv run python import_data/main.py
35-
# env:
36-
# GOOGLE_SHEETS_API_KEY: ${{ secrets.GOOGLE_SHEETS_API_KEY }}
37-
# - uses: stefanzweifel/[email protected]
38-
# with:
39-
# commit_message: Update backend
40-
# branch: master
41-
# env:
42-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43-
# - name: Setup Node.js for use with actions
44-
# uses: actions/[email protected]
45-
# with:
46-
# version: 10.x
47-
# - name: Install Serverless and run deploy
48-
# run: |
49-
# npm install -g serverless
50-
# yarn install
51-
# PATH="$PATH:/home/runner/.local/bin"
52-
# serverless deploy
53-
# env:
54-
# SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
55-
56-
57-
- name: serverless deploy
58-
uses: serverless/[email protected]
24+
25+
- uses: actions/[email protected]
5926
with:
60-
args: deploy
27+
node-version: 13
28+
29+
- name: Install Serverless Framework and other project dependencies
30+
run: |
31+
npm install -g serverless
32+
npm install
33+
pip install -r requirements.txt
34+
35+
- name: Deploy Lambda function to dev
36+
run: |
37+
PATH="$PATH:/home/runner/.local/bin"
38+
serverless deploy --stage dev --force --verbose
6139
env:
6240
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
6341
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
hooks:
55
- id: isort
66
- repo: git://github.com/pre-commit/pre-commit-hooks
7-
rev: v2.4.0
7+
rev: v2.5.0
88
hooks:
99
- id: check-ast
1010
- id: check-merge-conflict

DEVELOPMENT.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ As a short cheatsheet, you will need to use these commands:
66

77
```
88
# dev
9-
$ http --print Hhb --all --follow https://dev-lambda.modules.tf/ @input/blueprint_my.json
9+
$ http --print Hhb --all --follow https://dev.modules.tf/ @input/blueprint_my.json
1010
1111
# prod
12-
$ http --print Hhb --all --follow https://lambda.modules.tf/ @input/blueprint_my.json
12+
$ http --print Hhb --all --follow https://prod.modules.tf/ @input/blueprint_my.json
1313
```
1414

1515
* Invoke function locally providing `input.json`:

KNOWN_ISSUES.md

-3
This file was deleted.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ In [modules.tf-demo](https://github.com/antonbabenko/modules.tf-demo) repository
3737

3838
This project was partially sponsored by [Cloudcraft - the best way to draw AWS diagrams](https://cloudcraft.co).<br clear="all">
3939

40-
Monitoring of serverless applications provided by [Dashbird.io](https://dashbird.io/). <a href="https://dashbird.io/" target="_blank"><img src="https://raw.githubusercontent.com/antonbabenko/modules.tf-lambda/master/misc/dashbird-logo.png" alt="Dashbird - Monitor serverless applications" width="200" height="68" align="right" /></a><br clear="all">
40+
Monitoring of serverless applications provided by [Thundra](https://www.thundra.io/).
4141

4242
[Become a sponsor to @antonbabenko on GitHub](https://github.com/sponsors/antonbabenko/).
4343

handler.py

+84-16
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,86 @@ def load_data(event):
6060
return data
6161

6262

63+
def validation_result(config):
64+
return True
65+
66+
6367
def handler(event, context):
6468
link = ""
6569

66-
try:
67-
data = load_data(event)
68-
except ValueError as error:
69-
logger.error(error)
70+
# ALB response is different:
71+
# https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#respond-to-load-balancer
72+
request_from_lb = bool(event.get("requestContext", {}).get("elb"))
73+
http_method = event.get("httpMethod")
74+
is_validate_action = event.get("path") == "/validate"
7075

76+
if http_method == "OPTIONS":
7177
return {
72-
"body": error.args[0],
78+
"isBase64Encoded": False,
79+
"statusCode": 200,
80+
"statusDescription": "200 OK",
7381
"headers": {
82+
"Content-Type": "application/json",
7483
"Access-Control-Allow-Origin": "*",
75-
"Access-Control-Allow-Credentials": True
84+
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with",
85+
"Access-Control-Allow-Methods": "POST,GET,OPTIONS",
86+
# "Access-Control-Allow-Credentials": True # this header is not allowed by ALB
87+
},
88+
}
89+
90+
if request_from_lb and event.get("path") == "/healthz":
91+
return {
92+
"isBase64Encoded": False,
93+
"statusCode": 200,
94+
"statusDescription": "200 OK",
95+
"headers": {
96+
"Content-Type": "text/html"
7697
},
77-
"statusCode": error.args[1],
98+
"body": "Health OK"
7899
}
79100

101+
try:
102+
data = load_data(event)
103+
except ValueError as error:
104+
logger.error(error)
105+
106+
if request_from_lb:
107+
return {
108+
"isBase64Encoded": False,
109+
"statusCode": error.args[1],
110+
"statusDescription": str(error.args[1]) + " Server Error",
111+
"headers": {
112+
"Content-Type": "text/html",
113+
},
114+
"body": error.args[0],
115+
}
116+
else:
117+
return {
118+
"body": error.args[0],
119+
"headers": {
120+
"Access-Control-Allow-Origin": "*",
121+
"Access-Control-Allow-Credentials": True
122+
},
123+
"statusCode": error.args[1],
124+
}
125+
80126
# logger.info(pformat(data, indent=2))
81127

82128
graph = populate_graph(data)
83129

84130
config = convert_graph_to_modulestf_config(graph)
85131

132+
if is_validate_action:
133+
return {
134+
"isBase64Encoded": False,
135+
"statusCode": 200,
136+
"statusDescription": "200 OK",
137+
"headers": {
138+
"Content-Type": "application/json",
139+
},
140+
"body": json.dumps(validation_result(config))
141+
}
142+
86143
prepare_render_dirs()
87144

88145
render_from_modulestf_config(config, source=graph.source, regions=graph.regions)
@@ -93,12 +150,23 @@ def handler(event, context):
93150

94151
link = upload_file_to_s3(filename="archive.zip")
95152

96-
return {
97-
"body": "",
98-
"headers": {
99-
"Location": link,
100-
"Access-Control-Allow-Origin": "*",
101-
"Access-Control-Allow-Credentials": True
102-
},
103-
"statusCode": 302,
104-
}
153+
if request_from_lb:
154+
return {
155+
"isBase64Encoded": False,
156+
"headers": {
157+
"Location": link,
158+
"Access-Control-Allow-Origin": "*",
159+
},
160+
"statusCode": 302,
161+
"statusDescription": "302 Found",
162+
}
163+
else:
164+
return {
165+
"body": "",
166+
"headers": {
167+
"Location": link,
168+
"Access-Control-Allow-Origin": "*",
169+
"Access-Control-Allow-Credentials": True
170+
},
171+
"statusCode": 302,
172+
}

misc/ipexpo-demo-tfvars/autoscaling/terraform.tfvars

-49
This file was deleted.

misc/ipexpo-demo-tfvars/elb/terraform.tfvars

-46
This file was deleted.

misc/modulestf-logo2.png

6.73 KB
Loading

0 commit comments

Comments
 (0)