-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitcounter.py
38 lines (31 loc) · 1.07 KB
/
hitcounter.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
from aws_cdk import (
aws_lambda as _lambda,
aws_dynamodb as ddb,
core,
)
class HitCounter(core.Construct):
@property
def handler(self):
return self._handler
@property
def table(self):
return self._table
def __init__(self, scope: core.Construct, id: str, downstream: _lambda.IFunction, **kwargs):
super().__init__(scope, id, **kwargs)
self._table = ddb.Table(
self, 'Hits',
partition_key={'name': 'path', 'type': ddb.AttributeType.STRING}
)
self._handler = _lambda.Function(
self, 'HitCountHandler',
runtime=_lambda.Runtime.PYTHON_3_7,
handler='hitcount.handler',
code=_lambda.Code.asset('./lambda/hitcount'),
environment={
'DOWNSTREAM_FUNCTION_NAME': downstream.function_name,
'HITS_TABLE_NAME': self._table.table_name,
}
)
# Grant DynamoDB to write access
self._table.grant_read_write_data(self.handler)
downstream.grant_invoke(self.handler)