@@ -2,7 +2,7 @@ import * as acm from '@aws-cdk/aws-certificatemanager';
2
2
import * as lambda from '@aws-cdk/aws-lambda' ;
3
3
import { Construct , IResource , Lazy , Resource , Stack , Token , Duration } from '@aws-cdk/core' ;
4
4
import { CfnDistribution } from './cloudfront.generated' ;
5
- import { Origin } from './origin' ;
5
+ import { IOrigin , OriginBindConfig , OriginBindOptions } from './origin' ;
6
6
import { CacheBehavior } from './private/cache-behavior' ;
7
7
8
8
/**
@@ -53,6 +53,10 @@ export interface DistributionAttributes {
53
53
readonly distributionId : string ;
54
54
}
55
55
56
+ interface BoundOrigin extends OriginBindOptions , OriginBindConfig {
57
+ readonly origin : IOrigin ;
58
+ }
59
+
56
60
/**
57
61
* Properties for a Distribution
58
62
*
@@ -127,7 +131,7 @@ export class Distribution extends Resource implements IDistribution {
127
131
128
132
private readonly defaultBehavior : CacheBehavior ;
129
133
private readonly additionalBehaviors : CacheBehavior [ ] = [ ] ;
130
- private readonly origins : Set < Origin > = new Set < Origin > ( ) ;
134
+ private readonly boundOrigins : BoundOrigin [ ] = [ ] ;
131
135
132
136
private readonly errorResponses : ErrorResponse [ ] ;
133
137
private readonly certificate ?: acm . ICertificate ;
@@ -142,8 +146,8 @@ export class Distribution extends Resource implements IDistribution {
142
146
}
143
147
}
144
148
145
- this . defaultBehavior = new CacheBehavior ( { pathPattern : '*' , ... props . defaultBehavior } ) ;
146
- this . addOrigin ( this . defaultBehavior . origin ) ;
149
+ const originId = this . addOrigin ( props . defaultBehavior . origin ) ;
150
+ this . defaultBehavior = new CacheBehavior ( originId , { pathPattern : '*' , ... props . defaultBehavior } ) ;
147
151
if ( props . additionalBehaviors ) {
148
152
Object . entries ( props . additionalBehaviors ) . forEach ( ( [ pathPattern , behaviorOptions ] ) => {
149
153
this . addBehavior ( pathPattern , behaviorOptions . origin , behaviorOptions ) ;
@@ -172,26 +176,38 @@ export class Distribution extends Resource implements IDistribution {
172
176
* Adds a new behavior to this distribution for the given pathPattern.
173
177
*
174
178
* @param pathPattern the path pattern (e.g., 'images/*') that specifies which requests to apply the behavior to.
179
+ * @param origin the origin to use for this behavior
175
180
* @param behaviorOptions the options for the behavior at this path.
176
181
*/
177
- public addBehavior ( pathPattern : string , origin : Origin , behaviorOptions : AddBehaviorOptions = { } ) {
182
+ public addBehavior ( pathPattern : string , origin : IOrigin , behaviorOptions : AddBehaviorOptions = { } ) {
178
183
if ( pathPattern === '*' ) {
179
184
throw new Error ( 'Only the default behavior can have a path pattern of \'*\'' ) ;
180
185
}
181
- this . additionalBehaviors . push ( new CacheBehavior ( { pathPattern , origin , ... behaviorOptions } ) ) ;
182
- this . addOrigin ( origin ) ;
186
+ const originId = this . addOrigin ( origin ) ;
187
+ this . additionalBehaviors . push ( new CacheBehavior ( originId , { pathPattern , ... behaviorOptions } ) ) ;
183
188
}
184
189
185
- private addOrigin ( origin : Origin ) {
186
- if ( ! this . origins . has ( origin ) ) {
187
- this . origins . add ( origin ) ;
188
- origin . bind ( this , { originIndex : this . origins . size } ) ;
190
+ private addOrigin ( origin : IOrigin ) : string {
191
+ const existingOrigin = this . boundOrigins . find ( boundOrigin => boundOrigin . origin === origin ) ;
192
+ if ( existingOrigin ) {
193
+ return existingOrigin . originId ;
194
+ } else {
195
+ const originIndex = this . boundOrigins . length + 1 ;
196
+ const scope = new Construct ( this , `Origin${ originIndex } ` ) ;
197
+ const originId = scope . node . uniqueId ;
198
+ const originBindConfig = origin . bind ( scope , { originId } ) ;
199
+ this . boundOrigins . push ( { origin, originId, ...originBindConfig } ) ;
200
+ return originId ;
189
201
}
190
202
}
191
203
192
204
private renderOrigins ( ) : CfnDistribution . OriginProperty [ ] {
193
205
const renderedOrigins : CfnDistribution . OriginProperty [ ] = [ ] ;
194
- this . origins . forEach ( origin => renderedOrigins . push ( origin . renderOrigin ( ) ) ) ;
206
+ this . boundOrigins . forEach ( boundOrigin => {
207
+ if ( boundOrigin . originProperty ) {
208
+ renderedOrigins . push ( boundOrigin . originProperty ) ;
209
+ }
210
+ } ) ;
195
211
return renderedOrigins ;
196
212
}
197
213
@@ -229,7 +245,6 @@ export class Distribution extends Resource implements IDistribution {
229
245
minimumProtocolVersion : SecurityPolicyProtocol . TLS_V1_2_2018 ,
230
246
} ;
231
247
}
232
-
233
248
}
234
249
235
250
/**
@@ -443,5 +458,5 @@ export interface BehaviorOptions extends AddBehaviorOptions {
443
458
/**
444
459
* The origin that you want CloudFront to route requests to when they match this behavior.
445
460
*/
446
- readonly origin : Origin ;
461
+ readonly origin : IOrigin ;
447
462
}
0 commit comments