(@aws-cdk/aws-apigateway): Generation of RestApi.url fails if deploy property is false
What is the problem?
I am trying to print the API gateway url as part of synth/deploy:
new cdk.CfnOutput(this, 'ApiGatewayURL', { value: restApiGw.url, exportName: "ApiGatewayURL"} );
However this fails with:
Cannot determine deployment stage for API from "deploymentStage". Use "deploy" or explicitly set "deploymentStage"
Reproduction Steps
I suspect that this is due to some missing configuration somewhere when using deploy = false and having specified Stage/Deploy manually. Synth and deploy works as expected when line above is commented out.
This is the code to reproduce:
import * as cdk from "@aws-cdk/core";
import * as apiGw from "@aws-cdk/aws-apigateway";
import * as iam from "@aws-cdk/aws-iam";
import * as logs from "@aws-cdk/aws-logs";
import * as elb2 from "@aws-cdk/aws-elasticloadbalancingv2";
export class DocumentConverterApiGWStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const applicationName = 'DocumentConverter';
const boundary = iam.ManagedPolicy.fromManagedPolicyName(this, 'Boundary', 'Core-PermissionBoundaryPolicy');
iam.PermissionsBoundary.of(this).apply(boundary);
const networkLoadBalancer = elb2.NetworkLoadBalancer.fromLookup(this,"NblLookUp",{
loadBalancerArn: "NLB_ARN"
})
const vpcLink = new apiGw.VpcLink(this,"vpcLink", {
description: "VpcLink towards NLB",
targets: [networkLoadBalancer]
});
const policy = new iam.PolicyDocument({
statements: [new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
principals: [new iam.AnyPrincipal()],
actions: ["execute-api:Invoke"],
resources: ["execute-api:/*"]
}),
new iam.PolicyStatement({
effect: iam.Effect.DENY,
principals: [new iam.AnyPrincipal()],
actions: ["execute-api:Invoke"],
resources: ["execute-api:/*"],
conditions: {"NotIpAddress": {"aws:VpcSourceIp":["IP_LIST"] }}
})]
});
const restApiGw = new apiGw.RestApi(this,"RestApiGw",{
description: "Api Gateway for GroupServices",
deploy: false,
cloudWatchRole: true,
endpointConfiguration: {
types: [apiGw.EndpointType.PRIVATE],
},
policy:policy
}
);
const integrationGet = new apiGw.Integration({
type: apiGw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: "GET",
uri: "http://"+networkLoadBalancer.loadBalancerDnsName+"/{proxy}",
options: {
connectionType: apiGw.ConnectionType.VPC_LINK,
vpcLink: vpcLink,
passthroughBehavior: apiGw.PassthroughBehavior.WHEN_NO_MATCH,
requestParameters: {
"integration.request.path.proxy": "method.request.path.proxy"
}
},
});
const integrationPost = new apiGw.Integration({
type: apiGw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: "POST",
uri: "http://"+networkLoadBalancer.loadBalancerDnsName+"/{proxy}",
options: {
connectionType: apiGw.ConnectionType.VPC_LINK,
vpcLink: vpcLink,
passthroughBehavior: apiGw.PassthroughBehavior.WHEN_NO_MATCH,
requestParameters: {
"integration.request.path.proxy": "method.request.path.proxy",
}
},
})
const proxyResource = new apiGw.ProxyResource(this, "ProxyResource", {
parent: restApiGw.root,
});
proxyResource.addMethod("GET",integrationGet,{requestParameters: { "method.request.path.proxy": true }});
proxyResource.addMethod("POST",integrationPost, {requestParameters: { "method.request.path.proxy": true }});
const deployment = new apiGw.Deployment(this,"Deployment",{
api: restApiGw
})
const logGroupApiGw = new logs.LogGroup(this,'ApiGwLogGroup' ,{
logGroupName: "/api-gw/"+applicationName,
retention: 7
});
const stage = new apiGw.Stage(this,"Stage",{
deployment: deployment,
accessLogDestination: new apiGw.LogGroupLogDestination(logGroupApiGw),
loggingLevel: apiGw.MethodLoggingLevel.INFO,
dataTraceEnabled: true,
stageName: "DEV",
});
new cdk.CfnOutput(this, 'ApiGatewayURL', { value: restApiGw.url, exportName: "ApiGatewayURL"} );
}
}
What did you expect to happen?
I expected that the ApiGwUrl was printed as part of synth/deploy
What actually happened?
Error:
Error: Cannot determine deployment stage for API from "deploymentStage". Use "deploy" or explicitly set "deploymentStage"
at RestApi.urlForPath (/Users/ad03490/projects/bitbucket/cdk-document-converter/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:162:13)
at RestApi.get url [as url] (/Users/ad03490/projects/bitbucket/cdk-document-converter/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:495:17)
at new DocumentConverterApiGWStack (/Users/ad03490/projects/bitbucket/cdk-document-converter/lib/DocumentConverterApiGWStack.ts:104:70)
at Object.<anonymous> (/Users/ad03490/projects/bitbucket/cdk-document-converter/bin/cdk-document-converter.ts:18:25)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Module.m._compile (/Users/ad03490/projects/bitbucket/cdk-document-converter/node_modules/ts-node/src/index.ts:1056:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
at Object.require.extensions.<computed> [as .ts] (/Users/ad03490/projects/bitbucket/cdk-document-converter/node_modules/ts-node/src/index.ts:1059:12)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:816:12)
Subprocess exited with error 1
CDK CLI Version
1.127.0 (build 0ea309a)
Framework Version
No response
Node.js Version
v16.4.2
OS
macOS 12.0.1 (21A559)
Language
Typescript
Language Version
Typescript Version 3.9.10
Other information
Remove CfnOutput with ApiGwUrl reference and it works OK. Probably some missing configuration/reference somewhere when deploy = false and custom stage/deploy
Hey @matsaune 👋🏻
This seems related to https://github.com/aws/aws-cdk/issues/11986 and is expected behavior.
The RestApi.url property actually retrieves its value from the RestApi's deploymentStage property.
From the RestApi.deploymentStage docs:
If deploy is disabled, you will need to explicitly assign this value in order to set up integrations.
Solution:
Assigning a stage to restApiGw.deploymentStage before calling restApiGw.url should resolve the error:
restApiGw.deploymentStage = stage;
new cdk.CfnOutput(this, 'ApiGatewayURL', { value: restApiGw.url, exportName: "ApiGatewayURL" });
Or you could access the URL directly from the stage (same result):
new cdk.CfnOutput(this, 'ApiGatewayURL', { value: stage.urlForPath(), exportName: "ApiGatewayURL" });
Feel free to reopen if the guidance above does not resolve this issue.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.