-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (67 loc) · 2.42 KB
/
app.py
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
79
80
81
82
83
84
"""
Module defining project infrastructure using the AWS CDK.
Environment variables are provided to the lambda functions here.
"""
from aws_cdk import (
aws_lambda as alambda,
aws_apigateway as apigw,
aws_dynamodb as dynamodb,
Stack,
App,
Environment,
RemovalPolicy,
)
import config
# TODO change the names and ids of each resource as you require.
class MyStack(Stack):
def __init__(self, scope, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create DynamoDB table
table = dynamodb.Table(
scope=self,
id="MyTable",
partition_key=dynamodb.Attribute(
name="id",
type=dynamodb.AttributeType.STRING,
),
billing_mode=dynamodb.BillingMode.PROVISIONED,
read_capacity=1,
write_capacity=1,
removal_policy=RemovalPolicy.DESTROY, # WARN: makes `cdk destroy` work for this table.
)
# Enable read auto-scaling
read_scaling = table.auto_scale_read_capacity(min_capacity=1, max_capacity=10)
read_scaling.scale_on_utilization(target_utilization_percent=50)
# Enable write auto-scaling
write_scaling = table.auto_scale_write_capacity(min_capacity=1, max_capacity=10)
write_scaling.scale_on_utilization(target_utilization_percent=50)
# Create Lambda function
lambda_function = alambda.Function(
scope=self,
id="MyLambdaFunction",
runtime=alambda.Runtime.PYTHON_3_8,
handler="handler.handle",
code=alambda.Code.from_asset("bin/package.zip"),
environment={
"TABLE_NAME": table.table_name,
},
)
# Grant Lambda function access to DynamoDB table
table.grant_read_write_data(lambda_function)
# Create API Gateway
api = apigw.RestApi(
scope=self,
id="MyApiGateway",
rest_api_name="API Name",
description="API Gateway for my Lambda functions",
)
# Create API Gateway resource and associate with Lambda function
api_resource = api.root.add_resource("endpoint")
api_resource.add_method("ANY", apigw.LambdaIntegration(lambda_function))
app = App()
env = Environment(
account=config.AWS_ACCOUNT,
region=config.AWS_REGION,
)
stack = MyStack(app, "CdkTestStack", env=env)
app.synth()