Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet: Error importing existing Vpc #816

Closed
IndikaUdagedara opened this issue Sep 30, 2018 · 6 comments
Closed

dotnet: Error importing existing Vpc #816

IndikaUdagedara opened this issue Sep 30, 2018 · 6 comments
Assignees
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. language/dotnet Related to .NET bindings

Comments

@IndikaUdagedara
Copy link

IndikaUdagedara commented Sep 30, 2018

I'm trying to import an existing Vpc into a dotnet cdk stack using the example here provided by @eladb

            var vpc = VpcNetworkRef.Import(this, "ExternalVpc", new VpcNetworkRefProps
            {
                VpcId = "vpc-ff",
                AvailabilityZones = new AvailabilityZoneProvider(this).AvailabilityZones,
                PrivateSubnetIds = new[] {"subnet-0xx", "subnet-0yy", "subnet-0zz"},
                PublicSubnetIds = new[] {"subnet-aa", "subnet-bb", "subnet-cc"}
            });

However, when I do cdk deploy it says

Unhandled Exception: System.MemberAccessException: Cannot create an instance of Amazon.CDK.AWS.EC2.VpcNetworkRef because it is an abstract class.
   at System.Reflection.RuntimeConstructorInfo.CheckCanCreateInstance(Type declaringType, Boolean isVarArg)
   at System.Reflection.RuntimeConstructorInfo.ThrowNoInvokeException()
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Amazon.JSII.Runtime.Services.ReferenceMap.GetOrCreateNativeReference(ByRefValue byRefValue)
   at Amazon.JSII.Runtime.Services.Converters.JsiiToFrameworkConverter.TryConvertClass(IReferenceMap referenceMap, Object value, Object& result)
   at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvertCustomType(IReferenceMap referenceMap, Object value, Boolean isOptional, String fullyQualifiedName, Object& result)
   at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvert(TypeReference typeReference, IReferenceMap referenceMap, Object value, Object& result)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeMethodCore[T](JsiiMethodAttribute methodAttribute, Object[] arguments, Func`3 beginFunc, Func`3 invokeFunc)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.InvokeStaticMethod[T](Type type, Object[] arguments, String methodName)
   at Stack.AppStack..ctor(App parent, String name, IStackProps props) in E:\Stack\AppStack.cs:line 13

Is this a bug? Or is there another way to import an existing Vpc? (on a side note why is it required to specify all the properties and not just the VpcId?)

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 30, 2018

Duplicate of aws/jsii#223. @costleya, is the fix released yet, and if so, what version of what tool should be installed?

@rix0rrr rix0rrr added bug This issue is a bug. language/dotnet Related to .NET bindings labels Oct 1, 2018
@eladb
Copy link
Contributor

eladb commented Oct 10, 2018

Coming up in jsii-0.7.7

@IndikaUdagedara
Copy link
Author

IndikaUdagedara commented Oct 13, 2018

With jsii-0.7.7 the error Cannot create an instance of Amazon.CDK.AWS.EC2.VpcNetworkRef because it is an abstract class. seems to be gone but now getting a different error

            var vpc = VpcNetworkRef.Import(this, "ExternalVpc", new VpcNetworkRefProps
            {
                VpcId = "vpc-3f0a6758",
                AvailabilityZones = new [] {"ap-southeast-2a", "ap-southeast-2b", "ap-southeast-2c" },
                PrivateSubnetIds = new[]
                    {"subnet-07a29232a40c5b7c3", "subnet-087d4e011854270fd", "subnet-08d59b2790f41f7fe"},
                PublicSubnetIds = new[] {"subnet-59207e3e", "subnet-7c66ac24", "subnet-8ac78fc3" }
            });

            Console.WriteLine($"VpcId = {vpc?.VpcId}");
            
            var lambdaSecurityGroup = new SecurityGroup(this, "LambdaSecurityGroup", new SecurityGroupProps
            {
                Description = "Enable lambda access to Vpc resources",
                Vpc = vpc,
                GroupName = "LambdaSecurityGroup"
            });

Looks like it imports the Vpc, but when creating a security group attached to it throws

VpcId = vpc-3f0a6758

Unhandled Exception: Amazon.JSII.Runtime.JsiiException: Cannot read property 'vpcId' of undefined
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
   at ....AppStack..ctor(App parent, String name, IStackProps props) in ...\AppStack.cs:line 28

Any idea what's going wrong?

PS.

Can you please add some examples on how to import a VPC with multiple private subnets (e.g. to import a subnet configuration like this one where there are private subnets for Application and DB tiers)?

@IndikaUdagedara
Copy link
Author

Any updates on this one?

@eladb eladb changed the title Error importing existing Vpc dotnet: Error importing existing Vpc Dec 17, 2018
@costleya
Copy link
Contributor

Is this still an issue?

I wrote the following bootstrap code, and copied in the code you provided.

using System;
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;

namespace aws_cdk_816
{
    class TestStack : Stack
    {
        public TestStack(App app, string name, IStackProps stackProps) : base(app, name, stackProps)
        {
            var vpc = VpcNetworkRef.Import(this, "ExternalVpc", new VpcNetworkRefProps
            {
                VpcId = "vpc-3f0a6758",
                AvailabilityZones = new[] {"ap-southeast-2a", "ap-southeast-2b", "ap-southeast-2c"},
                PrivateSubnetIds = new[]
                    {"subnet-07a29232a40c5b7c3", "subnet-087d4e011854270fd", "subnet-08d59b2790f41f7fe"},
                PublicSubnetIds = new[] {"subnet-59207e3e", "subnet-7c66ac24", "subnet-8ac78fc3"}
            });

            Console.WriteLine($"VpcId = {vpc?.VpcId}");

            new SecurityGroup(this, "LambdaSecurityGroup", new SecurityGroupProps
            {
                Description = "Enable lambda access to Vpc resources",
                Vpc = vpc,
                GroupName = "LambdaSecurityGroup"
            });
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            new TestStack(app, "TestStack", new StackProps());
            app.Run();
        }
    }
}

Which ran successfully, giving the following output:

VpcId = vpc-3f0a6758
Resources:
  LambdaSecurityGroup0BD9FC99:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable lambda access to Vpc resources
      GroupName: LambdaSecurityGroup
      SecurityGroupEgress:
        - CidrIp: 0.0.0.0/0
          Description: Allow all outbound traffic by default
          IpProtocol: "-1"
      SecurityGroupIngress:
        []
      VpcId: vpc-3f0a6758
    Metadata:
      aws:cdk:path: TestStack/LambdaSecurityGroup/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: aws-cdk=0.21.0,@aws-cdk/aws-ec2=0.21.0,@aws-cdk/aws-iam=0.21.0,@aws-cdk/cdk=0.21.0,@aws-cdk/cx-api=0.21.0,jsii-runtime=DotNet/4.0.30319.42000

I am using .NET Core 2.2 and CDK version 0.21.0.

@costleya costleya added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Dec 21, 2018
@IndikaUdagedara
Copy link
Author

Thanks @costleya. Haven't tested recently. Please close if it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. language/dotnet Related to .NET bindings
Projects
None yet
Development

No branches or pull requests

4 participants