Skip to content

Commit 7f262e9

Browse files
ran-isenbergRan Isenberg
andauthored
feature: CDK 2 upgrade (#21)
* docs: update docs * feature: cdk2 upgrade Co-authored-by: Ran Isenberg <ran.isenberg@cyberark.com>
1 parent f4976a8 commit 7f262e9

File tree

15 files changed

+238
-501
lines changed

15 files changed

+238
-501
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"cSpell.words": [
3-
"Codecov"
3+
"Codecov",
4+
"ranisenberg"
45
]
56
}

Pipfile.lock

Lines changed: 161 additions & 389 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#!/usr/bin/env python3
22
import os
33

4-
from aws_cdk import core
4+
from aws_cdk import App, Environment
55
from boto3 import client, session
66
from service_stack.constants import get_stack_name
77
from service_stack.cookbook_stack import CookBookStack
88

99
# pylint: disable=invalid-name
1010
account = client('sts').get_caller_identity()['Account']
1111
region = session.Session().region_name
12-
app = core.App()
13-
my_stack = CookBookStack(
14-
app, get_stack_name(),
15-
env=core.Environment(account=os.environ.get('AWS_DEFAULT_ACCOUNT', account), region=os.environ.get('AWS_DEFAULT_REGION', region)))
12+
app = App()
13+
my_stack = CookBookStack(app, get_stack_name(),
14+
env=Environment(account=os.environ.get('AWS_DEFAULT_ACCOUNT', account), region=os.environ.get('AWS_DEFAULT_REGION', region)))
1615

1716
app.synth()

cdk/aws_lambda_handler_cookbook/service_stack/cookbook_construct.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from pathlib import Path
33

44
import boto3
5-
from aws_cdk import aws_apigateway
5+
from aws_cdk import CfnOutput, Duration, RemovalPolicy, aws_apigateway
66
from aws_cdk import aws_iam as iam
77
from aws_cdk import aws_lambda as _lambda
8-
from aws_cdk import core
9-
from aws_cdk.aws_lambda_python import PythonLayerVersion
8+
from aws_cdk.aws_lambda_python_alpha import PythonLayerVersion
9+
from constructs import Construct
1010

1111
from .constants import (
1212
API_HANDLER_LAMBDA_MEMORY_SIZE,
@@ -26,10 +26,10 @@
2626
)
2727

2828

29-
class LambdaConstruct(core.Construct):
29+
class LambdaConstruct(Construct):
3030

3131
# pylint: disable=invalid-name, no-value-for-parameter
32-
def __init__(self, scope: core.Construct, id_: str) -> None:
32+
def __init__(self, scope: Construct, id_: str) -> None:
3333
super().__init__(scope, id_)
3434

3535
self.lambda_role = self._build_lambda_role()
@@ -48,7 +48,7 @@ def _build_api_gw(self) -> aws_apigateway.LambdaRestApi:
4848
deploy_options=aws_apigateway.StageOptions(throttling_rate_limit=2, throttling_burst_limit=10),
4949
)
5050

51-
core.CfnOutput(self, id=APIGATEWAY, value=rest_api.url).override_logical_id(APIGATEWAY)
51+
CfnOutput(self, id=APIGATEWAY, value=rest_api.url).override_logical_id(APIGATEWAY)
5252
return rest_api
5353

5454
def _build_lambda_role(self) -> iam.Role:
@@ -66,7 +66,7 @@ def _build_common_layer(self) -> PythonLayerVersion:
6666
'CommonLayer',
6767
entry=COMMION_LAYER_BUILD_FOLDER,
6868
compatible_runtimes=[_lambda.Runtime.PYTHON_3_8],
69-
removal_policy=core.RemovalPolicy.DESTROY,
69+
removal_policy=RemovalPolicy.DESTROY,
7070
)
7171

7272
def __add_get_lambda_integration(self, api_name: aws_apigateway.Resource):
@@ -84,7 +84,7 @@ def __add_get_lambda_integration(self, api_name: aws_apigateway.Resource):
8484
},
8585
tracing=_lambda.Tracing.ACTIVE,
8686
retry_attempts=0,
87-
timeout=core.Duration.seconds(API_HANDLER_LAMBDA_TIMEOUT),
87+
timeout=Duration.seconds(API_HANDLER_LAMBDA_TIMEOUT),
8888
memory_size=API_HANDLER_LAMBDA_MEMORY_SIZE,
8989
layers=[self.common_layer],
9090
)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from aws_cdk import core
1+
from aws_cdk import Stack
22
from aws_lambda_handler_cookbook.service_stack.cookbook_construct import LambdaConstruct
3+
from constructs import Construct
34

45

5-
class CookBookStack(core.Stack):
6+
class CookBookStack(Stack):
67

78
# pylint: disable=redefined-builtin
8-
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
9+
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
910
super().__init__(scope, id, **kwargs)
1011
self.lambdas = LambdaConstruct(self, 'Service')

