user3368052
user3368052

Reputation: 31

Unable to find select_subnets function in aws CDK

I'm trying to select private subnets using the subnet_group_name attribute using the select_subnets method of aws_ec2.Vpc in AWS CDK as mentioned in below code snippet:

from aws_cdk import core as cdk
from aws_cdk import aws_ec2 as ec2
from aws_cdk import core

class SimpleCdkStack(cdk.Stack):

def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
    super().__init__(scope, construct_id, **kwargs)
    subnets = ec2.Vpc.select_subnets(self,
        subnet_group_name="private-subnet"
    )
    print("Subnet Ids: " + subnets.subnet_ids)

The above error produces below error during its execution:

$ cdk diff

jsii.errors.JavaScriptError:
Error: Class @aws-cdk/core.Stack doesn't have a method 'selectSubnets'
  at Kernel._typeInfoForMethod (/tmp/tmphu1erjw6/lib/program.js:8420:27)
  at Kernel._findInvokeTarget (/tmp/tmphu1erjw6/lib/program.js:8340:33)
  at Kernel.invoke (/tmp/tmphu1erjw6/lib/program.js:7966:44)
  at KernelHost.processRequest (/tmp/tmphu1erjw6/lib/program.js:9479:36)
  at KernelHost.run (/tmp/tmphu1erjw6/lib/program.js:9442:22)
  at Immediate._onImmediate (/tmp/tmphu1erjw6/lib/program.js:9443:46)
  at processImmediate (internal/timers.js:461:21)

I have installed the required packages using below command

$ pip install aws_cdk.aws_ec2

Not sure where I'm going wrong.

AWS clearly has mentioned about the method being available for the aws_ec2.Vpc class here

Help appreciated !

Upvotes: 2

Views: 1477

Answers (1)

Ahmed Elgamal
Ahmed Elgamal

Reputation: 66

You should pass the Vpc reference to the select_subnet call as the first parameter but you actually have passed self which is a CDK Stack.

Example

from aws_cdk import aws_ec2 as ec2
from aws_cdk import core as cdk


class SimpleCdkStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        subnet_ids = ec2.Vpc.from_lookup(
            vpc_id='your vpc id'
        ).select_subnets(
            subnet_group_name="private-subnet"
        ).subnet_ids

        for subnet_id in subnet_ids:
            print("Subnet Ids: " + subnet_id)

Upvotes: 4

Related Questions