File tree Expand file tree Collapse file tree 2 files changed +45
-3
lines changed
packages/@aws-cdk/aws-lambda
lib/log-retention-provider Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -49,9 +49,16 @@ export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent
49
49
// group for this function should already exist at this stage because we
50
50
// already logged the event but due to the async nature of Lambda logging
51
51
// there could be a race condition. So we also try to create the log group
52
- // of this function first.
53
- await createLogGroupSafe ( `/aws/lambda/${ context . functionName } ` ) ;
54
- await setRetentionPolicy ( `/aws/lambda/${ context . functionName } ` , 1 ) ;
52
+ // of this function first. If multiple LogRetention constructs are present
53
+ // in the stack, they will try to act on this function's log group at the
54
+ // same time. This can sometime result in an OperationAbortedException. To
55
+ // avoid this and because this operation is not critical we catch all errors.
56
+ try {
57
+ await createLogGroupSafe ( `/aws/lambda/${ context . functionName } ` ) ;
58
+ await setRetentionPolicy ( `/aws/lambda/${ context . functionName } ` , 1 ) ;
59
+ } catch ( e ) {
60
+ console . log ( e ) ;
61
+ }
55
62
}
56
63
}
57
64
Original file line number Diff line number Diff line change
1
+ import AWSSDK = require( 'aws-sdk' ) ;
1
2
import AWS = require( 'aws-sdk-mock' ) ;
2
3
import nock = require( 'nock' ) ;
3
4
import { Test } from 'nodeunit' ;
@@ -227,4 +228,38 @@ export = {
227
228
228
229
test . done ( ) ;
229
230
} ,
231
+
232
+ async 'does not fail when operations on provider log group fail' ( test : Test ) {
233
+ const createLogGroupFake = ( params : AWSSDK . CloudWatchLogs . CreateLogGroupRequest ) => {
234
+ if ( params . logGroupName === '/aws/lambda/provider' ) {
235
+ return Promise . reject ( new Error ( 'OperationAbortedException' ) ) ;
236
+ }
237
+ return Promise . resolve ( { } ) ;
238
+ } ;
239
+
240
+ const putRetentionPolicyFake = sinon . fake . resolves ( { } ) ;
241
+ const deleteRetentionPolicyFake = sinon . fake . resolves ( { } ) ;
242
+
243
+ AWS . mock ( 'CloudWatchLogs' , 'createLogGroup' , createLogGroupFake ) ;
244
+ AWS . mock ( 'CloudWatchLogs' , 'putRetentionPolicy' , putRetentionPolicyFake ) ;
245
+ AWS . mock ( 'CloudWatchLogs' , 'deleteRetentionPolicy' , deleteRetentionPolicyFake ) ;
246
+
247
+ const event = {
248
+ ...eventCommon ,
249
+ RequestType : 'Create' ,
250
+ ResourceProperties : {
251
+ ServiceToken : 'token' ,
252
+ RetentionInDays : '30' ,
253
+ LogGroupName : 'group'
254
+ }
255
+ } ;
256
+
257
+ const request = createRequest ( 'SUCCESS' ) ;
258
+
259
+ await provider . handler ( event as AWSLambda . CloudFormationCustomResourceCreateEvent , context ) ;
260
+
261
+ test . equal ( request . isDone ( ) , true ) ;
262
+
263
+ test . done ( ) ;
264
+ }
230
265
} ;
You can’t perform that action at this time.
0 commit comments