AWS | Cloud | Python | Technology
Increasing your development speed with AWS Lambda Powertools Joris Conijn 24 Aug, 2021
const cacheTable = new dynamoDB.Table(this, 'CacheTable'...
const handler = new lambda.Function(this, 'CacheHandler', ...
const cacheTable = new dynamoDB.Table(this, 'CacheTable', {
tableName: 'Cache',
partitionKey: {
name: 'id',
type: dynamoDB.AttributeType.STRING,
},
billingMode: dynamoDB.BillingMode.PROVISIONED,
timeToLiveAttribute: 'ttl',
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
The lambda is defined with Node18 as its runtime: const handler = new lambda.Function(this, 'CacheHandler', {
runtime: lambda.Runtime.NODEJS_18_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'cache.handler',
});
Besides the table and the lambda, cache-blog-stack.ts also defines an api gateway that will allow http access to the
cache lambda. To deploy the stack, run:cdk bootstrap
to create the stack in your aws account. And thennpm run build && cdk deploy
The stack is now created. The output shows the endpoint of the api gateway used to access the caching lambda. It will
look like this:CacheBlogStack
Deployment time: 22.37s
Outputs:
CacheBlogStack.CacheServicecacheapiEndpointDA49EE49
= https://0znqtkdtml.execute-api.eu-west-1.amazonaws.com/prod/
Now we can store and retrieve values from the cache like this:curl -X POST https://0znqtkdtml.execute-api.eu-west-1.amazonaws.com/prod/ -H "Accept: application/json" -d '{ "id": "id1", "value":"30"}'
curl https://0znqtkdtml.execute-api.eu-west-1.amazonaws.com/prod/\?id\=id1
The value of id doesn't really matter, but a pattern we found useful looks like <tableName> - <uuid>, e.g. PersonalData - dbf275d5-d61b-488a-97fc-c2cb1832c852. This would store personal data, maybe retrieved from a
backend service, for a user account with id dbf275d5-d61b-488a-97fc-c2cb1832c852. In principle, the uuid value
would be enough, but we found we often want to store different types of data about a specific user account. In that
case the uuid would be the uuid of the account and the prefix of the cache key would identify the type of data in the
cache record. The lambda code is straight forward, implementing a GET and POST method. The GET method is interesting because it shows
how to avoid a pitfall with the TTL attribute. const getCacheItem = async (id: string): ... in cache-blog/lambda/database.ts executes this query when retrieving the value with the given id: const queryCommand = new QueryCommand({
TableName: CACHE_TABLE,
ExpressionAttributeNames: {
'#id': 'id',
'#ttl': 'ttl',
},
KeyConditionExpression: '#id = :id',
ExpressionAttributeValues: {
':id': id,
':now': now,
},
FilterExpression: '#ttl > :now',
});
The important part is FilterExpression: '#ttl > :now'. This filter expression is necessary because DynamoDB doesn't
guarantee outdated records are removed when their TTL is passed. Therefore, a record may still be in the database when it has
actually expired. We can fix this problem by adding the filter expression. Note that TTL is expressed in seconds. Removing the infrastructure created for this blog can be done with CDK:cdk destroy
Are you sure you want to delete: CacheBlogStack (y/n)? y
CacheBlogStack: destroying... [1/1]
CacheBlogStack: destroyed