-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(platforms): unable to perform awscdk context lookups (#6286)
In order to fix #6279, we need synthesis to be triggered by the CDK CLI instead of directly from the Wing CLI. This is actually really easy to do. All you need is to create a `cdk.json` file (or use one from `cdk init`) and modify the `app` and the `output` options like so: ```json { "app": "CDK_STACK_NAME=MyStack wing compile --platform @winglang/platform-awscdk main.w", "output": "target/main.awscdk" } ``` Then, you can simply use `cdk deploy`, `cdk diff`, `cdk synth` as if it was a normal CDK app. No need to explicitly interact with the Wing CLI in this case. To allow context lookups, we need to support specifying the stack's AWS environment (account/region), so two new environment variables have been added: `CDK_AWS_ACCOUNT` and `CDK_AWS_REGION`. These can be set together with `CDK_STACK_NAME` in the `cdk.json` file mentioned above. ## 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) - [x] Docs updated (only required for features) - [x] 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
4 changed files
with
207 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { test, expect } from "vitest"; | ||
import { Platform } from "../src/platform" | ||
import { mkdtemp } from "@winglang/sdk/test/util"; | ||
import { readdirSync } from "fs"; | ||
import { Bucket } from "../src"; | ||
import { Stack } from "aws-cdk-lib"; | ||
|
||
test("wing platform", async () => { | ||
const workdir = mkdtemp(); | ||
const platform = new Platform(); | ||
process.env.CDK_STACK_NAME = "MyStack"; | ||
const app = platform.newApp?.({ entrypointDir: workdir, outdir: workdir }); | ||
|
||
new Bucket(app, "bucket"); | ||
|
||
const out = app.synth(); | ||
|
||
expect(JSON.parse(out).Resources.bucket43879C71).toStrictEqual({ | ||
DeletionPolicy: "Delete", | ||
Properties: { | ||
BucketEncryption: { | ||
ServerSideEncryptionConfiguration: [ | ||
{ | ||
ServerSideEncryptionByDefault: { | ||
SSEAlgorithm: "AES256", | ||
}, | ||
}, | ||
], | ||
}, | ||
PublicAccessBlockConfiguration: { | ||
BlockPublicAcls: true, | ||
BlockPublicPolicy: true, | ||
IgnorePublicAcls: true, | ||
RestrictPublicBuckets: true, | ||
}, | ||
}, | ||
Type: "AWS::S3::Bucket", | ||
UpdateReplacePolicy: "Delete", | ||
}); | ||
|
||
// output directory only contains wing artifacts. cdk artifacts will be in the cdk.out directory | ||
// when the CDK CLI is used | ||
expect(readdirSync(workdir)).toStrictEqual([ | ||
"MyStack.assets.json", | ||
"MyStack.template.json", | ||
"cdk.out", | ||
"connections.json", | ||
"manifest.json", | ||
"tree.json", | ||
]); | ||
}); | ||
|
||
test("CDK_STACK_NAME, CDK_AWS_ACCOUNT, CDK_AWS_REGION", async () => { | ||
const workdir = mkdtemp(); | ||
const platform = new Platform(); | ||
process.env.CDK_STACK_NAME = "YourStack"; | ||
process.env.CDK_AWS_ACCOUNT = "123"; | ||
process.env.CDK_AWS_REGION = "us-west-2"; | ||
|
||
const app = platform.newApp?.({ entrypointDir: workdir, outdir: workdir }); | ||
const stack = Stack.of(app); | ||
|
||
expect(stack.resolve(stack.region)).toStrictEqual("us-west-2"); | ||
expect(stack.resolve(stack.stackName)).toStrictEqual("YourStack"); | ||
expect(stack.resolve(stack.account)).toStrictEqual("123"); | ||
}); |