Skip to content

Commit 6f131c4

Browse files
ran-isenbergRan Isenberg
andauthored
feature: sync with aws cookbook 4.5.1 (#23)
--------- Co-authored-by: Ran Isenberg <ran.isenberg@ranthebuilder.cloud>
1 parent d59474b commit 6f131c4

Some content is hidden

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

41 files changed

+3356
-305
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Ran Isenberg
3+
Copyright (c) 2024 Ran Isenberg
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
This project can serve as a cookiecutter template for new Serverless services - CDK deployment code, pipeline and handler are covered with best practices built in.
1313
<br></br>
14-
The project is based on my [AWS Lambda Cookbook template project](https://github.com/ran-isenberg/aws-lambda-handler-cookbook):
14+
The project is based on my [AWS Lambda Cookbook template project](https://github.com/ran-isenberg/aws-lambda-handler-cookbook) and synced to version 4.5.1:
1515

1616
[![license](https://img.shields.io/github/license/ran-isenberg/aws-lambda-handler-cookbook)](https://github.com/ran-isenberg/aws-lambda-handler-cookbook/blob/master/LICENSE)
1717
![PythonSupport](https://img.shields.io/static/v1?label=python&message=3.11&color=blue?style=flat-square&logo=python)
@@ -81,15 +81,18 @@ The documentation provides information about CDK deployment, makefile commands,
8181
- Python Serverless service with a recommended file structure.
8282
- CDK infrastructure with infrastructure tests and security tests.
8383
- CI/CD pipelines based on Github actions that deploys to AWS with python linters, complexity checks and style formatters.
84-
- CI/CD pipeline deploys to dev/staging and production environment with different gates between each environment
84+
- CI/CD pipeline deploys to dev/staging and production environments with different gates between each environment
8585
- Makefile for simple developer experience.
8686
- The AWS Lambda handler embodies Serverless best practices and has all the bells and whistles for a proper production ready handler.
8787
- AWS Lambda handler uses [AWS Lambda Powertools](https://docs.powertools.aws.dev/lambda-python/).
8888
- AWS Lambda handler 3 layer architecture: handler layer, logic layer and data access layer
8989
- Features flags and configuration based on AWS AppConfig
9090
- Idempotent API
91+
- REST API protected by WAF with four AWS managed rules in production deployment
9192
- CloudWatch dashboards - High level and low level including CloudWatch alarms
9293
- Unit, infrastructure, security, integration and end to end tests.
94+
- Automatically generated OpenAPI endpoint: /swagger with Pydnatic schemas for both requests and responses
95+
- CI swagger protection - fails the PR if your swagger JSON file (stored at docs/swagger/openapi.json) is out of date
9396
<br></br>
9497

9598
## CDK Deployment

{{cookiecutter.repo_name}}/.flake8

Lines changed: 0 additions & 5 deletions
This file was deleted.

{{cookiecutter.repo_name}}/.github/workflows/codeql-analysis.yml.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ jobs:
4343

4444
steps:
4545
- name: Checkout repository
46-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
46+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
4747

4848
# Initializes the CodeQL tools for scanning.
4949
- name: Initialize CodeQL
50-
uses: github/codeql-action/init@v2
50+
uses: github/codeql-action/init@v3
5151
with:
5252
languages: ${{ matrix.language }}
5353
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -60,7 +60,7 @@ jobs:
6060
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
6161
# If this step fails, then you should remove it and run the build manually (see below)
6262
- name: Autobuild
63-
uses: github/codeql-action/autobuild@v2
63+
uses: github/codeql-action/autobuild@v3
6464

6565
# ℹ️ Command-line programs to run using the OS shell.
6666
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
@@ -73,6 +73,6 @@ jobs:
7373
# ./location_of_script_within_repo/buildscript.sh
7474

7575
- name: Perform CodeQL Analysis
76-
uses: github/codeql-action/analyze@v2
76+
uses: github/codeql-action/analyze@v3
7777
with:
7878
category: "/language:${{matrix.language}}"

{{cookiecutter.repo_name}}/.github/workflows/main-serverless-service.yml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ jobs:
3434
BRANCH_NAME: ${{ github.ref }}
3535
REPO_NAME: ${{ github.repository }}
3636
- name: Check out repository code
37-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
37+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
3838
- name: Install poetry
3939
run: pipx install poetry
4040
- name: Set up Python
41-
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
41+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
4242
with:
4343
python-version: ${{ env.PYTHON_VERSION }}
4444
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
4545
- name: Set up Node
46-
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
46+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
4747
with:
4848
node-version: ${{ env.NODE_VERSION }}
4949
cache: "npm"
@@ -73,9 +73,10 @@ jobs:
7373
- name: Codecov
7474
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
7575
with:
76+
token: ${{ secrets.CODECOV_TOKEN }}
7677
files: ./coverage.xml
77-
fail_ci_if_error: false # optional (default = false)
78-
verbose: false # optional (default = false)
78+
fail_ci_if_error: yes # optional (default = false)
79+
verbose: yes # optional (default = false)
7980
- name: Run E2E tests
8081
run: make e2e
8182
env:
@@ -99,16 +100,16 @@ jobs:
99100
BRANCH_NAME: ${{ github.ref }}
100101
REPO_NAME: ${{ github.repository }}
101102
- name: Check out repository code
102-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
103+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
103104
- name: Install poetry
104105
run: pipx install poetry
105106
- name: Set up Python
106-
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
107+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
107108
with:
108109
python-version: ${{ env.PYTHON_VERSION }}
109110
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
110111
- name: Set up Node
111-
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
112+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
112113
with:
113114
node-version: ${{ env.NODE_VERSION }}
114115
cache: "npm"
@@ -138,13 +139,13 @@ jobs:
138139
if: contains('refs/heads/main', github.ref)
139140
steps:
140141
- name: Check out repository code
141-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
142+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
142143
- name: Set up Python
143-
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
144+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
144145
with:
145146
python-version: ${{ env.PYTHON_VERSION }}
146147
- name: Set up Node
147-
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
148+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
148149
with:
149150
node-version: ${{ env.NODE_VERSION }}
150151
cache: "npm"

{{cookiecutter.repo_name}}/.github/workflows/pr-serverless-service.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ jobs:
3131
BRANCH_NAME: ${{ github.ref }}
3232
REPO_NAME: ${{ github.repository }}
3333
- name: Check out repository code
34-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
34+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
3535
- name: Install poetry
3636
run: pipx install poetry
3737
- name: Set up Python
38-
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
38+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
3939
with:
4040
python-version: ${{ env.PYTHON_VERSION }}
4141
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
@@ -55,16 +55,16 @@ jobs:
5555
id-token: write # required for requesting the JWT (GitHub OIDC)
5656
steps:
5757
- name: Check out repository code
58-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
58+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
5959
- name: Install poetry
6060
run: pipx install poetry
6161
- name: Set up Python
62-
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
62+
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
6363
with:
6464
python-version: ${{ env.PYTHON_VERSION }}
6565
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
6666
- name: Set up Node
67-
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
67+
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
6868
with:
6969
node-version: ${{ env.NODE_VERSION }}
7070
cache: "npm"
@@ -94,11 +94,14 @@ jobs:
9494
- name: Codecov
9595
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
9696
with:
97+
token: ${{ secrets.CODECOV_TOKEN }}
9798
files: ./coverage.xml
98-
fail_ci_if_error: false # optional (default = false)
99-
verbose: false # optional (default = false)
99+
fail_ci_if_error: yes # optional (default = false)
100+
verbose: yes # optional (default = false)
100101
- name: Run E2E tests
101102
run: make e2e
103+
- name: Validate OpenAPI Documentation
104+
run: make compare-openapi
102105
- name: Destroy stack
103106
if: always()
104107
run: make destroy

{{cookiecutter.repo_name}}/.github/workflows/scorecard.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ jobs:
3232

3333
steps:
3434
- name: "Checkout code"
35-
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
35+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
3636
with:
3737
persist-credentials: false
3838

3939
- name: "Run analysis"
40-
uses: ossf/scorecard-action@483ef80eb98fb506c348f7d62e28055e49fe2398 # v2.3.0
40+
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
4141
with:
4242
results_file: results.sarif
4343
results_format: sarif
@@ -59,14 +59,14 @@ jobs:
5959
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
6060
# format to the repository Actions tab.
6161
- name: "Upload artifact"
62-
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
62+
uses: actions/upload-artifact@c7d193f32edcb7bfad88892161225aeda64e9392 # v4.0.0
6363
with:
6464
name: SARIF file
6565
path: results.sarif
6666
retention-days: 5
6767

6868
# Upload the results to GitHub's code scanning dashboard.
6969
- name: "Upload to code-scanning"
70-
uses: github/codeql-action/upload-sarif@17573ee1cc1b9d061760f3a006fc4aac4f944fd5 # v2.2.4
70+
uses: github/codeql-action/upload-sarif@1500a131381b66de0c52ac28abb13cd79f4b7ecc # v2.22.12
7171
with:
7272
sarif_file: results.sarif

{{cookiecutter.repo_name}}/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,4 @@ dev_requirements.txt
250250
lambda_requirements.txt
251251
.dccache
252252
node_modules/
253+
.ruff_cache

{{cookiecutter.repo_name}}/.pre-commit-config.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.0.1
4+
rev: v4.5.0
55
hooks:
66
- id: trailing-whitespace
77
name: Ensure that code don't have trailing whitespace
@@ -24,3 +24,11 @@ repos:
2424
name: Tests should begin with test_
2525
args: ["--django"]
2626
exclude: "^(?!helpers/)"
27+
- repo: https://github.com/astral-sh/ruff-pre-commit
28+
# Ruff version.
29+
rev: v0.1.15
30+
hooks:
31+
# Run the Ruff linter.
32+
- id: ruff
33+
# Run the Ruff formatter.
34+
- id: ruff-format
Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
.PHONY: dev lint complex coverage pre-commit sort deploy destroy deps unit infra-tests integration e2e coverage-tests docs lint-docs build format
1+
.PHONY: dev lint complex coverage pre-commit sort deploy destroy deps unit infra-tests integration e2e coverage-tests docs lint-docs build format compare-openapi openapi
22
PYTHON := ".venv/bin/python3"
3-
43
.ONESHELL: # run all commands in a single shell, ensuring it runs within a local virtual env
4+
5+
OPENAPI_DIR := ./docs/swagger
6+
CURRENT_OPENAPI := $(OPENAPI_DIR)/openapi.json
7+
LATEST_OPENAPI := openapi_latest.json
8+
9+
510
dev:
611
pip install --upgrade pip pre-commit poetry
712
pre-commit install
813
# ensures poetry creates a local virtualenv (.venv)
914
poetry config --local virtualenvs.in-project true
10-
poetry install
15+
poetry install --no-root
1116
npm ci
1217

13-
format-fix:
14-
poetry run isort .
15-
poetry run yapf -vv --style=./.style -r --in-place .
16-
1718
format:
18-
poetry run isort .
19-
poetry run yapf -d -vv --style=./.style -r .
19+
poetry run ruff check . --fix
20+
21+
format-fix:
22+
poetry run ruff format .
2023

2124
lint: format
22-
@echo "Running flake8"
23-
poetry run flake8 {{cookiecutter.service_name}}/* cdk/* tests/*
2425
@echo "Running mypy"
2526
$(MAKE) mypy-lint
2627

@@ -34,32 +35,32 @@ pre-commit:
3435
poetry run pre-commit run -a --show-diff-on-failure
3536

3637
mypy-lint:
37-
poetry run mypy --pretty {{cookiecutter.service_name}} cdk tests
38+
poetry run mypy --pretty service cdk tests
3839

3940
deps:
4041
poetry export --only=dev --format=requirements.txt > dev_requirements.txt
4142
poetry export --without=dev --format=requirements.txt > lambda_requirements.txt
4243

4344
unit:
44-
poetry run pytest tests/unit --cov-config=.coveragerc --cov={{cookiecutter.service_name}} --cov-report xml
45+
poetry run pytest tests/unit --cov-config=.coveragerc --cov=service --cov-report xml
4546

4647
build: deps
47-
mkdir -p .build/lambdas ; cp -r {{cookiecutter.service_name}} .build/lambdas
48+
mkdir -p .build/lambdas ; cp -r service .build/lambdas
4849
mkdir -p .build/common_layer ; poetry export --without=dev --format=requirements.txt > .build/common_layer/requirements.txt
4950

5051
infra-tests: build
5152
poetry run pytest tests/infrastructure
5253

5354
integration:
54-
poetry run pytest tests/integration --cov-config=.coveragerc --cov={{cookiecutter.service_name}} --cov-report xml
55+
poetry run pytest tests/integration --cov-config=.coveragerc --cov=service --cov-report xml
5556

5657
e2e:
57-
poetry run pytest tests/e2e --cov-config=.coveragerc --cov={{cookiecutter.service_name}} --cov-report xml
58+
poetry run pytest tests/e2e --cov-config=.coveragerc --cov=service --cov-report xml
5859

59-
pr: deps pre-commit complex lint lint-docs unit deploy coverage-tests e2e
60+
pr: deps format pre-commit complex lint lint-docs unit deploy coverage-tests e2e openapi
6061

6162
coverage-tests:
62-
poetry run pytest tests/unit tests/integration --cov-config=.coveragerc --cov={{cookiecutter.service_name}} --cov-report xml
63+
poetry run pytest tests/unit tests/integration --cov-config=.coveragerc --cov=service --cov-report xml
6364

6465
deploy: build
6566
npx cdk deploy --app="${PYTHON} ${PWD}/app.py" --require-approval=never
@@ -78,4 +79,19 @@ watch:
7879

7980
update-deps:
8081
poetry update
82+
pre-commit autoupdate
8183
npm i --package-lock-only
84+
85+
openapi:
86+
poetry run python generate_openapi.py
87+
88+
compare-openapi:
89+
poetry run python generate_openapi.py --out-destination '.' --out-filename 'openapi_latest.json'
90+
@if cmp --silent $(CURRENT_OPENAPI) $(LATEST_OPENAPI); then \
91+
rm $(LATEST_OPENAPI); \
92+
echo "Swagger file is up to date"; \
93+
else \
94+
echo "Swagger files are not equal, did you run 'make pr' or 'make openapi'?"; \
95+
rm $(LATEST_OPENAPI); \
96+
exit 1; \
97+
fi

0 commit comments

Comments
 (0)