@vyriy/stack 0.1.16 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,6 +18,7 @@ This package keeps small, reusable CDK construction helpers close to the AWS pri
18
18
  - `route53.createCloudFrontTarget(distribution)` creates a Route 53 alias target for CloudFront.
19
19
  - `acm.createCertificate(scope, id, props)` creates an ACM certificate.
20
20
  - `deployment.createBucketDeployment(scope, id, props)` deploys files to S3 with a `512` MB default memory limit.
21
+ - `deployment.createHtmlCacheControl()` creates cache-control headers for HTML files that should revalidate before reuse.
21
22
  - `deployment.createImmutableCacheControl(days?)` creates long-lived immutable cache-control headers.
22
23
  - `deployment.Source` and `deployment.CacheControl` are re-exported from `aws-cdk-lib/aws-s3-deployment`.
23
24
 
@@ -26,13 +27,12 @@ This package keeps small, reusable CDK construction helpers close to the AWS pri
26
27
  This example wires the package helpers into a static website stack:
27
28
 
28
29
  - find an existing hosted zone
29
- - create a private S3 bucket for `vyriy.dev`
30
- - create a redirect S3 bucket for `www.vyriy.dev`
30
+ - create a private S3 bucket for `site.com`
31
31
  - create a DNS-validated ACM certificate
32
- - create one CloudFront distribution for the main site
33
- - create one CloudFront distribution for the `www` redirect
34
- - create Route 53 alias records
35
- - deploy local site files into the main bucket and invalidate CloudFront
32
+ - create a CloudFront distribution for the site
33
+ - create a Route 53 alias record
34
+ - deploy immutable assets and revalidating HTML files into the bucket
35
+ - invalidate CloudFront after each deployment
36
36
 
37
37
  CloudFront requires ACM certificates for custom aliases to be in `us-east-1`, so deploy this stack in `us-east-1` or split the certificate into a dedicated us-east-1 stack.
38
38
 
@@ -46,7 +46,7 @@ import { path } from '@vyriy/path';
46
46
 
47
47
  stack(
48
48
  class StaticSiteStack extends Stack {
49
- constructor(scope: Construct, id: string, props?: StackProps) {
49
+ constructor(scope: Construct, id: string, props: StackProps & { env: { account: string; region: string } }) {
50
50
  super(scope, id, props);
51
51
 
52
52
  const domain = 'site.com';
@@ -55,7 +55,7 @@ stack(
55
55
  domainName: domain,
56
56
  });
57
57
 
58
- const bucket = s3.createBucket(this, 'Bucket', {
58
+ const siteBucket = s3.createBucket(this, 'Bucket', {
59
59
  bucketName: domain,
60
60
  });
61
61
 
@@ -65,9 +65,9 @@ stack(
65
65
  validation: acm.CertificateValidation.fromDns(hostedZone),
66
66
  });
67
67
 
68
- const distribution = cf.createDistribution(this, 'Distribution', {
68
+ const siteDistribution = cf.createDistribution(this, 'Distribution', {
69
69
  certificate,
70
- defaultBehavior: cf.createDefaultBehavior(bucket),
70
+ defaultBehavior: cf.createDefaultBehavior(siteBucket),
71
71
  defaultRootObject: 'index.html',
72
72
  domainNames: [domain],
73
73
  errorResponses: [
@@ -85,17 +85,43 @@ stack(
85
85
  });
86
86
 
87
87
  route53.createARecord(this, 'RootRecord', {
88
- target: route53.createCloudFrontTarget(distribution),
88
+ target: route53.createCloudFrontTarget(siteDistribution),
89
89
  zone: hostedZone,
90
90
  });
91
91
 
92
- deployment.createBucketDeployment(this, 'DeploySite', {
92
+ const assetDeployment = deployment.createBucketDeployment(this, 'DeploySiteAssets', {
93
93
  cacheControl: deployment.createImmutableCacheControl(),
94
- destinationBucket: bucket,
95
- distribution,
94
+ destinationBucket: siteBucket,
95
+ distribution: siteDistribution,
96
+ exclude: ['index.html', '404.html'],
96
97
  distributionPaths: ['/*'],
97
98
  sources: [deployment.Source.asset(path('dist'))],
98
99
  });
100
+
101
+ const htmlDeployment = deployment.createBucketDeployment(this, 'DeploySiteHtml', {
102
+ cacheControl: deployment.createHtmlCacheControl(),
103
+ destinationBucket: siteBucket,
104
+ distribution: siteDistribution,
105
+ distributionPaths: ['/*'],
106
+ exclude: ['*'],
107
+ include: ['index.html', '404.html'],
108
+ prune: false,
109
+ sources: [deployment.Source.asset(path('dist'))],
110
+ });
111
+
112
+ htmlDeployment.node.addDependency(assetDeployment);
113
+
114
+ new CfnOutput(this, 'Account', { value: props.env.account });
115
+ new CfnOutput(this, 'Region', { value: props.env.region });
116
+ new CfnOutput(this, 'Tags', { value: JSON.stringify(props.tags ?? {}) });
117
+
118
+ new CfnOutput(this, 'BucketName', { value: siteBucket.bucketName });
119
+
120
+ new CfnOutput(this, 'DistributionDomainName', { value: siteDistribution.domainName });
121
+ new CfnOutput(this, 'DistributionId', { value: siteDistribution.distributionId });
122
+ new CfnOutput(this, 'DistributionUrl', { value: `https://${siteDistribution.domainName}/` });
123
+
124
+ new CfnOutput(this, 'SiteUrl', { value: `https://${domain}/` });
99
125
  }
100
126
  },
101
127
  );
@@ -3,3 +3,4 @@ import { BucketDeployment, BucketDeploymentProps, CacheControl, Source } from 'a
3
3
  export { Source, CacheControl };
4
4
  export declare const createBucketDeployment: (scope: Construct, id: string, props: BucketDeploymentProps) => BucketDeployment;
5
5
  export declare const createImmutableCacheControl: (days?: number) => CacheControl[];
6
+ export declare const createHtmlCacheControl: () => CacheControl[];
@@ -10,3 +10,8 @@ export const createImmutableCacheControl = (days = 365) => [
10
10
  CacheControl.maxAge(Duration.days(days)),
11
11
  CacheControl.immutable(),
12
12
  ];
13
+ export const createHtmlCacheControl = () => [
14
+ CacheControl.setPublic(),
15
+ CacheControl.maxAge(Duration.seconds(0)),
16
+ CacheControl.mustRevalidate(),
17
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vyriy/stack",
3
- "version": "0.1.16",
3
+ "version": "0.1.20",
4
4
  "description": "AWS CDK stack helpers for Vyriy infrastructure",
5
5
  "type": "module",
6
6
  "main": "./index.js",