Skip to content

Commit

Permalink
feat: add logRetention option for mergeSchema lambdas with default to…
Browse files Browse the repository at this point in the history
… NEVER
  • Loading branch information
arnaud-deprez authored and Arnaud Deprez committed Jul 11, 2024
1 parent cc3f26a commit 1b9e8fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 21 additions & 5 deletions src/source-api-association-merge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { randomUUID } from 'crypto';
import * as path from 'path';
import { CfnResource, CustomResource, Duration, Stack } from 'aws-cdk-lib';
import { CfnResource, CustomResource, Duration, Stack, RemovalPolicy } from 'aws-cdk-lib';
import { ISourceApiAssociation } from 'aws-cdk-lib/aws-appsync';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Code, Runtime, SingletonFunction } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { Construct, IConstruct } from 'constructs';

Expand Down Expand Up @@ -77,30 +77,46 @@ export class SourceApiAssociationMergeOperationProvider extends Construct implem
constructor(scope: Construct, id: string, props: SourceApiAssociationMergeOperationProviderProps) {
super(scope, id);

const schemaMergeLambdaName = `${id}-SchemaMergeOperation`;

const schemaMergeLambdaLogGroup = new LogGroup(scope, 'MergeSourceApiSchemaLambdaLogGroup', {
logGroupName: `/aws/lambda/${schemaMergeLambdaName}`,
retention: props.logRetention,
removalPolicy: RemovalPolicy.DESTROY,
});

this.schemaMergeLambda = new SingletonFunction(this, 'MergeSourceApiSchemaLambda', {
runtime: Runtime.NODEJS_20_X,
functionName: schemaMergeLambdaName,
code: Code.fromAsset(path.join(__dirname, 'mergeSourceApiSchemaHandler')),
handler: 'index.onEvent',
timeout: Duration.minutes(2),
uuid: '6148f39b-95bb-47e7-8a35-40adb8b93a7b',
logRetention: props.logRetention,
logGroup: schemaMergeLambdaLogGroup,
});

const sourceApiStablizationLambdaName = `${id}-ApiStabilization`;

const sourceApiStablizationLambdaLogGroup = new LogGroup(scope, 'PollSourceApiMergeLambdaLogGroup', {
logGroupName: `/aws/lambda/${sourceApiStablizationLambdaName}`,
retention: props.logRetention,
removalPolicy: RemovalPolicy.DESTROY,
});
this.sourceApiStablizationLambda = new SingletonFunction(this, 'PollSourceApiMergeLambda', {
runtime: Runtime.NODEJS_20_X,
functionName: sourceApiStablizationLambdaName,
code: Code.fromAsset(path.join(__dirname, 'mergeSourceApiSchemaHandler')),
handler: 'index.isComplete',
timeout: Duration.minutes(2),
uuid: '163e01ec-6f29-4bf4-b3b1-11245b00a6bc',
logRetention: props.logRetention,
logGroup: sourceApiStablizationLambdaLogGroup,
});

const provider = new Provider(this, 'SchemaMergeOperationProvider', {
onEventHandler: this.schemaMergeLambda,
isCompleteHandler: this.sourceApiStablizationLambda,
queryInterval: props.pollingInterval ?? Duration.seconds(5),
totalTimeout: props.totalTimeout ?? Duration.minutes(15),
logRetention: props.logRetention,
});

this.serviceToken = provider.serviceToken;
Expand Down
5 changes: 3 additions & 2 deletions test/source-api-association-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ test('source api association merge operation with logRetention and pollingInterv
},
});

Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 6); // +1 for log retention cleanup
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 5);
Template.fromStack(stack).resourceCountIs('Custom::AppSyncSourceApiMergeOperation', 1);
Template.fromStack(stack).resourceCountIs('Custom::LogRetention', 2);
// no custom log retention resource
Template.fromStack(stack).resourceCountIs('Custom::LogRetention', 0);
});

test('source api association merge operations with version identifier', () => {
Expand Down

0 comments on commit 1b9e8fc

Please sign in to comment.