cdk/setup.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
include_package_data=True,
2424
python_requires='>=3.8',
2525
install_requires=[
26-
'aws-cdk.aws-sam<2.0.0',
27-
'aws-cdk.core<2.0.0',
28-
'aws_cdk.aws_lambda<2.0.0',
29-
'aws-cdk.aws_apigateway<2.0.0',
30-
'aws-cdk.aws_lambda_python<2.0.0',
26+
'aws-cdk-lib>=2.0.0',
27+
'constructs>=10.0.0',
28+
'aws-cdk.aws-lambda-python-alpha==2.16.0a0',
3129
],
3230
)

dev_requirements.txt

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,17 @@
11
-i https://pypi.org/simple
22
-e ./cdk
33
attrs==21.4.0
4-
aws-cdk.assets==1.147.0
5-
aws-cdk.aws-acmpca==1.147.0
6-
aws-cdk.aws-apigateway==1.147.0
7-
aws-cdk.aws-applicationautoscaling==1.147.0
8-
aws-cdk.aws-autoscaling-common==1.147.0
9-
aws-cdk.aws-certificatemanager==1.147.0
10-
aws-cdk.aws-cloudformation==1.147.0
11-
aws-cdk.aws-cloudwatch==1.147.0
12-
aws-cdk.aws-codeguruprofiler==1.147.0
13-
aws-cdk.aws-codestarnotifications==1.147.0
14-
aws-cdk.aws-cognito==1.147.0
15-
aws-cdk.aws-ec2==1.147.0
16-
aws-cdk.aws-ecr-assets==1.147.0
17-
aws-cdk.aws-ecr==1.147.0
18-
aws-cdk.aws-efs==1.147.0
19-
aws-cdk.aws-elasticloadbalancingv2==1.147.0
20-
aws-cdk.aws-events==1.147.0
21-
aws-cdk.aws-iam==1.147.0
22-
aws-cdk.aws-kms==1.147.0
23-
aws-cdk.aws-lambda-python==1.147.0
24-
aws-cdk.aws-lambda==1.147.0
25-
aws-cdk.aws-logs==1.147.0
26-
aws-cdk.aws-route53==1.147.0
27-
aws-cdk.aws-s3-assets==1.147.0
28-
aws-cdk.aws-s3==1.147.0
29-
aws-cdk.aws-sam==1.147.0
30-
aws-cdk.aws-signer==1.147.0
31-
aws-cdk.aws-sns==1.147.0
32-
aws-cdk.aws-sqs==1.147.0
33-
aws-cdk.aws-ssm==1.147.0
34-
aws-cdk.aws-stepfunctions==1.147.0
35-
aws-cdk.cloud-assembly-schema==1.147.0
36-
aws-cdk.core==1.147.0
37-
aws-cdk.custom-resources==1.147.0
38-
aws-cdk.cx-api==1.147.0
39-
aws-cdk.region-info==1.147.0
40-
boto3==1.21.13
41-
botocore==1.24.13
4+
aws-cdk-lib==2.16.0
5+
aws-cdk.aws-lambda-python-alpha==2.16.0a0
6+
boto3==1.21.20
7+
botocore==1.24.20
428
cattrs==1.10.0 ; python_version >= '3.7'
439
certifi==2021.10.8
4410
cfgv==3.3.1
4511
charset-normalizer==2.0.12 ; python_version >= '3'
4612
click==8.0.4
4713
colorama==0.4.4 ; python_version > '3.4'
48-
constructs==3.3.234
14+
constructs==10.0.89
4915
coverage[toml]==6.3.2
5016
distlib==0.3.4
5117
filelock==3.6.0
@@ -54,22 +20,22 @@ future==0.18.2
5420
ghp-import==2.0.2
5521
gitdb==4.0.9
5622
gitpython==3.1.27
57-
identify==2.4.11
23+
identify==2.4.12
5824
idna==3.3
59-
importlib-metadata==4.11.2 ; python_version < '3.10'
25+
importlib-metadata==4.11.3 ; python_version < '3.10'
6026
iniconfig==1.1.1
6127
isort==5.10.1
6228
jinja2==3.0.3
6329
jmespath==0.10.0
64-
jsii==1.54.0
30+
jsii==1.55.1
6531
mando==0.6.4
6632
markdown==3.3.6
67-
markupsafe==2.1.0
33+
markupsafe==2.1.1
6834
mccabe==0.6.1
6935
mergedeep==1.3.4
70-
mkdocs-git-revision-date-plugin==0.3.1
36+
mkdocs-git-revision-date-plugin==0.3.2
7137
mkdocs-material-extensions==1.0.3
72-
mkdocs-material==8.2.4
38+
mkdocs-material==8.2.5
7339
mkdocs==1.2.3
7440
nodeenv==1.6.0
7541
packaging==21.3
@@ -87,7 +53,7 @@ pytest-cov==3.0.0
8753
pytest-html==3.1.1
8854
pytest-metadata==1.11.0
8955
pytest-mock==3.7.0
90-
pytest==7.0.1
56+
pytest==7.1.0
9157
python-dateutil==2.8.2
9258
python-dotenv==0.19.2
9359
pyyaml-env-tag==0.1
@@ -100,8 +66,8 @@ smmap==5.0.0
10066
toml==0.10.2
10167
tomli==2.0.1
10268
typing-extensions==4.1.1
103-
urllib3==1.26.8
104-
virtualenv==20.13.2
69+
urllib3==1.26.9
70+
virtualenv==20.13.3
10571
watchdog==2.1.6
10672
xenon==0.9.0
10773
yapf==0.32.0

