@@ -212,22 +212,133 @@ export = {
212
212
test . done ( ) ;
213
213
} ,
214
214
215
- 'picking public subnets is not allowed ' ( test : Test ) {
215
+ 'can pick public subnet for Lambda ' ( test : Test ) {
216
216
// GIVEN
217
217
const stack = new cdk . Stack ( ) ;
218
218
const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
219
219
220
+ // WHEN
221
+ new lambda . Function ( stack , 'PublicLambda' , {
222
+ allowPublicSubnet : true ,
223
+ code : new lambda . InlineCode ( 'foo' ) ,
224
+ handler : 'index.handler' ,
225
+ runtime : lambda . Runtime . NODEJS_10_X ,
226
+ vpc,
227
+ vpcSubnets : { subnetType : ec2 . SubnetType . PUBLIC } ,
228
+ } ) ;
229
+
230
+ // THEN
231
+ expect ( stack ) . to ( haveResource ( 'AWS::Lambda::Function' , {
232
+ VpcConfig : {
233
+ SecurityGroupIds : [
234
+ { 'Fn::GetAtt' : [ 'PublicLambdaSecurityGroup61D896FD' , 'GroupId' ] } ,
235
+ ] ,
236
+ SubnetIds : [
237
+ { Ref : 'VPCPublicSubnet1SubnetB4246D30' } ,
238
+ { Ref : 'VPCPublicSubnet2Subnet74179F39' } ,
239
+ ] ,
240
+ } ,
241
+ } ) ) ;
242
+ test . done ( ) ;
243
+ } ,
244
+
245
+ 'can pick private subnet for Lambda' ( test : Test ) {
246
+ // GIVEN
247
+ const stack = new cdk . Stack ( ) ;
248
+ const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
249
+
250
+ // WHEN
251
+ new lambda . Function ( stack , 'PrivateLambda' , {
252
+ code : new lambda . InlineCode ( 'foo' ) ,
253
+ handler : 'index.handler' ,
254
+ runtime : lambda . Runtime . NODEJS_10_X ,
255
+ vpc,
256
+ vpcSubnets : { subnetType : ec2 . SubnetType . PRIVATE } ,
257
+ } ) ;
258
+
259
+ // THEN
260
+
261
+ expect ( stack ) . to ( haveResource ( 'AWS::Lambda::Function' , {
262
+ VpcConfig : {
263
+ SecurityGroupIds : [
264
+ { 'Fn::GetAtt' : [ 'PrivateLambdaSecurityGroupF53C8342' , 'GroupId' ] } ,
265
+ ] ,
266
+ SubnetIds : [
267
+ { Ref : 'VPCPrivateSubnet1Subnet8BCA10E0' } ,
268
+ { Ref : 'VPCPrivateSubnet2SubnetCFCDAA7A' } ,
269
+ ] ,
270
+ } ,
271
+ } ) ) ;
272
+ test . done ( ) ;
273
+ } ,
274
+
275
+ 'can pick isolated subnet for Lambda' ( test : Test ) {
276
+ // GIVEN
277
+ const stack = new cdk . Stack ( ) ;
278
+ const vpc = new ec2 . Vpc ( stack , 'VPC' , {
279
+ subnetConfiguration : [
280
+ {
281
+ name : 'Isolated' ,
282
+ subnetType : ec2 . SubnetType . ISOLATED ,
283
+ } ,
284
+ ] ,
285
+ } ) ;
286
+
287
+ // WHEN
288
+ new lambda . Function ( stack , 'IsolatedLambda' , {
289
+ code : new lambda . InlineCode ( 'foo' ) ,
290
+ handler : 'index.handler' ,
291
+ runtime : lambda . Runtime . NODEJS_10_X ,
292
+ vpc,
293
+ vpcSubnets : { subnetType : ec2 . SubnetType . ISOLATED } ,
294
+ } ) ;
295
+
296
+ // THEN
297
+
298
+ expect ( stack ) . to ( haveResource ( 'AWS::Lambda::Function' , {
299
+ VpcConfig : {
300
+ SecurityGroupIds : [
301
+ { 'Fn::GetAtt' : [ 'IsolatedLambdaSecurityGroupCE25B6A9' , 'GroupId' ] } ,
302
+ ] ,
303
+ SubnetIds : [
304
+ { Ref : 'VPCIsolatedSubnet1SubnetEBD00FC6' } ,
305
+ { Ref : 'VPCIsolatedSubnet2Subnet4B1C8CAA' } ,
306
+ ] ,
307
+ } ,
308
+ } ) ) ;
309
+ test . done ( ) ;
310
+ } ,
311
+
312
+ 'picking public subnet type is not allowed if not overriding allowPublicSubnet' ( test : Test ) {
313
+ // GIVEN
314
+ const stack = new cdk . Stack ( ) ;
315
+ const vpc = new ec2 . Vpc ( stack , 'VPC' , {
316
+ subnetConfiguration : [
317
+ {
318
+ name : 'Public' ,
319
+ subnetType : ec2 . SubnetType . PUBLIC ,
320
+ } ,
321
+ {
322
+ name : 'Private' ,
323
+ subnetType : ec2 . SubnetType . PRIVATE ,
324
+ } ,
325
+ {
326
+ name : 'Isolated' ,
327
+ subnetType : ec2 . SubnetType . ISOLATED ,
328
+ } ,
329
+ ] ,
330
+ } ) ;
331
+
220
332
// WHEN
221
333
test . throws ( ( ) => {
222
- new lambda . Function ( stack , 'Lambda ' , {
334
+ new lambda . Function ( stack , 'PublicLambda ' , {
223
335
code : new lambda . InlineCode ( 'foo' ) ,
224
336
handler : 'index.handler' ,
225
337
runtime : lambda . Runtime . NODEJS_10_X ,
226
338
vpc,
227
339
vpcSubnets : { subnetType : ec2 . SubnetType . PUBLIC } ,
228
340
} ) ;
229
- } ) ;
230
-
341
+ } , / L a m b d a F u n c t i o n s i n a p u b l i c s u b n e t / ) ;
231
342
test . done ( ) ;
232
343
} ,
233
344
} ;
0 commit comments