Skip to content

Commit

Permalink
feat(appsync): allow user to configure log retention time
Browse files Browse the repository at this point in the history
  • Loading branch information
nikovirtala committed Jun 8, 2022
1 parent 5696c5d commit 8af6c70
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager';
import { IUserPool } from '@aws-cdk/aws-cognito';
import { ManagedPolicy, Role, IRole, ServicePrincipal, Grant, IGrantable } from '@aws-cdk/aws-iam';
import { IFunction } from '@aws-cdk/aws-lambda';
import { LogRetention, RetentionDays } from '@aws-cdk/aws-logs';
import { ArnFormat, CfnResource, Duration, Expiration, IResolvable, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnApiKey, CfnGraphQLApi, CfnGraphQLSchema, CfnDomainName, CfnDomainNameApiAssociation } from './appsync.generated';
Expand Down Expand Up @@ -253,6 +254,13 @@ export interface LogConfig {
* @default - None
*/
readonly role?: IRole;

/**
* log retention period
*
* @default - Use AppSync default
*/
readonly retention?: RetentionDays
}

/**
Expand Down Expand Up @@ -519,6 +527,13 @@ export class GraphqlApi extends GraphqlApiBase {
this.apiKeyResource.addDependsOn(this.schemaResource);
this.apiKey = this.apiKeyResource.attrApiKey;
}

if (props.logConfig?.retention) {
new LogRetention(this, 'ApiLogsRetention', {
logGroupName: `/aws/appsync/apis/${this.apiId}`,
retention: props.logConfig.retention,
});
};
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-appsync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@aws-cdk/aws-elasticsearch": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-opensearchservice": "0.0.0",
"@aws-cdk/aws-rds": "0.0.0",
"@aws-cdk/aws-s3-assets": "0.0.0",
Expand All @@ -113,6 +114,7 @@
"@aws-cdk/aws-elasticsearch": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-lambda": "0.0.0",
"@aws-cdk/aws-logs": "0.0.0",
"@aws-cdk/aws-opensearchservice": "0.0.0",
"@aws-cdk/aws-rds": "0.0.0",
"@aws-cdk/aws-s3-assets": "0.0.0",
Expand Down
35 changes: 35 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync.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/assertions';
import { Certificate } from '@aws-cdk/aws-certificatemanager';
import * as iam from '@aws-cdk/aws-iam';
import * as logs from '@aws-cdk/aws-logs';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';

Expand Down Expand Up @@ -191,3 +192,37 @@ test('appsync GraphqlApi should be configured with custom domain when specified'
DomainName: domainName,
});
});

test('log retention should be configured with given retention time when specified', () => {
// GIVEN
const retentionTime = logs.RetentionDays.ONE_WEEK;

// WHEN
new appsync.GraphqlApi(stack, 'log-retention', {
authorizationConfig: {},
name: 'log-retention',
schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.test.graphql')),
logConfig: {
retention: retentionTime,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
LogGroupName: {
'Fn::Join': [
'',
[
'/aws/appsync/apis/',
{
'Fn::GetAtt': [
'logretentionB69DFB48',
'ApiId',
],
},
],
],
},
RetentionInDays: 7,
});
});

0 comments on commit 8af6c70

Please sign in to comment.