Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logRetention option for mergeSchema lambdas with default to… #495

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/source-api-association-merge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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 { Provider } from 'aws-cdk-lib/custom-resources';
import { Construct, IConstruct } from 'constructs';
Expand Down Expand Up @@ -43,6 +44,13 @@ export interface SourceApiAssociationMergeOperationProviderProps {
* @default Duration.minutes(15)
*/
readonly totalTimeout?: Duration;

/**
* The number of days log events are kept in CloudWatch Logs for the schemaMergeLambda and sourceApiStablizationLambda.
*
* @default RetentionDays.INFINITE
*/
readonly logRetention?: RetentionDays;
}

/**
Expand All @@ -69,20 +77,39 @@ 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',
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',
logGroup: sourceApiStablizationLambdaLogGroup,
});

const provider = new Provider(this, 'SchemaMergeOperationProvider', {
Expand Down Expand Up @@ -127,6 +154,13 @@ export interface SourceApiAssociationMergeOperationProps {
*/
readonly mergeOperationProvider?: ISourceApiAssociationMergeOperationProvider;

/**
* The default merge operation provider props to use when it is charge of creating a new merge operation provider.
*
* @default { pollingInterval: Duration.seconds(30) }
*/
readonly defaultMergeOperationProviderProps?: SourceApiAssociationMergeOperationProviderProps;

/**
* The version identifier for the schema merge operation. Any change to the version identifier will trigger a merge on the next
* update. Use the version identifier property to control when the source API metadata is merged.
Expand Down Expand Up @@ -192,7 +226,7 @@ export class SourceApiAssociationMergeOperation extends Construct {

var mergeOperationProvider = props.mergeOperationProvider;
if (!mergeOperationProvider) {
mergeOperationProvider = this.getOrCreateMergeOperationProvider();
mergeOperationProvider = this.getOrCreateMergeOperationProvider(props.defaultMergeOperationProviderProps ?? {});
}

mergeOperationProvider.associateSourceApiAssociation(props.sourceApiAssociation);
Expand Down Expand Up @@ -241,13 +275,14 @@ export class SourceApiAssociationMergeOperation extends Construct {
* Get an existing merge operation provider from the current stack or create a new stack scoped merge operation provider.
* @returns SourceApiAssociationMergeOperationProvider
*/
private getOrCreateMergeOperationProvider(): SourceApiAssociationMergeOperationProvider {
private getOrCreateMergeOperationProvider(props: SourceApiAssociationMergeOperationProviderProps): SourceApiAssociationMergeOperationProvider {
const constructName = 'SchemaMergeOperationProvider';
const stack = Stack.of(this);
const existing = stack.node.tryFindChild(constructName);
if (!existing) {
return new SourceApiAssociationMergeOperationProvider(stack, 'SchemaMergeOperationProvider', {
pollingInterval: Duration.seconds(30),
...props,
});
} else {
return existing as SourceApiAssociationMergeOperationProvider;
Expand Down
18 changes: 18 additions & 0 deletions test/source-api-association-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import { Template } from 'aws-cdk-lib/assertions';
import * as appsync from 'aws-cdk-lib/aws-appsync';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as cdk from 'aws-cdk-lib/core';
import { SourceApiAssociationMergeOperation } from '../src';

Expand Down Expand Up @@ -112,6 +113,23 @@ test('source api association merge operation with always update', () => {

Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 5);
Template.fromStack(stack).resourceCountIs('Custom::AppSyncSourceApiMergeOperation', 1);
Template.fromStack(stack).resourceCountIs('Custom::LogRetention', 0);
});

test('source api association merge operation with logRetention and pollingInterval set', () => {
new SourceApiAssociationMergeOperation(stack, 'SourceApi1Merge', {
sourceApiAssociation: sourceApiAssociation1,
alwaysMergeOnStackUpdate: true,
defaultMergeOperationProviderProps: {
logRetention: logs.RetentionDays.ONE_DAY,
pollingInterval: cdk.Duration.minutes(1),
},
});

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

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