@@ -2,6 +2,7 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager';
22import { IUserPool } from '@aws-cdk/aws-cognito' ;
33import { ManagedPolicy , Role , IRole , ServicePrincipal , Grant , IGrantable } from '@aws-cdk/aws-iam' ;
44import { IFunction } from '@aws-cdk/aws-lambda' ;
5+ import { ILogGroup , LogGroup , LogRetention , RetentionDays } from '@aws-cdk/aws-logs' ;
56import { ArnFormat , CfnResource , Duration , Expiration , IResolvable , Stack } from '@aws-cdk/core' ;
67import { Construct } from 'constructs' ;
78import { CfnApiKey , CfnGraphQLApi , CfnGraphQLSchema , CfnDomainName , CfnDomainNameApiAssociation } from './appsync.generated' ;
@@ -248,6 +249,16 @@ export interface LogConfig {
248249 * @default - None
249250 */
250251 readonly role ?: IRole ;
252+
253+ /**
254+ * The number of days log events are kept in CloudWatch Logs.
255+ * By default AppSync keeps the logs infinitely. When updating this property,
256+ * unsetting it doesn't remove the log retention policy.
257+ * To remove the retention policy, set the value to `INFINITE`
258+ *
259+ * @default RetentionDays.INFINITE
260+ */
261+ readonly retention ?: RetentionDays
251262}
252263
253264/**
@@ -459,6 +470,11 @@ export class GraphqlApi extends GraphqlApiBase {
459470 */
460471 public readonly apiKey ?: string ;
461472
473+ /**
474+ * the CloudWatch Log Group for this API
475+ */
476+ public readonly logGroup : ILogGroup ;
477+
462478 private schemaResource : CfnGraphQLSchema ;
463479 private api : CfnGraphQLApi ;
464480 private apiKeyResource ?: CfnApiKey ;
@@ -527,6 +543,16 @@ export class GraphqlApi extends GraphqlApiBase {
527543 } ) ;
528544 }
529545
546+ const logGroupName = `/aws/appsync/apis/${ this . apiId } ` ;
547+
548+ this . logGroup = LogGroup . fromLogGroupName ( this , 'LogGroup' , logGroupName ) ;
549+
550+ if ( props . logConfig ?. retention ) {
551+ new LogRetention ( this , 'LogRetention' , {
552+ logGroupName : this . logGroup . logGroupName ,
553+ retention : props . logConfig . retention ,
554+ } ) ;
555+ } ;
530556 }
531557
532558 /**
@@ -620,10 +646,11 @@ export class GraphqlApi extends GraphqlApiBase {
620646 ManagedPolicy . fromAwsManagedPolicyName ( 'service-role/AWSAppSyncPushToCloudWatchLogs' ) ,
621647 ] ,
622648 } ) . roleArn ;
649+ const fieldLogLevel : FieldLogLevel = config . fieldLogLevel ?? FieldLogLevel . NONE ;
623650 return {
624651 cloudWatchLogsRoleArn : logsRoleArn ,
625652 excludeVerboseContent : config . excludeVerboseContent ,
626- fieldLogLevel : config . fieldLogLevel ,
653+ fieldLogLevel : fieldLogLevel ,
627654 } ;
628655 }
629656
0 commit comments