question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vpc.fromLookup can't determine region

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
rix0rrrcommented, Nov 12, 2019

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.

3reactions
stownscommented, May 8, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot retrieve value from context provider ami since account ...
If we pick apart the error, it's saying in order to locate the proper AMIs for your EC2 instances, it needs a region/account...
Read more >
interface VpcLookupOptions · AWS CDK
Whether to match the default VPC. region? Type: string (optional, default: Current stack's environment region). Optional to override inferred region.
Read more >
awslabs/aws-cdk - Gitter
JSIIError: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Either configure "env" with ...
Read more >
[CDK] Vpc.fromLookup()でエラー 「Cannot retrieve value ...
fromLookup ()でエラー 「Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level」が ...
Read more >
@aws-cdk/aws-ec2 - npm
fromLookup () . The CDK CLI will search for the specified VPC in the the stack's region and account, and import the subnet...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found