aws-cdk: Vpc.fromLookup can't determine region

I receive this 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)

even though I set the env in the stack and also set the environment variable CDK_DEFAULT_REGION.

Reproduction Steps

import cdk = require('@aws-cdk/core');
import {  Vpc } from "@aws-cdk/aws-ec2";

const stack = new cdk.Stack(
    new cdk.App(),
    'test',
    {
        env: {
            region: 'us-west-2'
        }
    }
);

Vpc.fromLookup( stack, 'vpc-lookup', { isDefault: true} );

Error Log

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

  • CLI Version : 1.13.1 (build 96cfc63)
  • Framework Version:: 1.13.1
  • OS : Linux (Manjaro)
  • Language : TypeScript

This is 🐛 Bug Report

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Read up on this: https://docs.aws.amazon.com/cdk/latest/guide/environments.html

If you want to use dynamic AWS accounts, you must use code like the following:

new MyDevStack(this, 'dev', { 
  env: { 
    account: process.env.CDK_DEFAULT_ACCOUNT, 
    region: process.env.CDK_DEFAULT_REGION 
}});

We recommend that you don’t, and that if you want to deploy to 5 different accounts, you instantiate your stack 5 times in your application, once for each account. Defining where a stack goes is different from obtaining credentials necessary to deploy it.

Closing this issue.

@niels1voo, I cannot reproduce your problem. The code you showed works for me as intended. Please open a new issue if you would like to dig into this deeper.

No, it’s fine to use for those cases, but you can’t rely on things like Vpc.from_lookup in those templates (that makes sense I hope, because every account / region will have different VPCs!). You would probably either create the VPC in that same template, or pass in the VPC ID through a parameter, for example.

How does it work as a param? I can’t synth a stack which uses CfnParameter to pass VpcId to fromLookup because of the error Vpc.fromLookup() must be concrete (no Tokens). I’m struggling to come up with a strategy to synth a stack in CDK which dynamically looks up a vpc based on vpcId or tag input and doesn’t have hardcoded account/region.

No, it’s fine to use for those cases, but you can’t rely on things like Vpc.from_lookup in those templates (that makes sense I hope, because every account / region will have different VPCs!). You would probably either create the VPC in that same template, or pass in the VPC ID through a parameter, for example.

@skinny85 Any how to specify the current account and region where the stack is being created?

From #5724, I assume you’re using Python, so it would be:

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # your constructs go here...

And then when actually instantiating MyStack:

from aws_cdk import core
from hello.hello_stack import MyStack

app = core.App()
MyStack(app, "Name", env={'region': 'us-west-2', 'account': '123456789012'})
app.synth()

@skinny85 Any how to specify the current account and region where the stack is being created?

@aaronsturm like the error message says, you’re missing the account in your env. So it should be:

const stack = new cdk.Stack(new cdk.App(), 'test', {
  env: {
    region: 'us-west-2',
    account: '123456789012', // your account here
  },
});

Thanks, Adam