Skip to content

Commit

Permalink
fix(apigateway): access log format does not allow tokens (#9769)
Browse files Browse the repository at this point in the history
fixes #9687


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar committed Aug 17, 2020
1 parent 611c48d commit a7c5c75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/stage.ts
@@ -1,4 +1,4 @@
import { Construct, Duration, IResource, Resource, Stack } from '@aws-cdk/core';
import { Construct, Duration, IResource, Resource, Stack, Token } from '@aws-cdk/core';
import { AccessLogFormat, IAccessLogDestination } from './access-log';
import { CfnStage } from './apigateway.generated';
import { Deployment } from './deployment';
Expand Down Expand Up @@ -210,7 +210,10 @@ export class Stage extends Resource implements IStage {
if (!accessLogDestination && !accessLogFormat) {
accessLogSetting = undefined;
} else {
if (accessLogFormat !== undefined && !/.*\$context.requestId.*/.test(accessLogFormat.toString())) {
if (accessLogFormat !== undefined &&
!Token.isUnresolved(accessLogFormat.toString()) &&
!/.*\$context.requestId.*/.test(accessLogFormat.toString())) {

throw new Error('Access log must include at least `AccessLogFormat.contextRequestId()`');
}
if (accessLogFormat !== undefined && accessLogDestination === undefined) {
Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.stage.ts
Expand Up @@ -333,6 +333,27 @@ export = {
test.done();
},

'does not fail when access log format is a token'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: false });
const deployment = new apigateway.Deployment(stack, 'my-deployment', { api });
api.root.addMethod('GET');

// WHEN
const testLogGroup = new logs.LogGroup(stack, 'LogGroup');
const testFormat = apigateway.AccessLogFormat.custom(cdk.Lazy.stringValue({ produce: () => 'test' }));

// THEN
test.doesNotThrow(() => new apigateway.Stage(stack, 'my-stage', {
deployment,
accessLogDestination: new apigateway.LogGroupLogDestination(testLogGroup),
accessLogFormat: testFormat,
}));

test.done();
},

'fails when access log destination is empty'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit a7c5c75

Please sign in to comment.