Skip to content
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

Vpc.from_lookup in v0.36 (python): Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level #3082

Closed
rstrahan opened this issue Jun 26, 2019 · 12 comments
Labels
bug This issue is a bug. p2

Comments

@rstrahan
Copy link

  • I'm submitting a ...

    • [X ] 🪲 bug report
  • 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)

  • What is the expected behavior (or behavior of feature suggested)?

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:

    • CDK CLI Version: 0.36.0 (build 6d38487)
    • Module Version: 0.36.0
    • OS: [Cloud 9: Amazon Linux AMI release 2018.03]
    • Language: [Python ]

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)
@fulghum fulghum added the p2 label Jun 26, 2019
@fulghum
Copy link
Contributor

fulghum commented Jun 26, 2019

We should update the error message so that it's clear how to resolve this.

@clareliguori
Copy link
Member

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.

@KarlHarrisSungardAS
Copy link

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.

@jesse-topcu
Copy link

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

@xelibrion
Copy link

@fulghum would be great to see the proper message in this thread as well

@shylaharild
Copy link

shylaharild commented Jun 27, 2019

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.

@NGL321 NGL321 added bug This issue is a bug. vpc labels Jun 27, 2019
@pagameba
Copy link

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.

@xelibrion
Copy link

@pagameba is there another way? Hard coding account id does not seem like a good idea.

@mrcustard
Copy link

I'm having a similar issue: 3130

@pagameba
Copy link

@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.

@eladb
Copy link
Contributor

eladb commented Jun 30, 2019

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']
  }
)

@amilnayar92
Copy link

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2
Projects
None yet
Development

No branches or pull requests