Skip to content

Commit be7acfd

Browse files
authored
feat(apigateway): Add stage ARN attribute (#18170)
This adds an attribute to retrieve the resource ARN of a stage (not the execute-api ARN). This is useful when integrating with services such as WAF or when writing IAM policies for managing the API. ARNs for v1 REST APIs are at https://docs.aws.amazon.com/apigateway/latest/developerguide/arn-format-reference.html#apigateway-v1-arns ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a08b804 commit be7acfd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Duration, IResource, Resource, Stack, Token } from '@aws-cdk/core';
1+
import { ArnFormat, Duration, IResource, Resource, Stack, Token } from '@aws-cdk/core';
22
import { Construct } from 'constructs';
33
import { AccessLogFormat, IAccessLogDestination } from './access-log';
44
import { CfnStage } from './apigateway.generated';
@@ -273,6 +273,26 @@ export class Stage extends Resource implements IStage {
273273
return `https://${this.restApi.restApiId}.execute-api.${Stack.of(this).region}.${Stack.of(this).urlSuffix}/${this.stageName}${path}`;
274274
}
275275

276+
/**
277+
* Returns the resource ARN for this stage:
278+
*
279+
* arn:aws:apigateway:{region}::/restapis/{restApiId}/stages/{stageName}
280+
*
281+
* Note that this is separate from the execute-api ARN for methods and resources
282+
* within this stage.
283+
*
284+
* @attribute
285+
*/
286+
public get stageArn() {
287+
return Stack.of(this).formatArn({
288+
arnFormat: ArnFormat.SLASH_RESOURCE_SLASH_RESOURCE_NAME,
289+
service: 'apigateway',
290+
account: '',
291+
resource: 'restapis',
292+
resourceName: `${this.restApi.restApiId}/stages/${this.stageName}`,
293+
});
294+
}
295+
276296
private renderMethodSettings(props: StageProps): CfnStage.MethodSettingProperty[] | undefined {
277297
const settings = new Array<CfnStage.MethodSettingProperty>();
278298
const self = this;

packages/@aws-cdk/aws-apigateway/test/stage.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@ describe('stage', () => {
113113
});
114114
});
115115

116+
test('"stageResourceArn" returns the ARN for the stage', () => {
117+
// GIVEN
118+
const stack = new cdk.Stack();
119+
const api = new apigateway.RestApi(stack, 'test-api');
120+
const deployment = new apigateway.Deployment(stack, 'test-deploymnet', {
121+
api,
122+
});
123+
api.root.addMethod('GET');
124+
125+
// WHEN
126+
const stage = new apigateway.Stage(stack, 'test-stage', {
127+
deployment,
128+
});
129+
130+
// THEN
131+
expect(stack.resolve(stage.stageArn)).toEqual({
132+
'Fn::Join': [
133+
'',
134+
[
135+
'arn:',
136+
{ Ref: 'AWS::Partition' },
137+
':apigateway:',
138+
{ Ref: 'AWS::Region' },
139+
'::/restapis/',
140+
{ Ref: 'testapiD6451F70' },
141+
'/stages/',
142+
{ Ref: 'teststage8788861E' },
143+
],
144+
],
145+
});
146+
});
147+
116148
test('custom method settings can be set by their path', () => {
117149
// GIVEN
118150
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)