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.

[stepfunctions-tasks] Cannot read property 'selectSubnets' of undefined

See original GitHub issue

❓ General Issue

I am importing an existing vpc using Vpc.fromLookup and would like to specify the private subnets to use when running an ECS Fargate Step Function task. However, I get the error Cannot read property 'selectSubnets' of undefined when I try to reference the imported vpc.

The Question

I would like to define a cluster that uses FARGATE_SPOT by default and so have this;

cluster = ecs.CfnCluster(
    self, 'cluster',
            'capacityProvider': 'FARGATE_SPOT',
            'weight': 1,
            'base': 0

I have an imported vpc;

vpc = ec2.Vpc.from_lookup(self, 'vpc', vpc_name='Existing VPC')

I want to create a Fargate task in a Step Function;

ecs_task = sfn_tasks.EcsRunTask(
    self, 'Run task',

How do I avoid the undefined error?


  • CDK CLI Version: 1.62.0 (build 8c2d7fc)
  • Module Version: 1.62.0
  • Node.js Version: v10.20.1
  • OS: Ubuntu
  • Language (Version): Python (3.7.4)

Other information

jsii.errors.JSIIError: Cannot read property 'selectSubnets' of undefined

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

cmoore1776commented, Feb 12, 2021

I think this is a problem where CDK doesn’t really know about the details of VPCs imported via vpc.from_lookup until you populate them into the context. So basically, if you cdk context --clear, your script will fail on first run because the context has no subnets to select from during synthesis of the ec2.SubnetSelection method.

I ran into this issue as well (although with a slightly different use case).

My solution was to create a dummy stack whose only content is the vpc.from_lookup call. I then cdk synth this dummy stack (and only the dummy stack, so comment out the stack that is broken.). This will populate the context with your VPC so that future attempts to lookup subnets do not fail. You don’t need to deploy the dummy stack, just synthesize it and then delete it or comment it out. Hacky, I know, but it solved my problem.

PierreKiwicommented, Feb 18, 2021

The problem is similar for EcsTask (one of the CW Events targets) -> cf. here.

The workaround is to create the CfnCluster in a separate stack and to do a lookup using from_cluster_attributes (where you specify the VPC via a vpc parameter). Then you have a Cluster object compatible with EcsRunTask (or EcsTask in my case).

But by specifying the SubnetSelection, the construct should be able to resolve the associated vpc if not resolvable via the cluster parameter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VPC "fromLookup" results in all Private Subnets and no Public ...
fromVpcAttributes using my vpcId , availabilityZones and SubnetIDs , I get the error "Cannot read property 'selectSubnets' of undefined".
Read more >
interface SelectedSubnets · AWS CDK
0.0/16" }) // Iterate the private subnets const selection = vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_NAT }); for (const subnet of selection.
Read more >
How to import an Existing VPC in AWS CDK | bobbyhadz
In order to import an existing VPC in CDK, we have to use the fromLookup static method on the Vpc construct. We have...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found