@@ -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' ;
@@ -350,19 +345,10 @@ class FunctionGenerator implements ConstructContainerEntryGenerator {
350
345
scope,
351
346
backendSecretResolver,
352
347
} : GenerateContainerEntryProps ) => {
353
- // resolve layers to LayerVersion objects for the NodejsFunction constructor using the scope.
354
- const resolvedLayers = Object . entries ( this . props . layers ) . map ( ( [ key , arn ] ) =>
355
- LayerVersion . fromLayerVersionArn (
356
- scope ,
357
- `${ this . props . name } -${ key } -layer` ,
358
- arn
359
- )
360
- ) ;
361
-
362
348
return new AmplifyFunction (
363
349
scope ,
364
350
this . props . name ,
365
- { ... this . props , resolvedLayers } ,
351
+ this . props ,
366
352
backendSecretResolver ,
367
353
this . outputStorageStrategy
368
354
) ;
@@ -382,14 +368,39 @@ class AmplifyFunction
382
368
constructor (
383
369
scope : Construct ,
384
370
id : string ,
385
- props : HydratedFunctionProps & { resolvedLayers : ILayerVersion [ ] } ,
371
+ props : HydratedFunctionProps ,
386
372
backendSecretResolver : BackendSecretResolver ,
387
373
outputStorageStrategy : BackendOutputStorageStrategy < FunctionOutput >
388
374
) {
389
375
super ( scope , id ) ;
390
376
391
377
this . stack = Stack . of ( scope ) ;
392
378
379
+ // resolve layers to LayerVersion objects for the NodejsFunction constructor using the scope.
380
+ const resolvedLayers = Object . entries ( props . layers ) . map ( ( [ key , arn ] ) => {
381
+ const layerRegion = arn . split ( ':' ) [ 3 ] ;
382
+ // If region is an unresolved token, use lazy to get region
383
+ const region = Token . isUnresolved ( this . stack . region )
384
+ ? Lazy . string ( {
385
+ produce : ( ) => this . stack . region ,
386
+ } )
387
+ : this . stack . region ;
388
+
389
+ if ( layerRegion !== region ) {
390
+ throw new AmplifyUserError ( 'InvalidLayerArnRegionError' , {
391
+ message : `Region in ARN does not match function region for layer: ${ key } ` ,
392
+ resolution :
393
+ 'Update the layer ARN with the same region as the function' ,
394
+ } ) ;
395
+ }
396
+
397
+ return LayerVersion . fromLayerVersionArn (
398
+ scope ,
399
+ `${ props . name } -${ key } -layer` ,
400
+ arn
401
+ ) ;
402
+ } ) ;
403
+
393
404
const runtime = nodeVersionMap [ props . runtime ] ;
394
405
395
406
const require = createRequire ( import . meta. url ) ;
@@ -432,7 +443,7 @@ class AmplifyFunction
432
443
timeout : Duration . seconds ( props . timeoutSeconds ) ,
433
444
memorySize : props . memoryMB ,
434
445
runtime : nodeVersionMap [ props . runtime ] ,
435
- layers : props . resolvedLayers ,
446
+ layers : resolvedLayers ,
436
447
bundling : {
437
448
...props . bundling ,
438
449
banner : bannerCode ,
0 commit comments