@@ -23,16 +23,11 @@ import {
23
23
SsmEnvironmentEntry ,
24
24
StackProvider ,
25
25
} from '@aws-amplify/plugin-types' ;
26
- import { Duration , Stack , Tags } from 'aws-cdk-lib' ;
26
+ import { Duration , Lazy , Stack , Tags , Token } from 'aws-cdk-lib' ;
27
27
import { Rule } from 'aws-cdk-lib/aws-events' ;
28
28
import * as targets from 'aws-cdk-lib/aws-events-targets' ;
29
29
import { Policy } from 'aws-cdk-lib/aws-iam' ;
30
- import {
31
- CfnFunction ,
32
- ILayerVersion ,
33
- LayerVersion ,
34
- Runtime ,
35
- } from 'aws-cdk-lib/aws-lambda' ;
30
+ import { CfnFunction , LayerVersion , Runtime } from 'aws-cdk-lib/aws-lambda' ;
36
31
import { NodejsFunction , OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs' ;
37
32
import { Construct } from 'constructs' ;
38
33
import { readFileSync } from 'fs' ;
@@ -361,19 +356,10 @@ class FunctionGenerator implements ConstructContainerEntryGenerator {
361
356
scope,
362
357
backendSecretResolver,
363
358
} : GenerateContainerEntryProps ) => {
364
- // resolve layers to LayerVersion objects for the NodejsFunction constructor using the scope.
365
- const resolvedLayers = Object . entries ( this . props . layers ) . map ( ( [ key , arn ] ) =>
366
- LayerVersion . fromLayerVersionArn (
367
- scope ,
368
- `${ this . props . name } -${ key } -layer` ,
369
- arn
370
- )
371
- ) ;
372
-
373
359
return new AmplifyFunction (
374
360
scope ,
375
361
this . props . name ,
376
- { ... this . props , resolvedLayers } ,
362
+ this . props ,
377
363
backendSecretResolver ,
378
364
this . outputStorageStrategy
379
365
) ;
@@ -393,14 +379,39 @@ class AmplifyFunction
393
379
constructor (
394
380
scope : Construct ,
395
381
id : string ,
396
- props : HydratedFunctionProps & { resolvedLayers : ILayerVersion [ ] } ,
382
+ props : HydratedFunctionProps ,
397
383
backendSecretResolver : BackendSecretResolver ,
398
384
outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput >
399
385
) {
400
386
super ( scope , id ) ;
401
387
402
388
this . stack = Stack . of ( scope ) ;
403
389
390
+ // resolve layers to LayerVersion objects for the NodejsFunction constructor using the scope.
391
+ const resolvedLayers = Object . entries ( props . layers ) . map ( ( [ key , arn ] ) => {
392
+ const layerRegion = arn . split ( ':' ) [ 3 ] ;
393
+ // If region is an unresolved token, use lazy to get region
394
+ const region = Token . isUnresolved ( this . stack . region )
395
+ ? Lazy . string ( {
396
+ produce : ( ) => this . stack . region ,
397
+ } )
398
+ : this . stack . region ;
399
+
400
+ if ( layerRegion !== region ) {
401
+ throw new AmplifyUserError ( 'InvalidLayerArnRegionError' , {
402
+ message : `Region in ARN does not match function region for layer: ${ key } ` ,
403
+ resolution :
404
+ 'Update the layer ARN with the same region as the function' ,
405
+ } ) ;
406
+ }
407
+
408
+ return LayerVersion . fromLayerVersionArn (
409
+ scope ,
410
+ `${ props . name } -${ key } -layer` ,
411
+ arn
412
+ ) ;
413
+ } ) ;
414
+
404
415
const runtime = nodeVersionMap [ props . runtime ] ;
405
416
406
417
const require = createRequire ( import . meta. url ) ;
@@ -449,7 +460,7 @@ class AmplifyFunction
449
460
timeout : Duration . seconds ( props . timeoutSeconds ) ,
450
461
memorySize : props . memoryMB ,
451
462
runtime : nodeVersionMap [ props . runtime ] ,
452
- layers : props . resolvedLayers ,
463
+ layers : resolvedLayers ,
453
464
bundling : {
454
465
...props . bundling ,
455
466
banner : bannerCode ,
0 commit comments