@@ -144,4 +144,38 @@ def handler(event, context):
144
144
// THEN
145
145
Template . fromStack ( stack ) . resourceCountIs ( 'AWS::CloudWatch::Alarm' , 2 ) ;
146
146
Template . fromStack ( stack ) . resourceCountIs ( 'AWS::Lambda::Permission' , 2 ) ;
147
+ } ) ;
148
+
149
+ test ( 'throws when multiple alarms are created for the same lambda if feature flag is set to false' , ( ) => {
150
+ // GIVEN
151
+ const stack = new Stack ( ) ;
152
+ stack . node . setContext ( LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION , false ) ; // Default, but explicit just in case.
153
+ const alarm1 = new cloudwatch . Alarm ( stack , 'Alarm1' , {
154
+ metric : new cloudwatch . Metric ( { namespace : 'AWS' , metricName : 'Test' } ) ,
155
+ evaluationPeriods : 3 ,
156
+ threshold : 100 ,
157
+ } ) ;
158
+ const alarm2 = new cloudwatch . Alarm ( stack , 'Alarm2' , {
159
+ metric : new cloudwatch . Metric ( { namespace : 'AWS' , metricName : 'Test2' } ) ,
160
+ evaluationPeriods : 3 ,
161
+ threshold : 100 ,
162
+ } ) ;
163
+
164
+ // WHEN
165
+ const alarmLambda = new lambda . Function ( stack , 'alarmLambda' , {
166
+ runtime : lambda . Runtime . PYTHON_3_12 ,
167
+ functionName : 'alarmLambda' ,
168
+ code : lambda . Code . fromInline ( `
169
+ def handler(event, context):
170
+ print('event:', event)
171
+ print('.............................................')
172
+ print('context:', context)` ) ,
173
+ handler : 'index.handler' ,
174
+ } ) ;
175
+ alarm1 . addAlarmAction ( new actions . LambdaAction ( alarmLambda ) ) ;
176
+
177
+ // THEN
178
+ expect ( ( ) => {
179
+ alarm2 . addAlarmAction ( new actions . LambdaAction ( alarmLambda ) ) ;
180
+ } ) . toThrow ( / T h e r e i s a l r e a d y a C o n s t r u c t w i t h n a m e ' A l a r m P e r m i s s i o n ' i n F u n c t i o n \[ a l a r m L a m b d a \] / ) ;
147
181
} ) ;
0 commit comments