-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-ec2): Vpc.from_lookup does not work out of the box on init templates #12321
Comments
I am experiencing the same issue as @hornetmadness on a typescript CDK project CDK: 1.83.0 Determining if we're on an EC2 instance.
Does not look like an EC2 instance.
Toolkit stack: CDKToolkit
Setting "CDK_DEFAULT_REGION" environment variable to us-east-1
Resolving default credentials
Retrieved account ID REDACTED from disk cache
Setting "CDK_DEFAULT_ACCOUNT" environment variable to REDACTED
context: {
'@aws-cdk/core:enableStackNameDuplicates': 'true',
'aws-cdk:enableDiffNoFail': 'true',
'@aws-cdk/core:stackRelativeExports': 'true',
'@aws-cdk/aws-ecr-assets:dockerIgnoreSupport': true,
'@aws-cdk/aws-secretsmanager:parseOwnedSecretName': true,
'@aws-cdk/aws-kms:defaultKeyPolicies': true,
'aws:cdk:enable-path-metadata': true,
'aws:cdk:enable-asset-metadata': true,
'aws:cdk:version-reporting': true,
'aws:cdk:bundling-stacks': []
}
outdir: cdk.out
env: {
CDK_DEFAULT_REGION: 'us-east-1',
CDK_DEFAULT_ACCOUNT: 'REDACTED',
CDK_CONTEXT_JSON: '{"@aws-cdk/core:enableStackNameDuplicates":"true","aws-cdk:enableDiffNoFail":"true","@aws-cdk/core:stackRelativeExports":"true","@aws-cdk/aws-ecr-assets:dockerIgnoreSupport":true,"@aws-cdk/aws-secretsmanager:parseOwnedSecretName":true,"@aws-cdk/aws-kms:defaultKeyPolicies":true,"aws:cdk:enable-path-metadata":true,"aws:cdk:enable-asset-metadata":true,"aws:cdk:version-reporting":true,"aws:cdk:bundling-stacks":[]}',
CDK_OUTDIR: 'cdk.out',
CDK_CLI_ASM_VERSION: '7.0.0',
CDK_CLI_VERSION: '1.83.0'
}
Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)
Subprocess exited with error 1 According to the Environment Guide
It appears that we advise users to explicitly import environment variables inside of the Stack invocation, and this works. new MyDevStack(app, 'dev', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION
}}); Output of cdk list -v: env: {
CDK_DEFAULT_REGION: 'us-east-1',
CDK_DEFAULT_ACCOUNT: 'REDACTED',
CDK_CONTEXT_JSON: '{"vpc-provider:account=REDACTED:filter.isDefault=true:region=us-east-1:returnAsymmetricSubnets=true":{"vpcId":"REDACTED","vpcCidrBlock":"REDACTED","availabilityZones":
...
CDK_OUTDIR: 'cdk.out',
CDK_CLI_ASM_VERSION: '7.0.0',
CDK_CLI_VERSION: '1.83.0'
} To me, there is a subtle, possibly opaque issues here: What is the difference between the CDK using an inherited CLI value (As per the docs), and the consumer/user setting these environment variables for CDK? If CDK "inherits" values from the CLI only when the user retrieves them inside the code, that doesn't feel like inheritance to me. That feels like a property being set by the user, from a retrieved value. It seems we've had this discussion before and updated our guidance, from this feedback, but I don't think we've resolved the issue with clarity here. I see a couple of resolution paths here:
|
@hornetmadness I think you are not transferring the env value to the super constructor call in your VPCImport2 class. This needs to be sent upstream to the stack. @webdog I advise against using the CDK_DEFAULT_XXX as a fallback when no env is set. When doing lookups I think we want to make sure that users are specifying the target environment exactly and not using whatever credentials they have active at the moment of synth. |
@webdog your main issue seems to be with the word "inherit", is that right? It's unlikely we'll change the behavior to do case analysis. The current behaviors are:
Muddying the waters by having "nothing specified" mean EITHER "environment agnostic" or "use current env" depending on the phase of the moon (not really, but the decision would be based on state that is not super obvious for users to observe, so that's a potential footgun right there -- the behavior will be hard to explain/predict) does not seem appealing. Two things we could address:
As for the first, would:
Help? As for the second, I guess we can make the error message EVEN LONGER to point them to the stack invocations, or have the default generated Stack throw an error until you pass an We could potentially generate commented-out code that shows the 3 alternatives side-by-side and encourages users to uncomment one of them. |
Adding @jerry-aws to see if he has some ideas on how to phrase this even more clearly. |
👋 More or less, yeah (Can't speak for the original author of this issue). I started diving into the codebase last week, but I couldn't find a definitive spot where we could inject logic to except another error message when a lookup is present, so I empathize with the challenge, especially Point 1 in your comment about API clarity.
It would, but I don't know if there's a need to be that explicit, and IIRC, the current
👎 On longer error messages, it might muddy the clarity as you've said. If I'm in the situation where I'm using a lookup, just let me know that when I'm using a lookup, I have to explicitly define the account and region, even when I've set defaults, or support the defaults in a non-production setup (Maybe this is something as simple as |
We can always improve documentation and error messages, but that might just be a bandage on the problem. Taking a step back, maybe there's some way to make this choice clearer to customers. To me, a significant difference in behavior like we have in the You can then deprecate |
It is actually; it's not atypical to deploy the same stack to a fixed prod account, as well as a "floating" dev account. Which is why inheritance is often not the answer, because it limits choices. |
I see. I still think overloading |
How the `env` parameter is supposed to be used is still confusing to a lot of users. Add uncommentable lines to the init templates showing and describing the 3 alternatives. Fixes #12321.
…#13696) How the `env` parameter is supposed to be used is still confusing to a lot of users. Add uncommentable lines to the init templates showing and describing the 3 alternatives. Fixes #12321. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
…#13696) How the `env` parameter is supposed to be used is still confusing to a lot of users. Add uncommentable lines to the init templates showing and describing the 3 alternatives. Fixes #12321. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
…aws#13696) How the `env` parameter is supposed to be used is still confusing to a lot of users. Add uncommentable lines to the init templates showing and describing the 3 alternatives. Fixes aws#12321. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This sample code that shows the env settings seem to be ignored when the VPCImport2 class is ran. If I comment out the import_vpc_stack line, it runs fine and creates the VPC. But when using the vpc.from_lookup function is always fails. This code is just a representation of trying to use vpc.from_import and showing that the env actually works.
Reproduction Steps
What did you expect to happen?
The VPC that was just created would get imported,
What actually happened?
Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)
Environment
Other
I feel like the error that is being thrown is not the real error but is some other underlyaing problem as I cant believe that this would stop working just for one certain function.
This is 🐛 Bug Report
The text was updated successfully, but these errors were encountered: