@vyriy/stack 0.1.23 → 0.2.0
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 +9 -2
- package/cloudfront/index.d.ts +11 -3
- package/cloudfront/index.js +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,8 @@ The `aws-cdk-lib` package is listed because CDK apps and bin entrypoints use CDK
|
|
|
28
28
|
- `cf.createDefaultBehavior(bucket, options?)` creates a CloudFront S3 origin behavior that redirects viewers to HTTPS.
|
|
29
29
|
- `cf.createWebsiteRedirectBehavior(bucket, options?)` creates a CloudFront behavior for an S3 website redirect bucket.
|
|
30
30
|
- `cf.createCloudFrontFunction(scope, id, props)` creates a CloudFront Function from inline JavaScript source.
|
|
31
|
+
- `cf.createIndexRewriteFunctionCode({ rootDomain, wwwDomain })` creates CloudFront Function source for www redirects and clean URL index rewrites.
|
|
32
|
+
- `cf.createFunctionAssociations(scope, id, { rootDomain, wwwDomain, ...props })` creates a viewer-request CloudFront Function association for www redirects and index rewrites.
|
|
31
33
|
- `cf.createDistribution(scope, id, props)` creates a CloudFront distribution.
|
|
32
34
|
- `route53.getHostedZone(scope, id, props)` looks up an existing Route 53 hosted zone.
|
|
33
35
|
- `route53.createARecord(scope, id, props)` creates a Route 53 A record.
|
|
@@ -83,9 +85,14 @@ stack(
|
|
|
83
85
|
|
|
84
86
|
const siteDistribution = cf.createDistribution(this, 'Distribution', {
|
|
85
87
|
certificate,
|
|
86
|
-
defaultBehavior: cf.createDefaultBehavior(siteBucket
|
|
88
|
+
defaultBehavior: cf.createDefaultBehavior(siteBucket, {
|
|
89
|
+
functionAssociations: cf.createFunctionAssociations(this, 'IndexRewriteFunction', {
|
|
90
|
+
rootDomain: domain,
|
|
91
|
+
wwwDomain: `www.${domain}`,
|
|
92
|
+
}),
|
|
93
|
+
}),
|
|
87
94
|
defaultRootObject: 'index.html',
|
|
88
|
-
domainNames: [domain],
|
|
95
|
+
domainNames: [domain, `www.${domain}`],
|
|
89
96
|
errorResponses: [
|
|
90
97
|
{
|
|
91
98
|
httpStatus: 403,
|
package/cloudfront/index.d.ts
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { Construct } from 'constructs';
|
|
2
|
-
import { AddBehaviorOptions, Distribution, DistributionProps, Function as CloudFrontFunction, FunctionEventType, FunctionProps, OriginProtocolPolicy, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
|
|
2
|
+
import { AddBehaviorOptions, Distribution, DistributionProps, Function as CloudFrontFunction, FunctionAssociation, FunctionEventType, FunctionProps, OriginProtocolPolicy, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
|
|
3
3
|
import { HttpOrigin } from 'aws-cdk-lib/aws-cloudfront-origins';
|
|
4
4
|
import { Bucket } from 'aws-cdk-lib/aws-s3';
|
|
5
5
|
export { FunctionEventType, OriginProtocolPolicy, ViewerProtocolPolicy };
|
|
6
6
|
export interface CreateCloudFrontFunctionProps extends Omit<FunctionProps, 'code'> {
|
|
7
7
|
readonly code: string;
|
|
8
8
|
}
|
|
9
|
+
export interface CreateIndexRewriteFunctionCodeProps {
|
|
10
|
+
readonly rootDomain: string;
|
|
11
|
+
readonly wwwDomain: string;
|
|
12
|
+
}
|
|
13
|
+
export interface CreateFunctionAssociationsProps extends CreateIndexRewriteFunctionCodeProps, Omit<CreateCloudFrontFunctionProps, 'code'> {
|
|
14
|
+
}
|
|
9
15
|
export declare const createDistribution: (scope: Construct, id: string, props: DistributionProps) => Distribution;
|
|
10
16
|
export declare const createCloudFrontFunction: (scope: Construct, id: string, props: CreateCloudFrontFunctionProps) => CloudFrontFunction;
|
|
17
|
+
export declare const createIndexRewriteFunctionCode: ({ rootDomain, wwwDomain }: CreateIndexRewriteFunctionCodeProps) => string;
|
|
18
|
+
export declare const createFunctionAssociations: (scope: Construct, id: string, { rootDomain, wwwDomain, ...props }: CreateFunctionAssociationsProps) => FunctionAssociation[];
|
|
11
19
|
export declare const createDefaultBehavior: (bucket: Bucket, options?: AddBehaviorOptions) => {
|
|
12
20
|
allowedMethods?: import("aws-cdk-lib/aws-cloudfront").AllowedMethods;
|
|
13
21
|
cachedMethods?: import("aws-cdk-lib/aws-cloudfront").CachedMethods;
|
|
@@ -18,7 +26,7 @@ export declare const createDefaultBehavior: (bucket: Bucket, options?: AddBehavi
|
|
|
18
26
|
responseHeadersPolicy?: import("aws-cdk-lib/aws-cloudfront").IResponseHeadersPolicyRef;
|
|
19
27
|
smoothStreaming?: boolean;
|
|
20
28
|
viewerProtocolPolicy: ViewerProtocolPolicy;
|
|
21
|
-
functionAssociations?:
|
|
29
|
+
functionAssociations?: FunctionAssociation[];
|
|
22
30
|
edgeLambdas?: import("aws-cdk-lib/aws-cloudfront").EdgeLambda[];
|
|
23
31
|
trustedKeyGroups?: import("aws-cdk-lib/aws-cloudfront").IKeyGroupRef[];
|
|
24
32
|
enableGrpc?: boolean;
|
|
@@ -34,7 +42,7 @@ export declare const createWebsiteRedirectBehavior: (bucket: Bucket, options?: A
|
|
|
34
42
|
responseHeadersPolicy?: import("aws-cdk-lib/aws-cloudfront").IResponseHeadersPolicyRef;
|
|
35
43
|
smoothStreaming?: boolean;
|
|
36
44
|
viewerProtocolPolicy: ViewerProtocolPolicy;
|
|
37
|
-
functionAssociations?:
|
|
45
|
+
functionAssociations?: FunctionAssociation[];
|
|
38
46
|
edgeLambdas?: import("aws-cdk-lib/aws-cloudfront").EdgeLambda[];
|
|
39
47
|
trustedKeyGroups?: import("aws-cdk-lib/aws-cloudfront").IKeyGroupRef[];
|
|
40
48
|
enableGrpc?: boolean;
|
package/cloudfront/index.js
CHANGED
|
@@ -6,6 +6,44 @@ export const createCloudFrontFunction = (scope, id, props) => new CloudFrontFunc
|
|
|
6
6
|
...props,
|
|
7
7
|
code: FunctionCode.fromInline(props.code),
|
|
8
8
|
});
|
|
9
|
+
export const createIndexRewriteFunctionCode = ({ rootDomain, wwwDomain }) => `
|
|
10
|
+
function handler(event) {
|
|
11
|
+
var request = event.request;
|
|
12
|
+
var host = request.headers.host.value;
|
|
13
|
+
|
|
14
|
+
if (host === ${JSON.stringify(wwwDomain)}) {
|
|
15
|
+
return {
|
|
16
|
+
statusCode: 301,
|
|
17
|
+
statusDescription: 'Moved Permanently',
|
|
18
|
+
headers: {
|
|
19
|
+
location: {
|
|
20
|
+
value: ${JSON.stringify(`https://${rootDomain}`)} + request.uri,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var uri = request.uri;
|
|
27
|
+
var lastSegment = uri.split('/').pop();
|
|
28
|
+
|
|
29
|
+
if (uri.endsWith('/')) {
|
|
30
|
+
request.uri = uri + 'index.html';
|
|
31
|
+
} else if (!lastSegment.includes('.')) {
|
|
32
|
+
request.uri = uri + '/index.html';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return request;
|
|
36
|
+
}
|
|
37
|
+
`;
|
|
38
|
+
export const createFunctionAssociations = (scope, id, { rootDomain, wwwDomain, ...props }) => [
|
|
39
|
+
{
|
|
40
|
+
eventType: FunctionEventType.VIEWER_REQUEST,
|
|
41
|
+
function: createCloudFrontFunction(scope, id, {
|
|
42
|
+
...props,
|
|
43
|
+
code: createIndexRewriteFunctionCode({ rootDomain, wwwDomain }),
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
];
|
|
9
47
|
export const createDefaultBehavior = (bucket, options = {}) => ({
|
|
10
48
|
origin: S3BucketOrigin.withOriginAccessControl(bucket),
|
|
11
49
|
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
|