feat(core): lazy mappings will only synthesize if keys are unresolved#15617
feat(core): lazy mappings will only synthesize if keys are unresolved#15617mergify[bot] merged 11 commits intomasterfrom
Conversation
This feature adds new static methods to the CfnMapping construct that
allow the creation of "lazy" mappings. A lazy mapping will only create
a Mappings section in the synthesized CFN template if some "find"
operation on the mapping was not able to return a value since one or
more of the lookup keys were unresolved.
Usage:
```ts
// Register the mapping as a lazy mapping.
CfnMapping.registerLazyMap('UNIQUEMAPPINGID', {
TopLevelKey: {
SecondLevelKey: 'value',
},
});
// Later, find a value from the mapping. Since the keys are both
// resolved, this returns a resolved value and does not create a
// CfnMapping.
CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', 'SecondLevelKey');
// > 'value'
// If we try to find a value from the mapping using unresolved keys, a
// CfnMapping is created and a Fn::FindInMap is returned.
CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', Aws.REGION);
// > { Fn::FindInMap: [ 'UNIQUEMAPPINGID', 'TopLevelKey', { Ref: 'AWS::Region' } ] }
```
eladb
left a comment
There was a problem hiding this comment.
Would it make sense to make this the default behavior of a normal map? (with an option to disable). What's the use of a map that is unused?
In my mind, most of the users who use |
|
|
||
| private informLazyUse() { | ||
| if (!this.lazyInformed) { | ||
| Annotations.of(this).addInfo('Consider making this CfnMapping a lazy mapping by providing `lazy: true`: either no findInMap was called or every findInMap could be immediately resolved without using Fn::FindInMap'); |
There was a problem hiding this comment.
| Annotations.of(this).addInfo('Consider making this CfnMapping a lazy mapping by providing `lazy: true`: either no findInMap was called or every findInMap could be immediately resolved without using Fn::FindInMap'); | |
| Annotations.of(this).addWarning('Consider making this CfnMapping a lazy mapping by providing `lazy: true`: either no findInMap was called or every findInMap could be immediately resolved without using Fn::FindInMap'); |
There was a problem hiding this comment.
warning feels a bit intense here since they're not necessarily doing anything wrong and I don't think strict synthesis should fail in this case
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
…aws#15617) This feature adds new static methods to the CfnMapping construct that allow the creation of "lazy" mappings. A lazy mapping will only create a Mappings section in the synthesized CFN template if some "find" operation on the mapping was not able to return a value since one or more of the lookup keys were unresolved. Usage: ```ts // Register the mapping as a lazy mapping. CfnMapping.registerLazyMap('UNIQUEMAPPINGID', { TopLevelKey: { SecondLevelKey: 'value', }, }); // Later, find a value from the mapping. Since the keys are both // resolved, this returns a resolved value and does not create a // CfnMapping. CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', 'SecondLevelKey'); // > 'value' // If we try to find a value from the mapping using unresolved keys, a // CfnMapping is created and a Fn::FindInMap is returned. CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', Aws.REGION); // > { Fn::FindInMap: [ 'UNIQUEMAPPINGID', 'TopLevelKey', { Ref: 'AWS::Region' } ] } ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
…aws#15617) This feature adds new static methods to the CfnMapping construct that allow the creation of "lazy" mappings. A lazy mapping will only create a Mappings section in the synthesized CFN template if some "find" operation on the mapping was not able to return a value since one or more of the lookup keys were unresolved. Usage: ```ts // Register the mapping as a lazy mapping. CfnMapping.registerLazyMap('UNIQUEMAPPINGID', { TopLevelKey: { SecondLevelKey: 'value', }, }); // Later, find a value from the mapping. Since the keys are both // resolved, this returns a resolved value and does not create a // CfnMapping. CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', 'SecondLevelKey'); // > 'value' // If we try to find a value from the mapping using unresolved keys, a // CfnMapping is created and a Fn::FindInMap is returned. CfnMapping.findInLazyMap(scope, 'UNIQUEMAPPINGID', 'TopLevelKey', Aws.REGION); // > { Fn::FindInMap: [ 'UNIQUEMAPPINGID', 'TopLevelKey', { Ref: 'AWS::Region' } ] } ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This feature adds new static methods to the CfnMapping construct that
allow the creation of "lazy" mappings. A lazy mapping will only create
a Mappings section in the synthesized CFN template if some "find"
operation on the mapping was not able to return a value since one or
more of the lookup keys were unresolved.
Usage:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license