Skip to content

Commit 8af6c70

Browse files
committed
feat(appsync): allow user to configure log retention time
1 parent 5696c5d commit 8af6c70

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Diff for: packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager';
22
import { IUserPool } from '@aws-cdk/aws-cognito';
33
import { ManagedPolicy, Role, IRole, ServicePrincipal, Grant, IGrantable } from '@aws-cdk/aws-iam';
44
import { IFunction } from '@aws-cdk/aws-lambda';
5+
import { LogRetention, RetentionDays } from '@aws-cdk/aws-logs';
56
import { ArnFormat, CfnResource, Duration, Expiration, IResolvable, Stack } from '@aws-cdk/core';
67
import { Construct } from 'constructs';
78
import { CfnApiKey, CfnGraphQLApi, CfnGraphQLSchema, CfnDomainName, CfnDomainNameApiAssociation } from './appsync.generated';
@@ -253,6 +254,13 @@ export interface LogConfig {
253254
* @default - None
254255
*/
255256
readonly role?: IRole;
257+
258+
/**
259+
* log retention period
260+
*
261+
* @default - Use AppSync default
262+
*/
263+
readonly retention?: RetentionDays
256264
}
257265

258266
/**
@@ -519,6 +527,13 @@ export class GraphqlApi extends GraphqlApiBase {
519527
this.apiKeyResource.addDependsOn(this.schemaResource);
520528
this.apiKey = this.apiKeyResource.attrApiKey;
521529
}
530+
531+
if (props.logConfig?.retention) {
532+
new LogRetention(this, 'ApiLogsRetention', {
533+
logGroupName: `/aws/appsync/apis/${this.apiId}`,
534+
retention: props.logConfig.retention,
535+
});
536+
};
522537
}
523538

524539
/**

Diff for: packages/@aws-cdk/aws-appsync/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"@aws-cdk/aws-elasticsearch": "0.0.0",
9898
"@aws-cdk/aws-iam": "0.0.0",
9999
"@aws-cdk/aws-lambda": "0.0.0",
100+
"@aws-cdk/aws-logs": "0.0.0",
100101
"@aws-cdk/aws-opensearchservice": "0.0.0",
101102
"@aws-cdk/aws-rds": "0.0.0",
102103
"@aws-cdk/aws-s3-assets": "0.0.0",
@@ -113,6 +114,7 @@
113114
"@aws-cdk/aws-elasticsearch": "0.0.0",
114115
"@aws-cdk/aws-iam": "0.0.0",
115116
"@aws-cdk/aws-lambda": "0.0.0",
117+
"@aws-cdk/aws-logs": "0.0.0",
116118
"@aws-cdk/aws-opensearchservice": "0.0.0",
117119
"@aws-cdk/aws-rds": "0.0.0",
118120
"@aws-cdk/aws-s3-assets": "0.0.0",

Diff for: packages/@aws-cdk/aws-appsync/test/appsync.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from 'path';
22
import { Template } from '@aws-cdk/assertions';
33
import { Certificate } from '@aws-cdk/aws-certificatemanager';
44
import * as iam from '@aws-cdk/aws-iam';
5+
import * as logs from '@aws-cdk/aws-logs';
56
import * as cdk from '@aws-cdk/core';
67
import * as appsync from '../lib';
78

@@ -191,3 +192,37 @@ test('appsync GraphqlApi should be configured with custom domain when specified'
191192
DomainName: domainName,
192193
});
193194
});
195+
196+
test('log retention should be configured with given retention time when specified', () => {
197+
// GIVEN
198+
const retentionTime = logs.RetentionDays.ONE_WEEK;
199+
200+
// WHEN
201+
new appsync.GraphqlApi(stack, 'log-retention', {
202+
authorizationConfig: {},
203+
name: 'log-retention',
204+
schema: appsync.Schema.fromAsset(path.join(__dirname, 'appsync.test.graphql')),
205+
logConfig: {
206+
retention: retentionTime,
207+
},
208+
});
209+
210+
// THEN
211+
Template.fromStack(stack).hasResourceProperties('Custom::LogRetention', {
212+
LogGroupName: {
213+
'Fn::Join': [
214+
'',
215+
[
216+
'/aws/appsync/apis/',
217+
{
218+
'Fn::GetAtt': [
219+
'logretentionB69DFB48',
220+
'ApiId',
221+
],
222+
},
223+
],
224+
],
225+
},
226+
RetentionInDays: 7,
227+
});
228+
});

0 commit comments

Comments
 (0)