Unable to extract the values using cdk synth command for Fn.importValue
General Issue
Fn.importValue unable to fetch the value and still returning Tokens
The Question
Hi Team,
I have an implementation where i need to fetch the existing ECS Cluster details. I have the import values from the VPC and Security Group of another base stack. However, While using the Fn.importValue i am getting an error :
All arguments to look up a security group must be concrete (no Tokens)
Here is my source code
ecs.Cluster.fromClusterAttributes(this, 'existing', { clusterName: Fn.importValue(${propsBackend.clusterName}-ExistingClusterStackName), vpc: vpc, securityGroups: [ ec2.SecurityGroup.fromLookup( this, 'ECSInstanceSecurityGroup', Fn.importValue(${propsBackend.clusterName}-ExistingClusterSecurityGroup), ), ], });
Whenever i run the cdk synth or cdk deploy command :
i am getting the above error
Error: All arguments to look up a security group must be concrete (no Tokens)
Please suggest what needs to be done here.
CDK CLI Version
1.132.0
Framework Version
No response
Node.js Version
17.0.1
OS
Mac
Language
Typescript
Language Version
Typescript(4.2.4)
Other information
Hi Team,
I have an implementation where i need to fetch the existing ECS Cluster details. I have the import values from the VPC and Security Group of another base stack. However, While using the Fn.importValue i am getting an error :
All arguments to look up a security group must be concrete (no Tokens)
Here is my source code
ecs.Cluster.fromClusterAttributes(this, 'existing', {
clusterName: Fn.importValue(${propsBackend.clusterName}-ExistingClusterStackName),
vpc: vpc,
securityGroups: [
ec2.SecurityGroup.fromLookup(
this,
'ECSInstanceSecurityGroup',
Fn.importValue(${propsBackend.clusterName}-ExistingClusterSecurityGroup),
),
],
});
Whenever i run the cdk synth or cdk deploy command : i am getting the above error Error: All arguments to look up a security group must be concrete (no Tokens)
Please suggest what needs to be done here.
A token is an unresolved value in the CDK code. Take a look at your code below, i've annotated all the token values being used.
ecs.Cluster.fromClusterAttributes(this, 'existing', {
clusterName: Fn.importValue(${propsBackend.clusterName}-ExistingClusterStackName), // Token
vpc: vpc,
securityGroups: [
ec2.SecurityGroup.fromLookup( this, 'ECSInstanceSecurityGroup', // Token
Fn.importValue(${propsBackend.clusterName}-ExistingClusterSecurityGroup), // Token
),
],
});
This use case is currently not supported. It's not possible to use fromLookup with a resource that is not available during synthesis. Fn.importValue is only resolved during deployment and therefore it is represented as a token when you call fromLookup.
You have to resolve these before calling the lookup. A few ways of doing this:
- Using hardcoded IDs
- Using hardcoded tags on resources
- Store the IDs in SSM parameter store
- Use the AWS SDK to query for the id
- If your other stacks are within the same source repo, then you can commit your
cdk.context.jsonto preserve IDs after deployments and reference them usingthis.node.tryGetContext.
Related issues: https://github.com/aws/aws-cdk/issues/12160, https://github.com/aws/aws-cdk/issues/3600, https://github.com/aws/aws-cdk/issues/12485
Use the AWS SDK to query for the id -- Do we have library for AWS Typescript SDK to fetch the values of the cloudformation?
I was wondering if there is any patterns already to configure this fix. Because i believe there will be many use cases where Cloudformation outputs needs to be configured and storing them in ID's in SSM is an overhead according to me.
You can use the aws-sdk-js-v3 which has first-class Typescript support.
I was wondering if there is any patterns already to configure this fix. Because i believe there will be many use cases where Cloudformation outputs needs to be configured and storing them in ID's in SSM is an overhead according to me.
I don't have any examples to reference but I can whip up some prototype projects for you. I'll follow up with you in this thread.
⚠️ One thing to keep in mind is that you cannot await in Typescript class constructs.
Options:
- Fetch the data before initializing any CDK constructs.
- Add a custom method to the stack class that fetches the data, then add the remaining data-dependent resources to the stack.
Here are some sample projects that demonstrate how to use the SDK to retrieve existing AWS resource values:
-
Example that does not use Typescript classes: https://github.com/ryparker/aws-cdk-sample-import-with-sdk
-
Example that uses Typescript classes: https://github.com/ryparker/aws-cdk-sample-import-with-sdk/tree/using-typescript-classes
Wow. This works like charm! You are awesome!
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
@ryparker would it make sense to have the CDK itself support resolving Fn.importValue at synth time in some way?
I am comfortable with the idea looking up resources at synth time rather than deploy, but in many cases these rely on exports/imports for lookup (particularly if they predate CDK), which means using import value to look them up.
This is technically possible by setting up my own SDK connection etc, but that feels quite heavyweight. Particularly since the cloudformation API pushes you towards listing exports in bulk, there is no get-export-by-name command that I can see.
After looking into this, it sounds like it could be related to https://github.com/aws/aws-cdk-rfcs/issues/18 where they are asking for the ability to write new Context Providers.
This would allow a Context Provider to be written that would retrieve the stack's exported value, in the same way that CDK currently retrieves VPC IDs and other data, and then hard codes it into the CloudFormation template so there is no link to another stack.
After looking into this, it sounds like it could be related to aws/aws-cdk-rfcs#18 where they are asking for the ability to write new Context Providers.
https://github.com/aws/aws-cdk-rfcs/issues/18 is focussed on enabling 3rd party context providers though. Are you suggesting the community uses this mechanism one day in the future to implement support for AWS stack exports, relegating them to the same level as non-AWS providers? Why would this not be supported by CDK itself?
Perhaps I misunderstood but I was assuming that in the process of adding third party context providers, it might be easier for CDK to ship with a bunch of built in providers including one that handles the stack exports.
Otherwise, if it could be done already, I would've expected a PR to show up already adding support for it.