Skip to content

Instantly share code, notes, and snippets.

@leegilmorecode
Created January 25, 2022 05:12
Show Gist options
  • Select an option

  • Save leegilmorecode/3c2b50b10c7b436b64996c9270cf0cb6 to your computer and use it in GitHub Desktop.

Select an option

Save leegilmorecode/3c2b50b10c7b436b64996c9270cf0cb6 to your computer and use it in GitHub Desktop.
Example of a CDK cached resolver in AppSync api
// appsync lambda data source
const blogNoDaxDataSource: appsync.LambdaDataSource =
new appsync.LambdaDataSource(this, "GetBlogNoDaxLambdaDataSource", {
api,
lambdaFunction: getBlogNoDaxHandler,
description: "Get Blog (no dax) Lambda Data Source",
name: "GetBlogNoDaxLambdaDataSource",
});
// appsync resolver
const getBlogNoDaxResolver: appsync.CfnResolver = new appsync.CfnResolver(
this,
"getBlogNoDaxResolver",
{
apiId: api.apiId,
typeName: "Query",
fieldName: "getBlogNoDax",
cachingConfig: {
ttl: cdk.Duration.seconds(30).toSeconds(),
cachingKeys: ["$context.arguments.id"], // Valid values are entries from the $context.arguments, $context.source, and $context.identity maps
},
kind: "UNIT",
dataSourceName: blogNoDaxDataSource.name,
}
);
// add caching for the api
new appsync.CfnApiCache(this, "appsync-cache", {
apiCachingBehavior: "PER_RESOLVER_CACHING",
apiId: api.apiId,
ttl: cdk.Duration.seconds(30).toSeconds(), // cache for 30 seconds as default
type: "SMALL",
atRestEncryptionEnabled: true,
transitEncryptionEnabled: true,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment