diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json new file mode 100644 index 0000000000000..b515bdf53dc56 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.5", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "apiDefaultTestDeployAssert018781F2.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/apiDefaultTestDeployAssert018781F2.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/cdk.out new file mode 100644 index 0000000000000..bd5311dc372de --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.5"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/integ.json new file mode 100644 index 0000000000000..65418585ed9a5 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.5", + "testCases": { + "api/DefaultTest": { + "stacks": [ + "stack" + ], + "assertionStack": "api/DefaultTest/DeployAssert", + "assertionStackName": "apiDefaultTestDeployAssert018781F2" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/manifest.json new file mode 100644 index 0000000000000..6a7729c8b52e7 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/manifest.json @@ -0,0 +1,131 @@ +{ + "version": "36.0.5", + "artifacts": { + "stack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "stack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "stack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "stack.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/850fd88ad0379363b8029cb69e0b1145b89f800e77384d51850a17961d68137f.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "stack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "stack.assets" + ], + "metadata": { + "/stack/baseAPI/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "baseAPIDF61E9F7" + } + ], + "/stack/baseAPI/Schema": [ + { + "type": "aws:cdk:logicalId", + "data": "baseAPISchema098BD9C8" + } + ], + "/stack/baseAPI/DefaultApiKey": [ + { + "type": "aws:cdk:logicalId", + "data": "baseAPIDefaultApiKey40D005BE" + } + ], + "/stack/apiCache/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "apiCache2AD49924" + } + ], + "/stack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/stack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "stack" + }, + "apiDefaultTestDeployAssert018781F2.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "apiDefaultTestDeployAssert018781F2.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "apiDefaultTestDeployAssert018781F2": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "apiDefaultTestDeployAssert018781F2.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "apiDefaultTestDeployAssert018781F2.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "apiDefaultTestDeployAssert018781F2.assets" + ], + "metadata": { + "/api/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/api/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "api/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.assets.json new file mode 100644 index 0000000000000..4c8cca9d73ae4 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.5", + "files": { + "850fd88ad0379363b8029cb69e0b1145b89f800e77384d51850a17961d68137f": { + "source": { + "path": "stack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "850fd88ad0379363b8029cb69e0b1145b89f800e77384d51850a17961d68137f.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.template.json new file mode 100644 index 0000000000000..99186926fc447 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/stack.template.json @@ -0,0 +1,85 @@ +{ + "Resources": { + "baseAPIDF61E9F7": { + "Type": "AWS::AppSync::GraphQLApi", + "Properties": { + "AuthenticationType": "API_KEY", + "Name": "baseAPI" + } + }, + "baseAPISchema098BD9C8": { + "Type": "AWS::AppSync::GraphQLSchema", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + }, + "Definition": "type test {\n version: String!\n}\ntype Query {\n getTests: [test]!\n}\ntype Mutation {\n addTest(version: String!): test\n}\n" + } + }, + "baseAPIDefaultApiKey40D005BE": { + "Type": "AWS::AppSync::ApiKey", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + } + }, + "DependsOn": [ + "baseAPISchema098BD9C8" + ] + }, + "apiCache2AD49924": { + "Type": "AWS::AppSync::ApiCache", + "Properties": { + "ApiCachingBehavior": "FULL_REQUEST_CACHING", + "ApiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + }, + "Ttl": 60, + "Type": "LARGE" + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/tree.json new file mode 100644 index 0000000000000..acdcec3df1f20 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.js.snapshot/tree.json @@ -0,0 +1,205 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "stack": { + "id": "stack", + "path": "stack", + "children": { + "baseAPI": { + "id": "baseAPI", + "path": "stack/baseAPI", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/baseAPI/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::GraphQLApi", + "aws:cdk:cloudformation:props": { + "authenticationType": "API_KEY", + "name": "baseAPI" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnGraphQLApi", + "version": "0.0.0" + } + }, + "Schema": { + "id": "Schema", + "path": "stack/baseAPI/Schema", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::GraphQLSchema", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + }, + "definition": "type test {\n version: String!\n}\ntype Query {\n getTests: [test]!\n}\ntype Mutation {\n addTest(version: String!): test\n}\n" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnGraphQLSchema", + "version": "0.0.0" + } + }, + "DefaultApiKey": { + "id": "DefaultApiKey", + "path": "stack/baseAPI/DefaultApiKey", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::ApiKey", + "aws:cdk:cloudformation:props": { + "apiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnApiKey", + "version": "0.0.0" + } + }, + "LogGroup": { + "id": "LogGroup", + "path": "stack/baseAPI/LogGroup", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.GraphqlApi", + "version": "0.0.0" + } + }, + "apiCache": { + "id": "apiCache", + "path": "stack/apiCache", + "children": { + "Resource": { + "id": "Resource", + "path": "stack/apiCache/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::AppSync::ApiCache", + "aws:cdk:cloudformation:props": { + "apiCachingBehavior": "FULL_REQUEST_CACHING", + "apiId": { + "Fn::GetAtt": [ + "baseAPIDF61E9F7", + "ApiId" + ] + }, + "ttl": 60, + "type": "LARGE" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.CfnApiCache", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_appsync.ApiCache", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "stack/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "stack/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "api": { + "id": "api", + "path": "api", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "api/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "api/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "api/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "api/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "api/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.ts new file mode 100644 index 0000000000000..795b585f08e01 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.appsync-cache.ts @@ -0,0 +1,25 @@ +import * as path from 'path'; +import * as cdk from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import * as appsync from 'aws-cdk-lib/aws-appsync'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'stack'); + +const api = new appsync.GraphqlApi(stack, 'baseAPI', { + name: 'baseAPI', + schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.test.graphql')), +}); + +new appsync.ApiCache(stack, 'apiCache', { + apiId: api.apiId, + apiCachingBehavior: appsync.CacheBehavior.FULL_REQUEST_CACHING, + type: appsync.CacheType.LARGE, + ttl: 60, +}); + +new IntegTest(app, 'api', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-appsync/README.md b/packages/aws-cdk-lib/aws-appsync/README.md index 40ba93f974350..d344a16c2c6d4 100644 --- a/packages/aws-cdk-lib/aws-appsync/README.md +++ b/packages/aws-cdk-lib/aws-appsync/README.md @@ -912,3 +912,29 @@ rule.addTarget(new targets.AppSync(api, { }), })); ``` + +## Caching + +You can create a cache for an API using the `apiId` from an existing graphQL API or add a cache +to an existing graphQL API resource. + +```ts +const api = new appsync.GraphqlApi(this, 'api', { + name: 'api', + definition: appsync.Definition.fromFile(path.join(__dirname, 'appsync.schema.graphql')), +}); + +const cache = new appsync.ApiCache(this, 'ApiCache', { + apiId: api.apiId, + apiCachingBehavior: appsync.CacheBehavior.FULL_REQUEST_CACHING, + type: appsync.CacheType.LARGE, + ttl: 60, +}); + +// or with the addCache method: +api.addCache('ApiCache', { + apiCachingBehavior: appsync.CacheBehavior.PER_RESOLVER_CACHING, + type: appsync.CacheType.LARGE, + ttl: 360, +}); +``` \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-appsync/lib/api-cache.ts b/packages/aws-cdk-lib/aws-appsync/lib/api-cache.ts new file mode 100644 index 0000000000000..0af10ed72a8d1 --- /dev/null +++ b/packages/aws-cdk-lib/aws-appsync/lib/api-cache.ts @@ -0,0 +1,135 @@ +import { Construct } from 'constructs'; +import { CfnApiCache } from './appsync.generated'; + +/** + * enum with all Api Caching Behavior values + */ +export enum CacheBehavior { + /** + * FULL_REQUEST_CACHING: All requests are fully cached. + */ + FULL_REQUEST_CACHING = 'FULL_REQUEST_CACHING', + /** + * PER_RESOLVER_CACHING: Individual resolvers that you specify are cached. + */ + PER_RESOLVER_CACHING = 'PER_RESOLVER_CACHING', +} + +/** + * enum with all Health Metrics Configuration options + */ +export enum HealthMetrics { + /** + * ENABLE CloudWatch metrics on cache + */ + ENABLED = 'ENABLED', + /** + * DISABLE CloudWatch metrics on cache + */ + DISABLED = 'DISABLED', +} + +/** + * enum with all cache instance type values + */ +export enum CacheType { + /** + * SMALL cache instance type + */ + SMALL = 'SMALL', + /** + * MEDIUM cache instance type + */ + MEDIUM = 'MEDIUM', + /** + * LARGE cache instance type + */ + LARGE = 'LARGE', + /** + * XLARGE cache instance type + */ + XLARGE = 'XLARGE', + /** + * LARGE_2X cache instance type + */ + LARGE_2X = 'LARGE_2X', + /** + * LARGE_4X cache instance type + */ + LARGE_4X = 'LARGE_4X', + /** + * LARGE_8X cache instance type (not available in all regions) + */ + LARGE_8X = 'LARGE_8X ', + /** + * LARGE_12X cache instance type + */ + LARGE_12X = 'LARGE_12X', +} + +/** + * Base properties for an Api Cache + */ +export interface ApiCacheBaseProps { + /** + * Caching behavior + */ + readonly apiCachingBehavior: CacheBehavior; + + /** + * At-rest encryption flag for cache + * + * @default - at rest encryption is disabled + */ + readonly atRestEncryptionEnabled?: boolean; + /** + * Controls how cache health metrics will be emitted to CloudWatch + * Metrics will be recorded by API ID + * + * @default - health metrics are disabled + */ + readonly healthMetricsConfig?: HealthMetrics; + /** + * Transit encryption flag when connecting to cache + * + * @default - transit encryption is disabled + */ + readonly transitEncryptionEnabled?: boolean; + /** + * TTL in seconds for cache entries. Valid values are 1–3,600 seconds. + */ + readonly ttl: number; + /** + * The cache instance type + */ + readonly type: CacheType; +} + +/** + * Properties for an Api Cache + */ +export interface ApiCacheProps extends ApiCacheBaseProps { + /** + * The GraphQL API ID. + */ + readonly apiId: string; +} + +/** + * Cache for a GraphQL API + */ +export class ApiCache extends Construct { + constructor(scope: Construct, id: string, props: ApiCacheProps) { + super(scope, id); + + new CfnApiCache(this, 'Resource', { + apiId: props.apiId, + apiCachingBehavior: props.apiCachingBehavior, + atRestEncryptionEnabled: props.atRestEncryptionEnabled, + healthMetricsConfig: props.healthMetricsConfig, + transitEncryptionEnabled: props.transitEncryptionEnabled, + ttl: props.ttl, + type: props.type, + }); + } +} \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts b/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts index 7b87718a52e5d..1c4f864157a05 100644 --- a/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts +++ b/packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts @@ -1,3 +1,4 @@ +import { ApiCache, ApiCacheBaseProps } from './api-cache'; import { DynamoDbDataSource, HttpDataSource, @@ -597,4 +598,22 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi { public grantSubscription(grantee: IGrantable, ...fields: string[]): Grant { return this.grant(grantee, IamResource.ofType('Subscription', ...fields), 'appsync:GraphQL'); } + + /** + * Creates an API Cache for this GraphQLApi + * + * @param id The id for this resource + * @param props The configuration needed for the API Cache resource + */ + public addCache(id: string, props: ApiCacheBaseProps): ApiCache { + return new ApiCache(this, id, { + apiId: this.apiId, + apiCachingBehavior: props.apiCachingBehavior, + atRestEncryptionEnabled: props.atRestEncryptionEnabled, + healthMetricsConfig: props.healthMetricsConfig, + transitEncryptionEnabled: props.transitEncryptionEnabled, + ttl: props.ttl, + type: props.type, + }); + } } diff --git a/packages/aws-cdk-lib/aws-appsync/lib/index.ts b/packages/aws-cdk-lib/aws-appsync/lib/index.ts index 4c14170b294d5..97a15a7f015b2 100644 --- a/packages/aws-cdk-lib/aws-appsync/lib/index.ts +++ b/packages/aws-cdk-lib/aws-appsync/lib/index.ts @@ -13,3 +13,4 @@ export * from './graphqlapi-base'; export * from './code'; export * from './runtime'; export * from './source-api-association'; +export * from './api-cache'; diff --git a/packages/aws-cdk-lib/aws-appsync/test/appsync-api-cache.test.ts b/packages/aws-cdk-lib/aws-appsync/test/appsync-api-cache.test.ts new file mode 100644 index 0000000000000..8bac6e42d4e17 --- /dev/null +++ b/packages/aws-cdk-lib/aws-appsync/test/appsync-api-cache.test.ts @@ -0,0 +1,51 @@ +import * as path from 'path'; +import { Template } from '../../assertions'; +import * as cdk from '../../core'; +import * as appsync from '../lib'; + +// GLOBAL GIVEN +let stack: cdk.Stack; +let api: appsync.GraphqlApi; +let cache: appsync.ApiCache; +beforeEach(() => { + stack = new cdk.Stack(); + api = new appsync.GraphqlApi(stack, 'baseApi', { + name: 'api', + schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'appsync.test.graphql')), + }); +}); + +describe('Api Cache configuration', () => { + test('API cache produces AWS::AppSync::ApiCache', () => { + cache = new appsync.ApiCache(stack, 'baseApiCache', { + apiId: api.apiId, + apiCachingBehavior: appsync.CacheBehavior.FULL_REQUEST_CACHING, + type: appsync.CacheType.LARGE, + ttl: 60, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::ApiCache', { + Type: 'LARGE', + Ttl: 60, + ApiCachingBehavior: 'FULL_REQUEST_CACHING', + ApiId: { 'Fn::GetAtt': ['baseApiCDA4D43A', 'ApiId'] }, + }); + }); +}); + +describe('Add a Cache to a GraphQL API resource', () => { + test('API cache gets added to api with addCache function', ()=> { + api.addCache('ApiCache', { + apiCachingBehavior: appsync.CacheBehavior.PER_RESOLVER_CACHING, + type: appsync.CacheType.LARGE, + ttl: 360, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::AppSync::ApiCache', { + Type: 'LARGE', + Ttl: 360, + ApiCachingBehavior: 'PER_RESOLVER_CACHING', + ApiId: { 'Fn::GetAtt': ['baseApiCDA4D43A', 'ApiId'] }, + }); + }); +}); \ No newline at end of file