@@ -515,7 +515,7 @@ export class Function extends FunctionBase {
515
515
/**
516
516
* Environment variables for this function
517
517
*/
518
- private readonly environment : { [ key : string ] : string } ;
518
+ private environment : { [ key : string ] : EnvironmentConfig } = { } ;
519
519
520
520
private readonly currentVersionOptions ?: VersionOptions ;
521
521
private _currentVersion ?: Version ;
@@ -558,7 +558,7 @@ export class Function extends FunctionBase {
558
558
const code = props . code . bind ( this ) ;
559
559
verifyCodeConfig ( code , props . runtime ) ;
560
560
561
- let profilingGroupEnvironmentVariables = { } ;
561
+ let profilingGroupEnvironmentVariables : { [ key : string ] : string } = { } ;
562
562
if ( props . profilingGroup && props . profiling !== false ) {
563
563
this . validateProfilingEnvironmentVariables ( props ) ;
564
564
props . profilingGroup . grantPublish ( this . role ) ;
@@ -582,7 +582,10 @@ export class Function extends FunctionBase {
582
582
} ;
583
583
}
584
584
585
- this . environment = { ...profilingGroupEnvironmentVariables , ...( props . environment || { } ) } ;
585
+ const env = { ...profilingGroupEnvironmentVariables , ...props . environment } ;
586
+ for ( const [ key , value ] of Object . entries ( env ) ) {
587
+ this . addEnvironment ( key , value ) ;
588
+ }
586
589
587
590
this . deadLetterQueue = this . buildDeadLetterQueue ( props ) ;
588
591
@@ -675,9 +678,10 @@ export class Function extends FunctionBase {
675
678
* If this is a ref to a Lambda function, this operation results in a no-op.
676
679
* @param key The environment variable key.
677
680
* @param value The environment variable's value.
681
+ * @param options Environment variable options.
678
682
*/
679
- public addEnvironment ( key : string , value : string ) : this {
680
- this . environment [ key ] = value ;
683
+ public addEnvironment ( key : string , value : string , options ?: EnvironmentOptions ) : this {
684
+ this . environment [ key ] = { value, ... options } ;
681
685
return this ;
682
686
}
683
687
@@ -764,27 +768,43 @@ export class Function extends FunctionBase {
764
768
return this . _logGroup ;
765
769
}
766
770
771
+ /** @internal */
772
+ public _checkEdgeCompatibility ( ) : void {
773
+ // Check env vars
774
+ const envEntries = Object . entries ( this . environment ) ;
775
+ for ( const [ key , config ] of envEntries ) {
776
+ if ( config . removeInEdge ) {
777
+ delete this . environment [ key ] ;
778
+ this . node . addInfo ( `Removed ${ key } environment variable for Lambda@Edge compatibility` ) ;
779
+ }
780
+ }
781
+ const envKeys = Object . keys ( this . environment ) ;
782
+ if ( envKeys . length !== 0 ) {
783
+ throw new Error ( `The function ${ this . node . path } contains environment variables [${ envKeys } ] and is not compatible with Lambda@Edge. \
784
+ Environment variables can be marked for removal when used in Lambda@Edge by setting the \'removeInEdge\' property in the \'addEnvironment()\' API.` ) ;
785
+ }
786
+
787
+ return ;
788
+ }
789
+
767
790
private renderEnvironment ( ) {
768
791
if ( ! this . environment || Object . keys ( this . environment ) . length === 0 ) {
769
792
return undefined ;
770
793
}
771
794
772
- // for backwards compatibility we do not sort environment variables in case
773
- // _currentVersion is not defined. otherwise, this would have invalidated
795
+ const variables : { [ key : string ] : string } = { } ;
796
+ // Sort environment so the hash of the function used to create
797
+ // `currentVersion` is not affected by key order (this is how lambda does
798
+ // it). For backwards compatibility we do not sort environment variables in case
799
+ // _currentVersion is not defined. Otherwise, this would have invalidated
774
800
// the template, and for example, may cause unneeded updates for nested
775
801
// stacks.
776
- if ( ! this . _currentVersion ) {
777
- return {
778
- variables : this . environment ,
779
- } ;
780
- }
802
+ const keys = this . _currentVersion
803
+ ? Object . keys ( this . environment ) . sort ( )
804
+ : Object . keys ( this . environment ) ;
781
805
782
- // sort environment so the hash of the function used to create
783
- // `currentVersion` is not affected by key order (this is how lambda does
784
- // it).
785
- const variables : { [ key : string ] : string } = { } ;
786
- for ( const key of Object . keys ( this . environment ) . sort ( ) ) {
787
- variables [ key ] = this . environment [ key ] ;
806
+ for ( const key of keys ) {
807
+ variables [ key ] = this . environment [ key ] . value ;
788
808
}
789
809
790
810
return { variables } ;
@@ -905,6 +925,27 @@ export class Function extends FunctionBase {
905
925
}
906
926
}
907
927
928
+ /**
929
+ * Environment variables options
930
+ */
931
+ export interface EnvironmentOptions {
932
+ /**
933
+ * When used in Lambda@Edge via edgeArn() API, these environment
934
+ * variables will be removed. If not set, an error will be thrown.
935
+ * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-requirements-lambda-function-configuration
936
+ *
937
+ * @default false - using the function in Lambda@Edge will throw
938
+ */
939
+ readonly removeInEdge ?: boolean
940
+ }
941
+
942
+ /**
943
+ * Configuration for an environment variable
944
+ */
945
+ interface EnvironmentConfig extends EnvironmentOptions {
946
+ readonly value : string ;
947
+ }
948
+
908
949
/**
909
950
* Given an opaque (token) ARN, returns a CloudFormation expression that extracts the function
910
951
* name from the ARN.
0 commit comments