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.

(aws-ec2): Vpc.from_lookup does not work out of the box on init templates

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
hoegertncommented, Jan 30, 2021

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

0reactions
github-actions[bot]commented, Mar 22, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

ec2.Vpc.fromLookup() - AWS Documentation - Amazon.com
No information is available for this page.
Read more >
Cdk use existing vpc - cattolimonica.it
EC2. This function only needs to be used to use VPCs not defined in your CDK ... The CDK will connect to AWS...
Read more >
awslabs/aws-cdk - Gitter
Can someone please explain the best approach to utilize the vpc lookup using the importValue function? ex: const vpcId = cdk.Fn.importValue(' ...
Read more >
Unable to import VPC in AWS cdk - python - Stack Overflow
I want to use default VPC in the resources. Below is my code. app.py from aws_cdk import core from cdk_python.cdk_python_stack import ...
Read more >
Using the AWS CDK In Real Life - Chariot Solutions
An S3 Bucket; An EC2 instance with Zoneminder; Application Load Balancer; Route 53; SSM Parameter Store; Secrets Manager. If I don't account for ......
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