serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Strings that contain an integer starting with 0 are converted into integers

Open gaetanmorice opened this issue 9 years ago • 14 comments

For instance, if you have a parameter in your template with a value that is a string "012345" it is silently converted into the integer 012345 after aws cloudformation package action.

This can raise issues:

  • if the parameter is expecting a string as a value.
  • if you use condition function to compare this value with other ones.
  • if you use such a string as a key in a Mapping section (map keys have to be strings).

Type tags, such as !!str, are not preserved by the package action, so they cannot be used as a workaround.

gaetanmorice avatar Sep 20 '17 12:09 gaetanmorice

@gaetanmorice I have some clarifying questions for you.

  1. Does this issue happen only when you run aws cloudformation package? If you were to take the same template and manually run it in a stack on the console, does that work?
  2. Could you provide a example we could use to repro this?

If this is only happening when you run the package command, it may be an issue in the awscli.

jfuss avatar Oct 18 '17 02:10 jfuss

Closing due to no response

jfuss avatar Jan 21 '18 01:01 jfuss

@jfuss This is happening to us. We have an account which starts with 0 and we tried to have mappings for specific accounts. Package converts the account number to integer by removing the quotes around it and the deploy complains that mapping has to be a string not integer. Package also seem to be sorting the Map elements.

atharfaraz avatar Oct 17 '19 21:10 atharfaraz

@atharfaraz is this specifically happening at the sam package step?

keetonian avatar Oct 17 '19 21:10 keetonian

@atharfaraz could you provide the following information?

  1. Does this issue happen only when you run aws cloudformation package? If you were to take the same template and manually run it in a stack on the console, does that work?
  2. Could you provide a example we could use to repro this?

If this is only happening when you run the package command, it may be an issue in the awscli.

keetonian avatar Oct 17 '19 21:10 keetonian

1. Does this issue happen only when you run aws cloudformation package? If you were to take the same template and manually run it in a stack on the console, does that work? Ans: Yes, it happens with cloud formation as well.. Same template meaning the packaged template?

2. Could you provide a example we could use to repro this? Ans: Sure, here it is:

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: This template deploys the hi-fi lambda function and associated resources.
Parameters:
  Environment:
    Description: "The environment to create"
    Type: String 
Mappings:
  AmazingMapPerAcc:
    "0123457652": 
        Version: 12
    "23457651011":
        Version: 10
    "1234221768658":
        Version: 8
    "9252719167122":
         Version: 4
Resources:
  paymentMs:
    DependsOn: paymentMsRole
    Type: "AWS::Serverless::Function"
    Properties:
      FunctionName: "dummy-ms"
      Handler: index.handler
      Runtime: nodejs10.x
      CodeUri: ./lambdas/dummy-ms
      Description: ""
      MemorySize: 128
      Timeout: 15

Since I couldn't copy paste my template coz it may contain information which should not be made public, the above is modified version. Please let me know if that helps. If not, I can spend a little more time on getting it identical. Thanks!

atharfaraz avatar Oct 17 '19 22:10 atharfaraz

This is also happening to me on a project im working on. AWS recommends using the account IDs for ALB found below to allow ALB to dump logs to some s3 bucket. the bucket policy is simple enough, but if you use the value for us-east-2, which has a leading zero, it will cause the account id to form an invalid value in scientific notation, thus causing the policy to fail creation.

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#enable-access-logging

concordion2k avatar Nov 14 '19 21:11 concordion2k

THis is also happening to us. Is there any progress/workarounds on this? We have our account ids in a map and referencing accounts that begin with '0' are being converted to scientific notation when using FindInMap.

We are using cloudformation package. It works when we run manually run the template

usa-usa-usa-usa avatar Nov 21 '19 18:11 usa-usa-usa-usa

I found that this happens from the CLI or console for strings:

  • That start with '0' and end with '8' or '9' (see #1135)
  • That exist in Mappings, Parameters, Environment.Variables, and most likely all other areas
  • Happens in SAM templates
  • Happens in vanilla CFN templates (that do not use SAM at all)
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
  FunctionNameParam:
    Type: String
    Default: "somename"
  AccId:
    Type: String
    Default: "012123123128" # converted

Conditions:
  TrueCondition:
    Fn::Equals: ["true", "true"]

Mappings:
  MyMap:
    a: 
      Version: "013566350540"
      Version2: "013566350541"
    b:
      Version: "330105517988"
      Version2: "013566350542"
    c:
      Version: "033010551798" # converted
      Version2: "013566350543"
    d:
      Version: "000999333888" # converted
      Version2: "013566350544"
    e:
      Version: "0121212121212"
      Version2: "013566350545"
    f:
      Version: "0121212121212"
      Version2: "013566350546"
    g:
      Version: "0121212121217"
      Version2: "013566350548" # converted
    h:
      Version: "0221212121219" # converted
      Version2: "033566350548" # converted
    i:
      Version: "0421212121218" # converted
      Version2: "003566350548" # converted

Resources:

  MyFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      InlineCode: |
        print('hi')
      Handler: index.handler
      Runtime: python3.7
      Environment:
        Variables:
          a1: !FindInMap [MyMap, a, Version]
          b1: !FindInMap [MyMap, b, Version]
          c1: !FindInMap [MyMap, c, Version] # converted into a number
          d1: !FindInMap [MyMap, d, Version] # converted into a number
          e1: !FindInMap [MyMap, e, Version]
          f1: !Ref AccId # converted into a number
          g1: "012123123128" # converted into a number
          h1: "012123123121" # not converted to a number

Outputs:
  Number:
    Value: 08 # converted to number, expected
  StringVal:
    Value: "08" # converted to number, not expected

See https://github.com/aws/aws-cli/issues/2934#issuecomment-341468059 for related information

Comments about this in pyyaml: https://github.com/yaml/pyyaml/issues/98

Workarounds:

  • Write your template in JSON if you run into this
  • Use !Join ["", ["0", "12123123128"]] (join the number together in a way that this parsing issue isn't encountered)

keetonian avatar Jan 03 '20 21:01 keetonian

I can confirm seeing the same problem in a slightly different flavor - "12345e67" would be converted to a number in sci notation - "6.2345E71".

dreamiurg avatar Jan 14 '20 19:01 dreamiurg

I confirm this happens with plain 'ole cloudformation template.

citrusoft avatar Jan 16 '20 23:01 citrusoft

Still a bug in 2020. It looks like https://github.com/aws/aws-cli/issues/3991 also covers this issue, and has the best workaround, which is to specify the --use-json flag.

adrian-skybaker avatar Apr 28 '20 10:04 adrian-skybaker

For those arriving here and then trying --use-json, that workaround does not appear to work, at least not in my case (A CFN template using "Transform": "AWS::Serverless-2016-10-31") despite the created JSON template clearly showing the data as strings:

"Globals": {
    "Function": {
        "Runtime": "python3.8",
        "Environment": {
            "Variables": {
                "TEST_ID_1": "0123456789",
                "TEST_ID_2": "1234567890"
            }
        }
    }
},

TEST_ID_1 gets turned into a float, TEST_ID_2 does not.

silvexis avatar Jun 30 '20 16:06 silvexis

Closing as the PR has been merged and released

hawflau avatar Feb 25 '21 18:02 hawflau