(core): `Vpc.fromVpcAttributes` using list tokens now throws errors
See original GitHub issueWhen importing a VPC using outputs from another stack, if the route table IDs are not specified for each imported subnet, a warning is printed to the console, as expected:
No routeTableId was provided to the subnet '#{Token[TOKEN.58]}'. Attempting to read its .routeTable.routeTableId will return null/undefined. (More info: https://github.com/aws/aws-cdk/pull/3171)
This warning line is wrongly evaluated by the new “floating token” check introduced by #11899, and throws an error when synthesizing the stack:
Found an encoded list token string in a scalar string context. Use 'Fn.select(0, list)' (not 'list[0]') to extract elements from token lists.
Additionally, this line is “consumed” by the floating-token check and does not reach the console.
Reproduction Steps
import * as cdk from "@aws-cdk/core";
import * as ec2 from "@aws-cdk/aws-ec2";
class TestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: cdk.StackProps) {
super(scope, id, props);
const vpcId = cdk.Fn.importValue("myVpcId");
const availabilityZones = cdk.Fn.split(",", cdk.Fn.importValue("myAvailabilityZones"));
const publicSubnetIds = cdk.Fn.split(",", cdk.Fn.importValue("myPublicSubnetIds"));
ec2.Vpc.fromVpcAttributes(this, "importedVpc", {
vpcId,
availabilityZones,
publicSubnetIds,
});
}
}
const app = new cdk.App();
new TestStack(app, "TestStack", {});
What did you expect to happen?
VPC is imported without error, and the following warning line is printed to the console:
No routeTableId was provided to the subnet '#{Token[TOKEN.58]}'. Attempting to read its .routeTable.routeTableId will return null/undefined. (More info: https://github.com/aws/aws-cdk/pull/3171)
What actually happened?
No warning is printed about the route table, and the stack fails to synthesize with the following error:
Found an encoded list token string in a scalar string context. Use 'Fn.select(0, list)' (not 'list[0]') to extract elements from token lists.
Environment
- CDK CLI Version : 1.77.0
- Framework Version: 1.77.0
- Node.js Version: 12.20.0
- OS : Linux (Pop!_OS 20.04 LTS x86_64)
- Language (Version): TypeScript 3.9.7
Other
The floating token check should ignore CDK “logging” lines if possible.
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:17 (12 by maintainers)
Top Results From Across the Web
Fix error: Found an encoded list token string in a scalar string ...
The error throw new Error: Found an encoded list token string in a scalar string context. Use 'Fn.select(0, list)' (not 'list[0]') to ...
Read more >Amazon EC2 Construct Library - AWS Documentation
A VPC consists of one or more subnets that instances can be placed into. CDK distinguishes three different subnet types: Public ( SubnetType.PUBLIC...
Read more >Importing VPC IDs into a stack with CDK - DEV Community
When we want to import a VPC ID from another stack using CDK, not all methods will accept the imported value. Errors can...
Read more >How to import existing VPC in aws cdk? - Stack Overflow
If I set isDefault to false, it gives me the correct one, and if I set it to true, it throws and error...
Read more >@aws-cdk/assert | Yarn - Package Manager
@aws-cdk/assert. owner aws664.8kApache-2.02.56.0TS vulns 0 vulnerabilities. An assertion library for use with CDK Apps. aws, cdk ...
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 FreeTop 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
Top GitHub Comments
@rix0rrr not sure if I’m missing something, but I’m still facing the same issue with
aws-cdk@1.82.0
when attempting to spin up an EKS cluster. Maybe it’s worth mentioning, the VPC has both public/private subnets, thus public/private Routing Tables. Please find my code below:Throwing the same error when resolving the template:
In addition, spinning an ECS cluster has no issues:
Greetings,
Is the LAMP stack scalable and durable example on on the example templates to provision application frameworks AWS page as good example of a template that can lead to the error when trying to resolve the subnets from the parameter?
Such that if you set a default for the subnets as follows
"Subnets" : { "Type" : "List<AWS::EC2::Subnet::Id>", "Description" : "The list of SubnetIds in your Virtual Private Cloud (VPC)", "ConstraintDescription" : "must be a list of at least two existing subnets associated with at least two different availability zones. They should be residing in the selected Virtual Private Cloud.", "Default": [ "subnet-123a351e", "subnet-456b351e" ] },
Which is referenced in the AutoScaling resource by
"VPCZoneIdentifier" : { "Ref" : "Subnets" }
When trying to resolve an item in the CfnAutoScalingGroup C.getVpcZoneIdentifier() list, using the template this.resolve(item) method, the error
Found an encoded list token string in a scalar string context. Use 'Fn.select(0, list)' (not 'list[0]') to extract elements from token lists.
is Expected?And a solution is to replace the reference to the subnet parameter as
"VPCZoneIdentifier" : [ "subnet-123a351e", "subnet-456b351e" ]
Or is there a coding solution for this and a way to recognize this problem?
Regards Alex