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

Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level #3130

Closed
1 of 5 tasks
mrcustard opened this issue Jun 28, 2019 · 11 comments · Fixed by #3137
Labels
needs-triage This issue or PR still needs to be triaged.

Comments

@mrcustard
Copy link

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

    • 🪲 bug report
    • 🚀 feature request
    • 📚 construct library gap
    • ☎️ security issue or vulnerability => Please see policy
    • ❓ 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)

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

@eladb eladb closed this as completed Jun 30, 2019
@eladb eladb reopened this Jun 30, 2019
eladb pushed a commit that referenced this issue Jun 30, 2019
…tacks

Following up on #2922: improve the error message emitted when trying to read context from an environment-agnostic stack by calling out CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION as a way to inherit environment information from the CLI.

Fixes #3078 
Fixes #3120 
Fixes #3130
@mrcustard
Copy link
Author

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

eladb pushed a commit that referenced this issue Jul 1, 2019
…tacks (#3137)

Following up on #2922: improve the error message emitted when trying to read context from an environment-agnostic stack by calling out CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION as a way to inherit environment information from the CLI.

Fixes #3078
Fixes #3120
Fixes #3130
@kaihendry
Copy link

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

@kaihendry
Copy link

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

@hoegertn
Copy link
Contributor

hoegertn commented Sep 9, 2019

@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,
	},
}

@niels1voo
Copy link

niels1voo commented Nov 4, 2019

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

@jimmyhmiller
Copy link
Contributor

jimmyhmiller commented Jan 23, 2020

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.

@codestorycooked
Copy link

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

@davxiao
Copy link

davxiao commented Aug 14, 2020

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 
    },
  });

@zafarella
Copy link

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 
    },
  });

Even when provided, it still reports error:

➜ cdk list --long
(node:43861) ExperimentalWarning: The fs.promises API is experimental
npx: installed 9 in 1.718s
When providing vpc options you need to provide a subnet for each AZ you are using
Subprocess exited with error 1

@sebsonjura
Copy link

sebsonjura commented Feb 19, 2021

A possible solution, if you end up here

My pipeline stack was not passing the props to its parent

export class PipelineStack extends Stack { 
    private props: PipelineStackProps;
    private artifacts: Map<string, Artifact>;
    private buildActions: Map<string, CodeBuildAction>;
    private resourcePrefix: string;

    constructor(scope: Construct, id: string, props: PipelineStackProps) { 
        super(scope, id);

working:

export class PipelineStack extends Stack { 
    private props: PipelineStackProps;
    private artifacts: Map<string, Artifact>;
    private buildActions: Map<string, CodeBuildAction>;
    private resourcePrefix: string;

    constructor(scope: Construct, id: string, props: PipelineStackProps) { 
        super(scope, id, props);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants