Repository navigation
Expand file tree
/
Copy pathorders_api_stack.py
More file actions
78 lines (65 loc) · 3.41 KB
/
Copy pathorders_api_stack.py
File metadata and controls
78 lines (65 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from aws_cdk import (
Stack,
aws_dynamodb as dynamodb,
aws_lambda as lambda_,
aws_apigatewayv2 as apigatewayv2,
aws_apigatewayv2_integrations as integrations,
)
from constructs import Construct
class OrdersApiStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# DynamoDB table
table = dynamodb.Table(self, "OrdersTable",
partition_key={"name": "orderId", "type": dynamodb.AttributeType.STRING},
table_name="Orders")
# Lambda functions
create_order_lambda = lambda_.Function(self, "CreateOrderFunction",
runtime=lambda_.Runtime.PYTHON_3_8,
handler="create_order.lambda_handler",
code=lambda_.Code.from_asset("lambda"))
read_order_lambda = lambda_.Function(self, "ReadOrderFunction",
runtime=lambda_.Runtime.PYTHON_3_8,
handler="read_order.lambda_handler",
code=lambda_.Code.from_asset("lambda"))
update_order_lambda = lambda_.Function(self, "UpdateOrderFunction",
runtime=lambda_.Runtime.PYTHON_3_8,
handler="update_order.lambda_handler",
code=lambda_.Code.from_asset("lambda"))
delete_order_lambda = lambda_.Function(self, "DeleteOrderFunction",
runtime=lambda_.Runtime.PYTHON_3_8,
handler="delete_order.lambda_handler",
code=lambda_.Code.from_asset("lambda"))
# Grant Lambda functions access to the DynamoDB table
table.grant_read_write_data(create_order_lambda)
table.grant_read_write_data(read_order_lambda)
table.grant_read_write_data(update_order_lambda)
table.grant_read_write_data(delete_order_lambda)
# HTTP API Gateway
http_api = apigatewayv2.HttpApi(self, "OrdersApi")
# Define API integrations
create_order_integration = integrations.LambdaProxyIntegration(handler=create_order_lambda)
read_order_integration = integrations.LambdaProxyIntegration(handler=read_order_lambda)
update_order_integration = integrations.LambdaProxyIntegration(handler=update_order_lambda)
delete_order_integration = integrations.LambdaProxyIntegration(handler=delete_order_lambda)
# Add routes to HTTP API
http_api.add_routes(
path="/orders",
methods=[apigatewayv2.HttpMethod.POST],
integration=create_order_integration,
)
http_api.add_routes(
path="/orders/{orderId}",
methods=[apigatewayv2.HttpMethod.GET],
integration=read_order_integration,
)
http_api.add_routes(
path="/orders/{orderId}",
methods=[apigatewayv2.HttpMethod.PUT],
integration=update_order_integration,
)
http_api.add_routes(
path="/orders/{orderId}",
methods=[apigatewayv2.HttpMethod.DELETE],
integration=delete_order_integration,
)