question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use FindInMap to set property of number type?

See original GitHub issue

Fn.findInMap returns a string token. However, how to use it set a property which is number type?

The following usage will throw Exception in thread "main" java.lang.NumberFormatException: For input string: "${Token[TOKEN.14]}"

Mapping writeCapacityMapping = new Mapping(parent, "TableWriteCapacityMapping", MappingProps.builder()
                .withMapping(ImmutableMap.of(
                        Stage.beta.name(), ImmutableMap.of(
                                "min", 5,
                                "max", 100),
                        Stage.prod.name(), ImmutableMap.of(
                                "min", 400,
                                "max", 1200)))
                .build());

...
String minCapacity = Fn.findInMap("TableWriteCapacityMapping", stageParameter.getValueAsString(), "min");
String maxCapacity = Fn.findInMap("TableWriteCapacityMapping", stageParameter.getValueAsString(), "max");
 
ScalableTargetProps props = ScalableTargetProps.builder()
                .withMinCapacity(Integer.valueOf(minCapacity))
                .withMaxCapacity(Integer.valueOf(maxCapacity))
                .build();

And the expected cloudformation template will be:

Mappings:
  WriteCapacityMap:
    beta:
      min: 5
      max: 100
    prod:
      min: 400
      max: 1200
 
TableWriteCapacityScalableTarget:
    Type: "AWS::ApplicationAutoScaling::ScalableTarget"
    Properties:
      MinCapacity:
        Fn::FindInMap: [WriteCapacityMap, { Ref: "Stage" }, min]
      MaxCapacity:
        Fn::FindInMap: [WriteCapacityMap, { Ref: "Stage" }, max]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rix0rrrcommented, Jun 4, 2021

Token.asNumber() is the right way to force tokens to the right type.

1reaction
skinny85commented, Feb 17, 2021

@eladb I’m re-opening this one, let me know if you disagree!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use AWS CDK FindInMap to set property of number ...
ScalableTarget accepts parameter of Number type. void setMinCapacity(final java.lang.Number value);. AWS CloudFormation library: ...
Read more >
Fn::FindInMap - AWS CloudFormation
The following example shows how to use Fn::FindInMap for a template with a Mappings ... resource whose ImageId property is set by the...
Read more >
Fn::FindInMap in CloudFormation explained with an example
Welcome to part 3.5 of this tutorial series on AWS CloudFormation. In this tutorial, I have covered the intrinsic function ...
Read more >
What is FindInMap in AWS CloudFormation? | by Tek Loon
Mappings allow you to define a set of mapping in the CloudFormation template and ! ... FindInMap in the ImageId property and we...
Read more >
Can FindInMap return a list? - Server Fault
I've tried various combinations of the example below, but I always get the same error Value of property Subnets must be of type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found