docs/best_practices/environment_variables.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Environment Variables decorator is a simple parser for environment variables tha
66

77
![Environment Variables](../media/pydantic.png){: style="height:50%;width:20%"}
88

9-
## Key features
9+
## **Key features**
1010
* A defined [Pydantic](https://pydantic-docs.helpmanual.io/){:target="_blank" rel="noopener"} schema for all required environment variables
1111
* A decorator that parses and validates environment variables, value constraints included
1212
* Global getter for parsed & valid schema dataclass with all environment variables
@@ -18,11 +18,11 @@ The best practice for handling environment variables is to validate & parse them
1818
In case of misconfiguration, a validation exception is raised with all the relevant exception details.
1919

2020

21-
## Blog Reference
21+
## **Blog Reference**
2222
Read more about the importance of validating environment variables and how this utility works. Click [**HERE**](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-environment-variables){:target="_blank" rel="noopener"}
2323

2424

25-
## Schema Definition
25+
## **Schema Definition**
2626

2727
You need to define all your environment variables in a Pydantic schema class that extend Pydantic's BaseModel class.
2828

@@ -55,7 +55,7 @@ This schema makes sure that:
5555

5656
Read [here](https://pydantic-docs.helpmanual.io/usage/models/){:target="_blank" rel="noopener"} about Pydantic Model capabilities.
5757

58-
## Decorator Usage
58+
## **Decorator Usage**
5959
The decorator 'init_environment_variables' is defined under the utility folder **service.utils.env_vars_parser.py** and imported in the handler.
6060

6161
The decorator requires a **model** parameter, which in this example is the name of the schema class we defined above.
@@ -78,7 +78,7 @@ The decorator requires a **model** parameter, which in this example is the name
7878
return {'statusCode': HTTPStatus.OK, 'headers': {'Content-Type': 'application/json'}, 'body': json.dumps({'message': 'success'})}
7979
```
8080

81-
## Global Getter Usage
81+
## **Global Getter Usage**
8282
The getter function 'get_environment_variables' is defined under the utility folder **service.utils.env_vars_parser.py** and imported in the handler.
8383

8484
The getter function returns a parsed and validated global instance of the environment variables Pydantic schema class.
@@ -106,7 +106,7 @@ It can be used *anywhere* in the function code, not just the handler.
106106

107107

108108

109-
## More Details
109+
## **More Details**
110110

111111
Read [here](https://pydantic-docs.helpmanual.io/usage/types/){:target="_blank" rel="noopener"} about Pydantic field types.
112112

docs/best_practices/logger.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ It’s a wrapper of Python’s logging library that provides extra capabilities
66

77
![Logger](../media/logger.png)
88

9-
## Key features
9+
## **Key features**
1010

1111
* Capture key fields from Lambda context, cold start and structures logging output as JSON
1212
* Log Lambda event when instructed (disabled by default)
1313
* Append additional keys to structured log at any point in time
1414

1515

16-
## Usage in Handler
16+
## **Usage in Handler**
1717
The logger is a singleton which is defined under the utility folder **service.utils.observability.py** and imported in the handler.
1818

19-
## Blog Reference
19+
## **Blog Reference**
2020
Read more about the importance of the logger and how to use AWS CloudWatch logs in my blog. Click [**HERE**](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-elevate-your-handler-s-code-part-1-logging){:target="_blank" rel="noopener"}
2121

2222

23-
## More Details
23+
## **More Details**
2424
You can find more information at the official documentation. Go to [https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/){:target="_blank" rel="noopener"}

docs/best_practices/metrics.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ These metrics can be visualized through [Amazon CloudWatch Console](https://cons
1212

1313
![Metrics](../media/metrics.png)
1414

15-
## Key features
15+
## **Key features**
1616

1717
* Aggregate up to 100 metrics using a single CloudWatch EMF object (large JSON blob)
1818
* Validate against common metric definitions mistakes (metric unit, values, max dimensions, max metrics, etc)
1919
* Metrics are created asynchronously by CloudWatch service, no custom stacks needed
2020

2121

22-
## Usage in Handler
22+
## **Usage in Handler**
2323
The metrics is a singleton which is defined under the utility folder **service.utils.observability.py** and imported in the handler.
2424

25-
## Blog Reference
25+
## **Blog Reference**
2626
Read more about the importance of the business KPis and metrics in my blog. Click [**HERE**](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-elevate-your-handler-s-code-part-3-business-domain-observability){:target="_blank" rel="noopener"}
2727

2828

29-
## More Details
29+
## **More Details**
3030
You can find more information at the official documentation. Go to [https://awslabs.github.io/aws-lambda-powertools-python/latest/core/metrics/](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/metrics/){:target="_blank" rel="noopener"}

0 commit comments

Comments
 (0)