(aws-ec2): Vpc.from_lookup does not work out of the box on init templates
See original GitHub issueThis sample code that shows the env settings seem to be ignored when the VPCImport2 class is ran. If I comment out the import_vpc_stack line, it runs fine and creates the VPC. But when using the vpc.from_lookup function is always fails. This code is just a representation of trying to use vpc.from_import and showing that the env actually works.
Reproduction Steps
from aws_cdk import core, aws_ec2 as ec2
app = core.App()
vpc_id = "vpc-08c949884e65e3b6f"
#env = core.Environment(region="us-west-1",account="000")
env = {"region": "us-west-1", "account": "000"}
class addvpc(core.Stack):
def __init__(
self,
scope: core.Construct,
id: str,
env: dict,
**kwargs,
) -> None:
super().__init__(scope, id, **kwargs)
self.vpc = ec2.Vpc(
self,
id=f"Just-a-test-vpc",
max_azs=2,
cidr="192.168.0.0/16",
)
class VPCImport2(core.Stack):
def __init__(
self, scope: core.Construct, id: str, env: dict, vpc_id: str, **kwargs
) -> None:
super().__init__(scope, id, **kwargs)
print(env)
vpc = ec2.Vpc.from_lookup(
self,
"vpc_imported",
is_default=False,
vpc_id=vpc_id,
)
vpc_stack = addvpc(scope=app, id="Addfakevpc", env=env)
import_vpc_stack = VPCImport2(scope=app, id="vpc-import", env=env, vpc_id=vpc_id)
app.synth()
What did you expect to happen?
The VPC that was just created would get imported,
What actually happened?
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)
cdk list -v
...
Setting "CDK_DEFAULT_REGION" environment variable to us-west-1
Resolving default credentials
Retrieved account ID 000 from disk cache
Setting "CDK_DEFAULT_ACCOUNT" environment variable to 000
context: {
'availability-zones:account=000:region=us-west-1': [ 'us-west-1a', 'us-west-1c' ],
....
Environment
- CDK CLI Version : 1.8.1
- Framework Version:
- Node.js Version: v14.15.1
- OS : Debian Buster
- Language (Version): Python (3.9.1)
Other
I feel like the error that is being thrown is not the real error but is some other underlyaing problem as I cant believe that this would stop working just for one certain function.
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top GitHub Comments
@hornetmadness I think you are not transferring the env value to the super constructor call in your VPCImport2 class. This needs to be sent upstream to the stack.
super().__init__(scope, id, env=env, **kwargs)
@webdog I advise against using the CDK_DEFAULT_XXX as a fallback when no env is set. When doing lookups I think we want to make sure that users are specifying the target environment exactly and not using whatever credentials they have active at the moment of synth.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.