Skip to content

Commit e817381

Browse files
authored
fix(apigateway): allow using GENERATE_IF_NEEDED for the physical name in LambdaRestApi (#19638)
Fixes #9374 ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a74d82e commit e817381

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ export abstract class RestApiBase extends Resource implements IRestApi {
322322
protected cloudWatchAccount?: CfnAccount;
323323

324324
constructor(scope: Construct, id: string, props: RestApiBaseProps = { }) {
325-
super(scope, id);
326-
this.restApiName = props.restApiName ?? id;
325+
const restApiName = props.restApiName ?? id;
326+
super(scope, id, {
327+
physicalName: restApiName,
328+
});
329+
this.restApiName = restApiName;
327330

328331
Object.defineProperty(this, RESTAPI_SYMBOL, { value: true });
329332
}
@@ -736,7 +739,7 @@ export class RestApi extends RestApiBase {
736739
super(scope, id, props);
737740

738741
const resource = new CfnRestApi(this, 'Resource', {
739-
name: this.restApiName,
742+
name: this.physicalName,
740743
description: props.description,
741744
policy: props.policy,
742745
failOnWarnings: props.failOnWarnings,

packages/@aws-cdk/aws-apigateway/test/lambda-api.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,24 @@ describe('lambda api', () => {
242242
],
243243
});
244244
});
245+
246+
test('LambdaRestApi allows passing GENERATE_IF_NEEDED as the physical name', () => {
247+
// GIVEN
248+
const stack = new cdk.Stack();
249+
250+
// WHEN
251+
new apigw.LambdaRestApi(stack, 'lambda-rest-api', {
252+
handler: new lambda.Function(stack, 'handler', {
253+
handler: 'index.handler',
254+
code: lambda.Code.fromInline('boom'),
255+
runtime: lambda.Runtime.NODEJS_10_X,
256+
}),
257+
restApiName: cdk.PhysicalName.GENERATE_IF_NEEDED,
258+
});
259+
260+
// THEN
261+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
262+
Name: Match.absent(),
263+
});
264+
});
245265
});

0 commit comments

Comments
 (0)