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

How to use FnImportValue where CDK wants a string or list of string? #674

Closed
dsilvasc opened this issue Sep 6, 2018 · 6 comments
Closed
Labels
@aws-cdk/core Related to core CDK functionality

Comments

@dsilvasc
Copy link

dsilvasc commented Sep 6, 2018

Hi,

I'm trying to import a VPC from an existing (non-cdk) stack, following the examples in:
https://gist.github.com/mipearson/aeaf303b0770c25f8b5f6e360594cfbf
#603
#506

I added exports in my existing stack for the VPC ID, CIDR block, availability zones, and subnets.

In order to call VpcNetworkRef.import_, I need to pass it VpcNetworkRefProps, and the builder for the props wants a list of strings for AZs.

None of these type check as arguments to VpcNetworkRefProps.builder().withVpcId(id).withAvailabilityZones:

  • new FnImportValue("other-stack-AZs") where other-stack-AZs is a comma-separated export (Fn::Join on the other stack's cloudformation template)
  • new FnSplit(",", new FnImportValue("other-stack-AZs"))
  • [new FnImportValue("other-stack-AZ1"), new FnImportValue("other-stack-AZ2"), new FnImportValue("other-stack-AZ3")]

How can I import availability zones from another stack?

And more generally, how should we use FnImportValue where CDK wants a string or a list of strings?

@eladb
Copy link
Contributor

eladb commented Sep 6, 2018

CDK tokens, such as FnImportValue can be represented as strings, so you should be able to just do [ new FnImportValue("other-stack-az1").toString(), new FnImportValue("other-stack-az2").toString(), ... ]. Can't say that's super elegant but that should work.

Would that work for your use case?

@dsilvasc
Copy link
Author

dsilvasc commented Sep 6, 2018

Thank you -- that would work.

How about in places where CloudFormation accepted Fn::Split but CDK wants a list? For example:

Resources:
  LoadBalancer:
    Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
    Properties:
      Subnets: !Split [ ",", !ImportValue shared-infra-PublicSubnetIDs ]
  AutoScalingGroup:
    Type: 'AWS::AutoScaling::AutoScalingGroup'
    Properties:
      VPCZoneIdentifier: !Split [ ",", !ImportValue shared-infra-PrivateSubnetIDs ]

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 6, 2018

You are being bitten by this issue: #607

For your second problem (can't use a FnSplit() in places where CDK expects a parameter like SomeType[]), I'm afraid we don't really have a solution for that. If you know how many elements will be in the array, you can write it like this:

const parts = new FnSplit(',', new FnImportValue('shared-infra-PublicSubnetIDs);
const array = [
    new SomeType(new FnSelect(0, parts))),
    new SomeType(new FnSelect(1, parts))),
    new SomeType(new FnSelect(2, parts))),
    ...
    ];

But if you don't how many elements there are, unfortunately that won't work.

@eladb
Copy link
Contributor

eladb commented Sep 6, 2018

We don't have support for that, but I wonder if we could somehow support something like FnSplit.toStringArray which will return something that can be assigned to List<String> values. Still not sure how we can achieve something like that (@rix0rrr any idea?)

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 6, 2018

We could extend Token stringification to have different encodings for different types... but the types would have to be known beforehand which we also don't have.

With the type system that we have, not much we can do I think.

@eladb
Copy link
Contributor

eladb commented Sep 6, 2018

I don't think we need a general-purpose solution, just support for string arrays.

@rix0rrr rix0rrr added the @aws-cdk/core Related to core CDK functionality label Sep 20, 2018
@eladb eladb closed this as completed Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality
Projects
None yet
Development

No branches or pull requests

3 participants