-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sdk): cannot reference api.url within cloud.Api route on AWS (#5637)
Fixes #2740 In this bug, when the user creates an API route whose inflight code references the API's url... ```js let api = new cloud.Api(); api.get("/my_url", inflight () => { return { status: 200, body: api.url }; }); ``` ... when they deploy to the `tf-aws` platform, it causes resources to be created in a dependency cycle, making it impossible to deploy the app. The reason this happens is that first, the `cloud.Api` is turned into an API Gateway, whose OpenAPI specification (list of all of the API routes) needs to reference the physical name of the `cloud.Function` (usually called the "ARN" in AWS-speak). Second, the `cloud.Function` is turned into an AWS Lambda function, which needs an environment variable to provide the URL of the API Gateway. Both resources want to use a property of the other resource that seems to only be available once it's created. To unravel this dependency cycle, I updated the implementation of `cloud.Api` to take leverage of the fact that the ARNs of functions are well-defined in the CDK model, and can almost entirely[1] be determined at compile time. This allows the API Gateway to be deployed before the Lambda function without any issues. [1] The only exception is that the AWS account name and region is needed, but these values can be obtained freely at deploy time. ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
Showing
32 changed files
with
596 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
bring cloud; | ||
bring http; | ||
bring util; | ||
|
||
// This test checks that an API can have a route whose handler | ||
// references the API's URL. | ||
|
||
if ["sim", "tf-aws", "awscdk"].contains(util.env("WING_TARGET")) { | ||
let api = new cloud.Api(); | ||
|
||
api.get("/my_url", inflight () => { | ||
return { | ||
status: 200, | ||
body: api.url | ||
}; | ||
}); | ||
|
||
test "GET /my_url" { | ||
let resp = http.get("{api.url}/my_url"); | ||
assert(resp.status == 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.