I'm submitting a ...
What is the current behavior?
Upgraded to 0.36.0 (build 6d38487).
Problems with Vpc.from_lookup().. stack class code at the bottom. Snippet here:
VPCID='vpc-0dc5b6f0c0613263c'
vpc = ec2.Vpc.from_lookup(self, 'vpc', vpc_id=VPCID)
Running cdk synth results in error:
(.env) Admin:~/environment/hmh-dms-poc (master) $ cdk synth
jsii.errors.JavaScriptError:
Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level
at Function.getValue (/tmp/jsii-kernel-GmpUrj/node_modules/@aws-cdk/core/lib/context-provider.js:38:19)
at Function.fromLookup (/tmp/jsii-kernel-GmpUrj/node_modules/@aws-cdk/aws-ec2/lib/vpc.js:307:51)
at _wrapSandboxCode (/home/ec2-user/environment/hmh-dms-poc/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6514:51)
at Kernel._wrapSandboxCode (/home/ec2-user/environment/hmh-dms-poc/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7129:19)
at ret._ensureSync (/home/ec2-user/environment/hmh-dms-poc/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6514:25)
at Kernel._ensureSync (/home/ec2-user/environment/hmh-dms-poc/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7097:20)
at Kernel.sinvoke (/home/ec2-user/environment/hmh-dms-poc/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:6513:26)
Expected cdk synth to work with no errors. It worked with earlier cdk releases.
Did 0.36 introduce new dependencies for vpc context provider?
What does the msg mean:
since account/region are not specified at the stack level
Please tell us about your environment:
Stack code:
from aws_cdk import core
class HmhDmsPocStack(core.Stack):
def __init__(self, app: core.App, id: str, **kwargs) -> None:
super().__init__(app, id)
# The code that defines your stack goes here
from aws_cdk import (
aws_ec2 as ec2
)
# Repro error
VPCID='vpc-0dc5b6f0c0613263c'
vpc = ec2.Vpc.from_lookup(self, 'vpc', vpc_id=VPCID)
We should update the error message so that it's clear how to resolve this.
I also got this error for the new route53.HostedZone.fromLookup when trying to update the static-site example for v0.36. I'm specifying the region in the stack env, but it seems I also have to provide a concrete account ID? Are there other ways out of this error?
https://github.com/aws-samples/aws-cdk-examples/blob/1bbbe037513bbb52764e66831a10eb4b1db01b73/typescript/static-site/index.ts#L29
The docs seem to suggest I should be able to make this template "relocatable" and usable by others, but specifying a concrete account ID makes that difficult.
Having the same issue with 0.36 trying to lookup an existing vpc.
Seeing the same error:
Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level
First time I'm trying fromLookup so I'm going to try a simpler "lookup" program and see if I have the same issues. I have quite a bit of code before and after the fromLookup static method call. Using Typescript.
I'm having the same issue. I was able to fix it by having my stacks environment configured with an account and region. I don't know how it knew which account and region to fetch the default VPC from before updating to 0.36.0
@fulghum would be great to see the proper message in this thread as well
Same issue with HostedZone.fromLookup. However, I can see that cdk synth -v takes the required account id and region but still shows the below error.
Error: Cannot retrieve value from context provider hosted-zone since account/region are not specified at the stack level
The verbose of cdk synth is as follows.
$ cdk synth -c environment=dev -c stackPrefix=foo -v
CDK toolkit version: 0.36.0 (build 6d38487)
Command line arguments: { _: [ 'synth' ],
'ignore-errors': false,
ignoreErrors: false,
json: false,
j: false,
verbose: true,
v: true,
ec2creds: undefined,
i: undefined,
'version-reporting': undefined,
versionReporting: undefined,
'path-metadata': true,
pathMetadata: true,
'asset-metadata': true,
assetMetadata: true,
staging: true,
ci: false,
c: [ 'environment=dev', 'stackPrefix=foo' ],
context: [ 'environment=dev', 'stackPrefix=foo' ],
'role-arn': undefined,
r: undefined,
roleArn: undefined,
'$0': '/usr/local/bin/cdk' }
Determining whether we're on an EC2 instance.
CLI argument context: environment=dev
CLI argument context: stackPrefix=foo
Does not look like EC2 instance.
cdk.json: {
"app": "node ./dist/index"
}
merged settings: { versionReporting: true,
pathMetadata: true,
output: 'cdk.out',
app: 'node ./dist/index',
context: { environment: 'dev', stackPrefix: 'foo' },
tags: [],
assetMetadata: true,
staging: true }
Setting "CDK_DEFAULT_REGION" environment variable to us-east-1
Resolving default credentials
Retrieved account ID xxxxxxxxxxxx from disk cache
Setting "CDK_DEFAULT_ACCOUNT" environment variable to xxxxxxxxxxxx
context: { environment: 'dev',
stackPrefix: 'foo',
'aws:cdk:enable-path-metadata': true,
'aws:cdk:enable-asset-metadata': true }
outdir: cdk.out
env: { CDK_DEFAULT_REGION: 'us-east-1',
CDK_DEFAULT_ACCOUNT: 'xxxxxxxxxxxx',
CDK_CONTEXT_JSON:
'{"environment":"dev","stackPrefix":"foo","aws:cdk:enable-path-metadata":true,"aws:cdk:enable-asset-metadata":true}',
CDK_OUTDIR: 'cdk.out' }...../node_modules/@aws-cdk/core/lib/context-provider.ts:74
throw new Error(Cannot retrieve value from context provider ${options.provider} since account/region are not specified at the stack level);
^
Error: Cannot retrieve value from context provider hosted-zone since account/region are not specified at the stack level
I am using TypeScript here.
to fix this error, you have to add { env: { region: "your-region", account: "your-account-id" } } to the call that creates the new stack instance.
@pagameba is there another way? Hard coding account id does not seem like a good idea.
I'm having a similar issue: 3130
@xelibrion I'm just getting started with CDK, but I believe that this should be picked up by CDK from your account credentials and isn't due to a bug - my suggestion was just a work-around.
@mrcustard sounds like #3130 is the same symptom I faced, this was my work-around so I could keep moving forward.
This is the expected behavior >= 0.36.0. We wanted to reduce the implicit effect the user's environment has on the synthesis result as this can cause production risks, so we made this explicit. If you don't specify env when a stack is defined, the stack will be "env-agnostic" which means that Vpc.fromLookup won't be able to work. If, for development purposes you wish your stack to inherit it's environment information from the CLI, you can use the CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION environment variables:
DeploymentStack(
app=app,
id='Dev',
env={
'account': os.environ['CDK_DEFAULT_ACCOUNT'],
'region': os.environ['CDK_DEFAULT_REGION']
}
)
@eladb I am still facing this issue even after providing the env. This error only happens when i use nested stacks. For normal stack it works fine!
Most helpful comment
@pagameba is there another way? Hard coding account id does not seem like a good idea.