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)
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:
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.
How does it work as a param? I can’t synth a stack which uses
CfnParameterto pass VpcId tofromLookupbecause of the errorVpc.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_lookupin 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.From #5724, I assume you’re using Python, so it would be:
And then when actually instantiating
MyStack:@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
accountin yourenv. So it should be:Thanks, Adam