Skip to content

Commit

Permalink
fix: fix missing replacements and update to v4.7.1 of cookbook (#24)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Ran Isenberg <[email protected]>
  • Loading branch information
ran-isenberg and Ran Isenberg authored Mar 21, 2024
1 parent 6f131c4 commit 318c797
Show file tree
Hide file tree
Showing 22 changed files with 633 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
- name: Set up Node
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
Expand All @@ -53,7 +53,7 @@ jobs:
# once that's discussed we can move unit and infra tests as part of the fast quality standards
# see https://github.com/ran-isenberg/serverless-python-demo/pull/38#discussion_r1299372169
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: ${{ secrets['AWS_ROLE'] }}
role-session-name: ${{ env.SESSION_NAME }}
Expand All @@ -71,7 +71,7 @@ jobs:
env:
ENVIRONMENT: staging # Custom environment variable
- name: Codecov
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
Expand Down Expand Up @@ -109,7 +109,7 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
- name: Set up Node
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
Expand All @@ -119,7 +119,7 @@ jobs:
# once that's discussed we can move unit and infra tests as part of the fast quality standards
# see https://github.com/ran-isenberg/serverless-python-demo/pull/38#discussion_r1299372169
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: ${{ secrets['AWS_ROLE'] }}
role-session-name: ${{ env.SESSION_NAME }}
Expand All @@ -145,7 +145,7 @@ jobs:
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up Node
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,14 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}
cache: "poetry" # NOTE: poetry must be installed before this step, or else cache doesn't work
- name: Set up Node
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
- name: Install dependencies
run: make dev
# NOTE: unit tests are connecting to AWS to instantiate boto3 clients/resources
# once that's discussed we can move unit and infra tests as part of the fast quality standards
# see https://github.com/ran-isenberg/serverless-python-demo/pull/38#discussion_r1299372169
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
role-session-name: ${{ env.SESSION_NAME }}
Expand All @@ -89,19 +86,24 @@ jobs:
run: make deploy
# NOTE: these run unit and integration tests
# we can look into coverage collection only later to make it faster and less brittle (--collect-only)
- name: Check OpenAPI Spec breaking action
uses: oasdiff/oasdiff-action/breaking@1f0be76ff3090248be28b8aa32f673d0f39584a7 # v0.0.16
with:
base: https://raw.githubusercontent.com/{{cookiecutter.author}}/{{cookiecutter.repo_name}}/main/docs/swagger/openapi.json
revision: ./docs/swagger/openapi.json
- name: Validate OpenAPI Documentation
run: make compare-openapi
- name: Code coverage tests
run: make coverage-tests
- name: Codecov
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
uses: codecov/codecov-action@54bcd8715eee62d40e33596ef5e8f0f48dbbccab # v4.1.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
fail_ci_if_error: yes # optional (default = false)
verbose: yes # optional (default = false)
- name: Run E2E tests
run: make e2e
- name: Validate OpenAPI Documentation
run: make compare-openapi
- name: Destroy stack
if: always()
run: make destroy
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/.github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@c7d193f32edcb7bfad88892161225aeda64e9392 # v4.0.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: SARIF file
path: results.sarif
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Ran Isenberg
Copyright (c) 2024 Ran Isenberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 6 additions & 6 deletions {{cookiecutter.repo_name}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,32 @@ pre-commit:
poetry run pre-commit run -a --show-diff-on-failure

mypy-lint:
poetry run mypy --pretty service cdk tests
poetry run mypy --pretty {{cookiecutter.service_name}} cdk tests

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

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

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

infra-tests: build
poetry run pytest tests/infrastructure

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

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

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

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

deploy: build
npx cdk deploy --app="${PYTHON} ${PWD}/app.py" --require-approval=never
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _build_api_gw(self) -> aws_apigateway.RestApi:
CfnOutput(self, id=constants.APIGATEWAY, value=rest_api.url).override_logical_id(constants.APIGATEWAY)
return rest_api

def _build_lambda_role(self, db: dynamodb.Table, idempotency_table: dynamodb.Table) -> iam.Role:
def _build_lambda_role(self, db: dynamodb.TableV2, idempotency_table: dynamodb.TableV2) -> iam.Role:
return iam.Role(
self,
constants.SERVICE_ROLE_ARN,
Expand Down Expand Up @@ -107,14 +107,19 @@ def _build_common_layer(self) -> PythonLayerVersion:
)

def _add_post_lambda_integration(
self, api_resource: aws_apigateway.Resource, role: iam.Role, db: dynamodb.Table, appconfig_app_name: str, idempotency_table: dynamodb.Table
self,
api_resource: aws_apigateway.Resource,
role: iam.Role,
db: dynamodb.TableV2,
appconfig_app_name: str,
idempotency_table: dynamodb.TableV2,
) -> _lambda.Function:
lambda_function = _lambda.Function(
self,
constants.CREATE_LAMBDA,
runtime=_lambda.Runtime.PYTHON_3_12,
code=_lambda.Code.from_asset(constants.BUILD_FOLDER),
handler='service.handlers.handle_create_order.lambda_handler',
handler='{{cookiecutter.service_name}}.handlers.handle_create_order.lambda_handler',
environment={
constants.POWERTOOLS_SERVICE_NAME: constants.SERVICE_NAME, # for logger, tracer and metrics
constants.POWER_TOOLS_LOG_LEVEL: 'INFO', # for logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class ApiDbConstruct(Construct):
def __init__(self, scope: Construct, id_: str) -> None:
super().__init__(scope, id_)

self.db: dynamodb.Table = self._build_db(id_)
self.idempotency_db: dynamodb.Table = self._build_idempotency_table(id_)
self.db: dynamodb.TableV2 = self._build_db(id_)
self.idempotency_db: dynamodb.TableV2 = self._build_idempotency_table(id_)

def _build_idempotency_table(self, id_: str) -> dynamodb.Table:
def _build_idempotency_table(self, id_: str) -> dynamodb.TableV2:
table_id = f'{id_}{constants.IDEMPOTENCY_TABLE_NAME}'
table = dynamodb.Table(
table = dynamodb.TableV2(
self,
table_id,
table_name=table_id,
partition_key=dynamodb.Attribute(name='id', type=dynamodb.AttributeType.STRING),
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
billing=dynamodb.Billing.on_demand(),
removal_policy=RemovalPolicy.DESTROY,
time_to_live_attribute='expiration',
point_in_time_recovery=True,
Expand All @@ -29,14 +29,14 @@ def _build_idempotency_table(self, id_: str) -> dynamodb.Table:
)
return table

def _build_db(self, id_prefix: str) -> dynamodb.Table:
def _build_db(self, id_prefix: str) -> dynamodb.TableV2:
table_id = f'{id_prefix}{constants.TABLE_NAME}'
table = dynamodb.Table(
table = dynamodb.TableV2(
self,
table_id,
table_name=table_id,
partition_key=dynamodb.Attribute(name='id', type=dynamodb.AttributeType.STRING),
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
billing=dynamodb.Billing.on_demand(),
point_in_time_recovery=True,
removal_policy=RemovalPolicy.DESTROY,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path

import aws_cdk.aws_appconfig_alpha as appconfig
from aws_cdk import Duration
from aws_cdk import aws_appconfig as appconfig
from constructs import Construct

from cdk.{{cookiecutter.service_name}}.configuration.schema import FeatureFlagsConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
LAMBDA_BASIC_EXECUTION_ROLE = 'AWSLambdaBasicExecutionRole'
SERVICE_ROLE = 'ServiceRole'
CREATE_LAMBDA = 'CreateOrder'
TABLE_NAME = 'ordersDb'
IDEMPOTENCY_TABLE_NAME = 'IdempotencyTable'
TABLE_NAME = 'orders'
IDEMPOTENCY_TABLE_NAME = 'Idempotency'
TABLE_NAME_OUTPUT = 'DbOutput'
IDEMPOTENCY_TABLE_NAME_OUTPUT = 'IdempotencyDbOutput'
APIGATEWAY = 'Apigateway'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from constructs import Construct

from cdk.service import constants
from cdk.{{cookiecutter.service_name}} import constants


class CrudMonitoring(Construct):
Expand All @@ -24,8 +24,8 @@ def __init__(
scope: Construct,
id_: str,
crud_api: aws_apigateway.RestApi,
db: dynamodb.Table,
idempotency_table: dynamodb.Table,
db: dynamodb.TableV2,
idempotency_table: dynamodb.TableV2,
functions: list[_lambda.Function],
) -> None:
super().__init__(scope, id_)
Expand Down Expand Up @@ -84,7 +84,9 @@ def _build_high_level_dashboard(self, crud_api: aws_apigateway.RestApi, topic: s
group = CustomMetricGroup(metrics=[create_metric], title='Daily Order Requests')
high_level_facade.monitor_custom(metric_groups=[group], human_readable_name='Daily KPIs', alarm_friendly_name='KPIs')

def _build_low_level_dashboard(self, db: dynamodb.Table, idempotency_table: dynamodb.Table, functions: list[_lambda.Function], topic: sns.Topic):
def _build_low_level_dashboard(
self, db: dynamodb.TableV2, idempotency_table: dynamodb.TableV2, functions: list[_lambda.Function], topic: sns.Topic
):
low_level_facade = MonitoringFacade(
self,
f'{self.id_}LowFacade',
Expand Down
8 changes: 4 additions & 4 deletions {{cookiecutter.repo_name}}/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion {{cookiecutter.repo_name}}/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"aws-cdk": "2.125.0"
"aws-cdk": "2.131.0"
}
}
Loading

0 comments on commit 318c797

Please sign in to comment.