Aws-cdk: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level

Created on 28 Jun 2019  路  9Comments  路  Source: aws/aws-cdk

Note: for support questions, please first reference our documentation, then use Stackoverflow. This repository's issues are intended for feature requests and bug reports.

  • I'm submitting a ...

    • [ ] :beetle: bug report
    • [ ] :rocket: feature request
    • [ ] :books: construct library gap
    • [ ] :phone: security issue or vulnerability => Please see policy
    • [X] :question: support request => Please see note at the top of this template.
  • What is the current behavior?
    When trying to use ec2.Vpc.fromLookup, and passing { env: { region: "your-region", account: "your-account-id"} } I get and error when doing cdk list: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level
    Link to code: https://gist.github.com/mrcustard/828242e3e5b8786776fd0c78d1e74e63

  • What is the expected behavior (or behavior of feature suggested)?
    I would expect to be able to lookup the vpc and get ec2.IVPc returned

  • Please tell us about your environment:

    • CDK CLI Version: 0.36.0 (build 6d38487)
    • Module Version: 0.36.0
    • OS: OSX Mojave
    • Language: TypeScript
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

needs-triage

Most helpful comment

@kaihendry you do not have to set these variables. They are set by CDK but you have to use them in den StackProps.

{
    env: {
        account: process.env.CDK_DEFAULT_ACCOUNT,
        region: process.env.CDK_DEFAULT_REGION,
    },
}

All 9 comments

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

In this case I had both account and region specified in env as outlined here: https://gist.github.com/mrcustard/828242e3e5b8786776fd0c78d1e74e63#file-mssymbol-server-cdk-ts-L17-L18 and I still received the error message

I'm using 1.7.0 (build 8870695) with https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ec2-readme.html#importing-an-existing-vpc and I'm hitting this "Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level." despite

[hendry@t480s speed]$ export CDK_DEFAULT_ACCOUNT=812644853088
[hendry@t480s speed]$ export CDK_DEFAULT_REGION=ap-southeast-1

Workaround is to set up env in the stack. Wonder how to autofill this with my account number from my profile next.

@kaihendry you do not have to set these variables. They are set by CDK but you have to use them in den StackProps.

{
    env: {
        account: process.env.CDK_DEFAULT_ACCOUNT,
        region: process.env.CDK_DEFAULT_REGION,
    },
}

@hoegertn @eladb I'm doing exactly as you advise and I'm still getting this error when attempting to lookup an existing VPC. Very frustrating to get started with CDK due to this issue.

`import cdk = require("@aws-cdk/core");
import ec2 = require("@aws-cdk/aws-ec2");

export class TestInitStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// The code that defines your stack goes here
ec2.Vpc.fromLookup(this, "whatever", { tags: { ["sx_name"]: "primary"}});

}
}

new TestInitStack(new cdk.App(), "TestInitStack", {
env: {
region: "us-east-1",
account: "123456789012"
}
});
`

cdk --verbose ls
CDK toolkit version: 1.15.0 (build bdbe3aa)
Command line arguments: {
_: [ 'ls' ],
verbose: true,
v: true,
'ignore-errors': false,
ignoreErrors: false,
json: false,
j: false,
ec2creds: undefined,
i: undefined,
'version-reporting': undefined,
versionReporting: undefined,
'path-metadata': true,
pathMetadata: true,
'asset-metadata': true,
assetMetadata: true,
'role-arn': undefined,
r: undefined,
roleArn: undefined,
staging: true,
'no-color': false,
noColor: false,
long: false,
l: false,
'$0': '/usr/local/bin/cdk'
}
Determining whether we're on an EC2 instance.
Does not look like EC2 instance.
cdk.json: {
"app": "npx ts-node bin/test-init.ts"
}
merged settings: {
versionReporting: true,
pathMetadata: true,
output: 'cdk.out',
app: 'npx ts-node bin/test-init.ts',
context: {},
tags: [],
assetMetadata: true,
toolkitBucket: {},
staging: true
}
Setting "CDK_DEFAULT_REGION" environment variable to us-east-1
Resolving default credentials
Retrieved account ID 123456789012 from disk cache
Setting "CDK_DEFAULT_ACCOUNT" environment variable to 123456789012
context: {
'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: '123456789012',
CDK_CONTEXT_JSON: '{"aws:cdk:enable-path-metadata":true,"aws:cdk:enable-asset-metadata":true}',
CDK_OUTDIR: 'cdk.out',
CDK_CLI_ASM_VERSION: '1.10.0',
CDK_CLI_VERSION: '1.15.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
Error: Subprocess exited with error 1
at ChildProcess. (/usr/local/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:115:23)
at ChildProcess.emit (events.js:210:5)
at ChildProcess.EventEmitter.emit (domain.js:476:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)

In case someone runs into this problem and made the same mistake as me. (@niels1voo it looks like you might have). I had been constructing my own stack and properly passing in the env. But I used the boilerplate and it had its own stack in bin/project-name.ts that was making a stack. I didn't realize that. Instead of making my own stack, I edited that one to include an environment. That made everything work.

You can also use account_name instead of aws account number :

new InfrastructureStack(app, 'sample-app-stack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region:process.env.CDK_DEFAULT_REGION
}
});

export CDK_DEFAULT_ACCOUNT=awsAccountName
export CDK_DEFAULT_REGION=awsRegion

In case someone runs into this problem and made the same mistake as me. (@niels1voo it looks like you might have). I had been constructing my own stack and properly passing in the env. But I used the boilerplate and it had its own stack in bin/project-name.ts that was making a stack. I didn't realize that. Instead of making my own stack, I edited that one to include an environment. That made everything work.

That's correct and just adding to that, the code would be something like

const process = require('process');

const app = new cdk.App();
new CdkTypescriptDemoStack(app, 'CdkTypescriptDemoStack', {
    env: {
      account: process.env.CDK_DEFAULT_ACCOUNT, 
      region: process.env.CDK_DEFAULT_REGION 
    },
  });
Was this page helpful?
0 / 5 - 0 ratings