Skip to content

Commit

Permalink
Throw error if API has no deployment stage
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Jul 10, 2024
1 parent 36d5508 commit a253bc6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-pipes-targets-alpha/lib/api-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export class ApiGatewayTarget implements ITarget {
this.restApi = restApi;
this.restApiParameters = parameters;

if (this.restApiParameters?.stage === undefined && this.restApi.deploymentStage === undefined) {
throw Error('The REST API must have a deployed stage.');
}

this.restApiArn = this.restApi.arnForExecuteApi(
this.restApiParameters?.method,
this.restApiParameters?.path || '/',
Expand Down
28 changes: 20 additions & 8 deletions packages/@aws-cdk/aws-pipes-targets-alpha/test/api-gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describe('API Gateway REST API', () => {
':',
{ Ref: 'AWS::AccountId' },
':',
{ Ref: 'MyLambdaRestApiECB8AFAF' },
{ Ref: 'MyLambdaRestApiDeployTrueD6F0338A' },
'/',
{ Ref: 'MyLambdaRestApiDeploymentStageprodA127C527' },
{ Ref: 'MyLambdaRestApiDeployTrueDeploymentStageprodAA1DAA0D' },
'/*/',
],
],
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('API Gateway REST API', () => {
':',
{ Ref: 'AWS::AccountId' },
':',
{ Ref: 'MyLambdaRestApiECB8AFAF' },
{ Ref: 'MyLambdaRestApiDeployTrueD6F0338A' },
'/prod/GET/books',
],
],
Expand Down Expand Up @@ -182,9 +182,9 @@ describe('API Gateway REST API', () => {
':',
{ Ref: 'AWS::AccountId' },
':',
{ Ref: 'MyLambdaRestApiECB8AFAF' },
{ Ref: 'MyLambdaRestApiDeployTrueD6F0338A' },
'/',
{ Ref: 'MyLambdaRestApiDeploymentStageprodA127C527' },
{ Ref: 'MyLambdaRestApiDeployTrueDeploymentStageprodAA1DAA0D' },
'/*/',
],
],
Expand All @@ -194,16 +194,28 @@ describe('API Gateway REST API', () => {
},
});
});

it('should throw error when REST API does not have a deployed stage', () => {
// ARRANGE
const restApiNoDeploy = newTestRestApi(stack, false);

// ACT & ASSERT
expect(() => {
new ApiGatewayTarget(restApiNoDeploy);
}).toThrow('The REST API must have a deployed stage.');
});
});

function newTestRestApi(scope: Construct) {
const lambdaFunction = new lambda.Function(scope, 'MyLambda', {
function newTestRestApi(scope: Construct, deploy: boolean = true) {
const deployStr = String(deploy)[0].toUpperCase() + String(deploy).slice(1);
const lambdaFunction = new lambda.Function(scope, `MyLambdaDeploy${deployStr}`, {
code: new lambda.InlineCode('foo'),
handler: 'bar',
runtime: lambda.Runtime.NODEJS_LATEST,
});

return new LambdaRestApi(scope, 'MyLambdaRestApi', {
return new LambdaRestApi(scope, `MyLambdaRestApiDeploy${deployStr}`, {
handler: lambdaFunction,
deploy,
});
}

0 comments on commit a253bc6

Please sign in to comment.