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
4949 // group for this function should already exist at this stage because we
5050 // already logged the event but due to the async nature of Lambda logging
5151 // 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+ }
5562 }
5663 }
5764
Original file line number Diff line number Diff line change 1+ import AWSSDK = require( 'aws-sdk' ) ;
12import AWS = require( 'aws-sdk-mock' ) ;
23import nock = require( 'nock' ) ;
34import { Test } from 'nodeunit' ;
@@ -227,4 +228,38 @@ export = {
227228
228229 test . done ( ) ;
229230 } ,
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+ }
230265} ;
You can’t perform that action at this time.
0 commit comments