11/*
2- Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
4- Licensed under the Apache License, Version 2.0 (the "License").
5- You may not use this file except in compliance with the License.
6- You may obtain a copy of the License at
7-
8- http://www.apache.org/licenses/LICENSE-2.0
9-
10- Unless required by applicable law or agreed to in writing, software
11- distributed under the License is distributed on an "AS IS" BASIS,
12- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- See the License for the specific language governing permissions and
14- limitations under the License.
2+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ Licensed under the Apache License, Version 2.0 (the "License").
4+ You may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+ http://www.apache.org/licenses/LICENSE-2.0
7+ Unless required by applicable law or agreed to in writing, software
8+ distributed under the License is distributed on an "AS IS" BASIS,
9+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+ See the License for the specific language governing permissions and
11+ limitations under the License.
1512 */
16-
17- import {
13+ import {
1814 Aws ,
1915 aws_cloudfront as cloudfront ,
16+ aws_s3 as s3
2017} from "aws-cdk-lib" ;
2118import * as path from "path" ;
2219import * as fs from "fs" ;
23-
2420import { Construct } from "constructs" ;
2521import { HostingInfrastructure } from "./hosting_infrastructure" ;
2622import { PipelineInfrastructure } from "./pipeline_infrastructure" ;
2723import { HostingConfiguration } from "../bin/cli/shared/types" ;
2824import { truncateString } from "./utility" ;
25+
2926interface IParamProps {
3027 hostingConfiguration : HostingConfiguration ;
3128 buildFilePath : string ;
3229 cffSourceFilePath : string ;
33- connectionArn ?: string ;
30+ connectionArn ?: string ;
3431 certificateArn ?: string ;
3532}
3633
@@ -43,50 +40,94 @@ interface IParamProps {
4340 * @param scope - The Construct scope in which this construct is defined.
4441 * @param id - The identifier for this construct within the scope.
4542 * @param params - Parameters for configuring hosting resources.
46- * - `configuration` (required): The IConfiguration object representing the hosting configuration.
47- * - `buildFilePath` (required): The path to the build file for the hosting resources.
48- * - `connectionArn` (optional): The ARN of the connection resource (if applicable).
49- * - `certificateArn` (optional): The ARN of the certificate resource (if applicable).
5043 */
5144export class Hosting extends Construct {
45+ /**
46+ * The CloudFront Distribution created by this construct.
47+ */
48+ public readonly distribution : cloudfront . Distribution ;
49+
50+ /**
51+ * The S3 Bucket used for hosting the content.
52+ */
53+ public readonly hostingBucket : s3 . IBucket ;
54+
55+ /**
56+ * The CloudFront Function used for URI manipulation.
57+ */
58+ public readonly changeUriFunction : cloudfront . Function ;
59+
60+ /**
61+ * The Key-Value Store used by CloudFront.
62+ */
63+ public readonly uriStore : cloudfront . KeyValueStore ;
64+
65+ /**
66+ * The distribution's domain name.
67+ */
68+ public readonly distributionDomainName : string ;
69+
70+ /**
71+ * The distribution's URL (https://{domainName}).
72+ */
73+ public readonly distributionUrl : string ;
74+
75+ /**
76+ * Reference to the hosting infrastructure construct.
77+ */
78+ public readonly hostingInfrastructure : HostingInfrastructure ;
79+
80+ /**
81+ * Reference to the pipeline infrastructure construct.
82+ */
83+ public readonly pipelineInfrastructure : PipelineInfrastructure ;
84+
5285 constructor ( scope : Construct , id : string , params : IParamProps ) {
5386 super ( scope , id ) ;
5487
55- const uriStore = new cloudfront . KeyValueStore ( this , 'UriStore' , {
88+ // Create URI Store
89+ this . uriStore = new cloudfront . KeyValueStore ( this , 'UriStore' , {
5690 keyValueStoreName : truncateString ( Aws . STACK_NAME + "-" + Aws . REGION , 65 )
5791 } ) ;
5892
93+ // Setup CloudFront Function
5994 let cloudFrontFunctionCode = fs . readFileSync ( params . cffSourceFilePath , 'utf-8' ) ;
95+ cloudFrontFunctionCode = cloudFrontFunctionCode . replace ( / _ _ K V S _ I D _ _ / g, this . uriStore . keyValueStoreId ) ;
6096
61- cloudFrontFunctionCode = cloudFrontFunctionCode . replace ( / _ _ K V S _ I D _ _ / g, uriStore . keyValueStoreId ) ;
62-
63- const changeUri = new cloudfront . Function ( this , "ChangeUri" , {
97+ this . changeUriFunction = new cloudfront . Function ( this , "ChangeUri" , {
6498 code : cloudfront . FunctionCode . fromInline ( cloudFrontFunctionCode ) ,
65- runtime : cloudfront . FunctionRuntime . JS_2_0 ,
99+ runtime : cloudfront . FunctionRuntime . JS_2_0 ,
66100 comment : "Change uri" ,
67-
68101 } ) ;
69102
70-
71- ( changeUri . node . defaultChild as cloudfront . CfnFunction ) . addPropertyOverride ( "FunctionConfig.KeyValueStoreAssociations" ,
72- [ {
73- "KeyValueStoreARN" : uriStore . keyValueStoreArn
74- } ] ) ;
75-
103+ ( this . changeUriFunction . node . defaultChild as cloudfront . CfnFunction ) . addPropertyOverride (
104+ "FunctionConfig.KeyValueStoreAssociations" ,
105+ [ {
106+ "KeyValueStoreARN" : this . uriStore . keyValueStoreArn
107+ } ]
108+ ) ;
76109
77- const hostingInfrastructure = new HostingInfrastructure ( this , "HostingInfrastructure" , {
78- changeUri : changeUri ,
110+ // Create Hosting Infrastructure
111+ this . hostingInfrastructure = new HostingInfrastructure ( this , "HostingInfrastructure" , {
112+ changeUri : this . changeUriFunction ,
79113 certificateArn : params . certificateArn ,
80114 hostingConfiguration : params . hostingConfiguration ,
81115 } ) ;
82116
83- new PipelineInfrastructure ( this , "PipelineInfrastructure" , {
117+ // Set properties from hosting infrastructure
118+ this . distribution = this . hostingInfrastructure . distribution ;
119+ this . hostingBucket = this . hostingInfrastructure . hostingBucket ;
120+ this . distributionDomainName = this . distribution . distributionDomainName ;
121+ this . distributionUrl = `https://${ this . distributionDomainName } ` ;
122+
123+ // Create Pipeline Infrastructure
124+ this . pipelineInfrastructure = new PipelineInfrastructure ( this , "PipelineInfrastructure" , {
84125 hostingConfiguration : params . hostingConfiguration ,
85126 connectionArn : params . connectionArn ,
86- kvsArn : uriStore . keyValueStoreArn ,
87- hostingBucket : hostingInfrastructure . hostingBucket ,
88- changeUri : changeUri ,
127+ kvsArn : this . uriStore . keyValueStoreArn ,
128+ hostingBucket : this . hostingBucket ,
129+ changeUri : this . changeUriFunction ,
89130 buildFilePath : params . buildFilePath ,
90131 } ) ;
91132 }
92- }
133+ }
0 commit comments