Skip to content

Commit

Permalink
feat(amplify): support cache configuration for app (#31381)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

N/A

### Reason for this change
On August 13, 2024, Amplify released improvements to caching efficiency for applications.

Ref: https://aws.amazon.com/jp/blogs/mobile/cdn-caching-improvements-for-better-app-performance-with-aws-amplify-hosting/

To support this feature, add cache configuration for app


### Description of changes
Add `cacheConfigType` property to `App` class


### Description of how you validated changes
Add unit tests and integ test.


### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mazyu36 authored Sep 10, 2024
1 parent 84f69af commit b7bd041
Show file tree
Hide file tree
Showing 12 changed files with 594 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ const amplifyApp = new amplify.App(this, 'MyApp', {
});
```

## Cache Config

Amplify uses Amazon CloudFront to manage the caching configuration for your hosted applications. A cache configuration is applied to each app to optimize for the best performance.

Setting the `cacheConfigType` field on the Amplify `App` construct can be used to control cache configguration. By default, the value is set to `AMPLIFY_MANAGED`. If you want to exclude all cookies from the cache key, set `AMPLIFY_MANAGED_NO_COOKIES`.

For more information, see [Managing the cache configuration for an app](https://docs.aws.amazon.com/amplify/latest/userguide/caching.html).

```ts
const amplifyApp = new amplify.App(this, 'MyApp', {
cacheConfigType: amplify.CacheConfigType.AMPLIFY_MANAGED_NO_COOKIES,
});
```

## Deploying Assets

`sourceCodeProvider` is optional; when this is not specified the Amplify app can be deployed to using `.zip` packages. The `asset` property can be used to deploy S3 assets to Amplify as part of the CDK:
Expand Down
27 changes: 26 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ export interface AppProps {
* @default Platform.WEB
*/
readonly platform?: Platform;

/**
* The type of cache configuration to use for an Amplify app.
*
* @default CacheConfigType.AMPLIFY_MANAGED
*/
readonly cacheConfigType?: CacheConfigType;
}

/**
Expand Down Expand Up @@ -239,7 +246,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
buildSpec: props.autoBranchCreation.buildSpec && props.autoBranchCreation.buildSpec.toBuildSpec(),
enableAutoBranchCreation: true,
enableAutoBuild: props.autoBranchCreation.autoBuild ?? true,
environmentVariables: Lazy.any({ produce: () => renderEnvironmentVariables(this.autoBranchEnvironmentVariables ) }, { omitEmptyArray: true }), // eslint-disable-line max-len
environmentVariables: Lazy.any({ produce: () => renderEnvironmentVariables(this.autoBranchEnvironmentVariables) }, { omitEmptyArray: true }), // eslint-disable-line max-len
enablePullRequestPreview: props.autoBranchCreation.pullRequestPreview ?? true,
pullRequestEnvironmentName: props.autoBranchCreation.pullRequestEnvironmentName,
stage: props.autoBranchCreation.stage,
Expand All @@ -249,6 +256,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
? props.basicAuth.bind(this, 'AppBasicAuth')
: { enableBasicAuth: false },
buildSpec: props.buildSpec && props.buildSpec.toBuildSpec(),
cacheConfig: props.cacheConfigType ? { type: props.cacheConfigType } : undefined,
customRules: Lazy.any({ produce: () => this.customRules }, { omitEmptyArray: true }),
description: props.description,
environmentVariables: Lazy.any({ produce: () => renderEnvironmentVariables(this.environmentVariables) }, { omitEmptyArray: true }),
Expand Down Expand Up @@ -554,3 +562,20 @@ export enum Platform {
*/
WEB_COMPUTE = 'WEB_COMPUTE',
}

/**
* The type of cache configuration to use for an Amplify app.
*/
export enum CacheConfigType {
/**
* AMPLIFY_MANAGED - Automatically applies an optimized cache configuration
* for your app based on its platform, routing rules, and rewrite rules.
*/
AMPLIFY_MANAGED = 'AMPLIFY_MANAGED',

/**
* AMPLIFY_MANAGED_NO_COOKIES - The same as AMPLIFY_MANAGED,
* except that it excludes all cookies from the cache key.
*/
AMPLIFY_MANAGED_NO_COOKIES = 'AMPLIFY_MANAGED_NO_COOKIES',
}
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,17 @@ test('create a dynamically rendered app when the platform is set to WEB_COMPUTE'
Platform: amplify.Platform.WEB_COMPUTE,
});
});

test.each([amplify.CacheConfigType.AMPLIFY_MANAGED, amplify.CacheConfigType.AMPLIFY_MANAGED_NO_COOKIES])('create a app with cacheConfigType is set to %s', (cacheConfigType) => {
// WHEN
new amplify.App(stack, 'App', {
cacheConfigType,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::App', {
CacheConfig: {
Type: cacheConfigType,
},
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"Resources": {
"AppRole1AF9B530": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "amplify.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
}
},
"AppF1B96344": {
"Type": "AWS::Amplify::App",
"Properties": {
"BasicAuthConfig": {
"EnableBasicAuth": false
},
"CacheConfig": {
"Type": "AMPLIFY_MANAGED_NO_COOKIES"
},
"IAMServiceRole": {
"Fn::GetAtt": [
"AppRole1AF9B530",
"Arn"
]
},
"Name": "App",
"Platform": "WEB"
}
},
"AppmainF505BAED": {
"Type": "AWS::Amplify::Branch",
"Properties": {
"AppId": {
"Fn::GetAtt": [
"AppF1B96344",
"AppId"
]
},
"BranchName": "main",
"EnableAutoBuild": true,
"EnablePullRequestPreview": true
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"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."
}
]
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b7bd041

Please sign in to comment.