[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',
capacity_providers=['FARGATE_SPOT'],
default_capacity_provider_strategy=[
{
'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',
cluster=cluster,
launch_target=sfn_tasks.EcsFargateLaunchTarget(
platform_version=ecs.FargatePlatformVersion.LATEST
),
integration_pattern=sfn.IntegrationPattern.RUN_JOB,
task_definition=task,
subnets=ec2.SubnetSelection(
subnets=vpc.private_subnets
)
)
How do I avoid the undefined error?
Environment
- 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:
- Created 3 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 youcdk context --clear
, your script will fail on first run because the context has no subnets to select from during synthesis of theec2.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 thencdk 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.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 avpc
parameter). Then you have a Cluster object compatible withEcsRunTask
(orEcsTask
in my case).But by specifying the SubnetSelection, the construct should be able to resolve the associated
vpc
if not resolvable via thecluster
parameter.