Skip to content

Commit b58378a

Browse files
committed
migrated to AWS CDK v2 (python)
1 parent 832499e commit b58378a

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

Diff for: apigw-http-api-lambda-cdk/app.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#!/usr/bin/env python3
22
import os
3-
3+
from constructs import Construct
44
from aws_cdk import (
5-
core as cdk,
6-
aws_apigatewayv2 as apigatewayv2,
7-
aws_apigatewayv2_integrations as integrations,
8-
aws_lambda as lambda_,
5+
App,
6+
Stack,
7+
CfnOutput,
8+
Duration,
9+
aws_lambda as lambda_
910
)
11+
import aws_cdk.aws_apigatewayv2_alpha as _apigw
12+
import aws_cdk.aws_apigatewayv2_integrations_alpha as _integrations
13+
1014

1115
DIRNAME = os.path.dirname(__file__)
1216

13-
class ApigwHttpApiLambdaStack(cdk.Stack):
17+
class ApigwHttpApiLambdaStack(Stack):
1418

15-
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
19+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
1620
super().__init__(scope, construct_id, **kwargs)
1721

1822
# Create the Lambda function to receive the request
@@ -29,25 +33,25 @@ def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
2933
)
3034

3135
# Create the HTTP API with CORS
32-
http_api = apigatewayv2.HttpApi(
36+
http_api = _apigw.HttpApi(
3337
self, "MyHttpApi",
34-
cors_preflight=apigatewayv2.CorsPreflightOptions(
35-
allow_methods=[apigatewayv2.CorsHttpMethod.GET],
38+
cors_preflight=_apigw.CorsPreflightOptions(
39+
allow_methods=[_apigw.CorsHttpMethod.GET],
3640
allow_origins=["*"],
37-
max_age=cdk.Duration.days(10),
41+
max_age=Duration.days(10),
3842
)
3943
)
4044

4145
# Add a route to GET /
4246
http_api.add_routes(
4347
path="/",
44-
methods=[apigatewayv2.HttpMethod.GET],
45-
integration=integrations.LambdaProxyIntegration(handler=lambda_fn),
48+
methods=[_apigw.HttpMethod.GET],
49+
integration=_integrations.HttpLambdaIntegration("LambdaProxyIntegration", handler=lambda_fn),
4650
)
4751

4852
# Outputs
49-
cdk.CfnOutput(self, "API Endpoint", description="API Endpoint", value=http_api.api_endpoint)
53+
CfnOutput(self, "API Endpoint", description="API Endpoint", value=http_api.api_endpoint)
5054

51-
app = cdk.App()
55+
app = App()
5256
ApigwHttpApiLambdaStack(app, "ApigwHttpApiLambdaStack")
5357
app.synth()

Diff for: apigw-http-api-lambda-cdk/cdk.json

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
{
22
"app": "python3 app.py",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"requirements*.txt",
11+
"source.bat",
12+
"**/__init__.py",
13+
"python/__pycache__",
14+
"tests"
15+
]
16+
},
317
"context": {
418
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
5-
"@aws-cdk/core:enableStackNameDuplicates": "true",
6-
"aws-cdk:enableDiffNoFail": "true",
7-
"@aws-cdk/core:stackRelativeExports": "true",
8-
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
9-
"@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
10-
"@aws-cdk/aws-kms:defaultKeyPolicies": true,
11-
"@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
12-
"@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true,
19+
"@aws-cdk/core:stackRelativeExports": true,
1320
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
14-
"@aws-cdk/aws-efs:defaultEncryptionAtRest": true,
1521
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
16-
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
22+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
23+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
24+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
25+
"@aws-cdk/core:target-partitions": [
26+
"aws",
27+
"aws-cn"
28+
]
1729
}
18-
}
30+
}

Diff for: apigw-http-api-lambda-cdk/requirements.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
aws-cdk.aws-apigatewayv2
2-
aws-cdk.aws-apigatewayv2-integrations
3-
aws-cdk.aws-lambda
1+
aws-cdk-lib>=2.13.0
2+
constructs>=10.0.0,<11.0.0
3+
aws-cdk.aws-apigatewayv2-alpha
4+
aws-cdk.aws-apigatewayv2-integrations-alpha

0 commit comments

Comments
 (0)