Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(@aws-cdk/core): CfnMapping overview example throws Template format error: Mappings attribute name 'us-east-1' must contain only alphanumeric characters. #16866

Closed
gmedard opened this issue Oct 8, 2021 · 7 comments · Fixed by #16882
Labels
documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.

Comments

@gmedard
Copy link

gmedard commented Oct 8, 2021

link to reference doc page

https://docs.aws.amazon.com/cdk/api/latest/docs/core-readme.html#mappings

Describe your issue?

Attempting to define a region template mapping:

  const regionTable = new cdk.CfnMapping(this, "RegionTable", {
  mapping: {
    regionName: {
      "us-east-1": "value-one",
      "us-west-2": "value-two",
    },
  },
});

However, this is not working as documented. It's throwing this error:
"Template format error: Mappings attribute name 'us-east-1' must contain only alphanumeric characters."

@gmedard gmedard added documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Oct 8, 2021
@jumic
Copy link
Contributor

jumic commented Oct 8, 2021

The CloudFormation documentation describes this behavior:

The name can contain only alphanumeric characters (A-Za-z0-9).

After changing the structure of the mapping, the example can be deployed:

const regionTable = new cdk.CfnMapping(this, 'RegionTable', {
  mapping: {
    'us-east-1': {
      regionName: 'US East (N. Virginia)',
      // ...
    },
    'us-east-2': {
      regionName: 'US East (Ohio)',
      // ...
    },
    // ...
  }
});

regionTable.findInMap(cdk.Aws.REGION, 'regionName')

@gmedard
Copy link
Author

gmedard commented Oct 9, 2021

Thank you for the response @jumic. That fix resulted in this new local error now:

"throw new Error(Mapping doesn't contain top-level key '${key1}');"

@jumic
Copy link
Contributor

jumic commented Oct 9, 2021

In which region are you deploying the stack? Did you maintain the mapping for your region (e.g. eu-west-1) in CfnMapping?

If I'm using my example with value eu-west-1, I get the same error. The log output also shows the specific value that is missing (see last line):

        throw new Error(`Mapping doesn't contain top-level key '${key1}'`);
              ^
Error: Mapping doesn't contain top-level key 'eu-west-1'

If this does not resolve your problem, could you please show me your code again?

@gmedard
Copy link
Author

gmedard commented Oct 12, 2021

I was deploying in us-west-2 region and my code was as follows:

const regionTable = new cdk.CfnMapping(this, 'RegionTable', {
  mapping: {
    'us-east-1': {
      regionName: 'US East (N. Virginia)',
      // ...
    },
    'us-east-2': {
      regionName: 'US East (Ohio)',
      // ...
    },
    // ...
  }
});

regionTable.findInMap(cdk.Aws.REGION, 'regionName')

However, I have since figured out the way CDK will not make a fuss if I want to map the regions as the top key. The code that worked for me is below:

const regionMap = new cdk.CfnMapping(this, "RegionMap", {
  mapping: {
    ["us-east-1"]: {
      AMI: "ami-1234567e8f9example",
      SUBNETID: "subnet-ffffffff",
      SG: "sg-d9d9d9d9",
    },
    ["us-west-2"]: {
      AMI: "ami-1234567e6749example",
      SUBNETID: "subnet-eeeeeeee",
      SG: "sg-012345d7f90,
    },
  },
});

The synthesized mappings section of the template from above code is:

Mappings:
  RegionMap:
    us-east-1:
      AMI: ami-1234567e8f9example
      SUBNETID: subnet-ffffffff
      SG: sg-d9d9d9d9
    us-west-2:
      AMI: ami-1234567e6749example
      SUBNETID: subnet-eeeeeeee
      SG: sg-012345d7f90

The brackets seem to have solved the non alphanumeric original error.

@jumic
Copy link
Contributor

jumic commented Oct 12, 2021

In my example, I maintained the mapping only for regions us-east-1 and us-east-2. If you deploy it to region us-west-2, you have to maintain this value too. I think this was the reason for the error.

One additional question: Does your code work without squared brackets, too? I copied into my example project and I could deploy it without squared brackets.

I have created a pull request which updates the documentation. Therefore, it would be great to know if the squared brackets are mandatory in some cases.

@gmedard
Copy link
Author

gmedard commented Oct 13, 2021

I had not tried without the brackets. I just deployed without them, and it worked. So it works with or without. Thanks much for the help on getting the documentation fixed.

@mergify mergify bot closed this as completed in #16882 Oct 22, 2021
mergify bot pushed a commit that referenced this issue Oct 22, 2021
The CloudFormation intrinsic function `Fn::FindInMap` only supports alphanumeric characters as name. However, the `CfnMapping` examples in the README file contain hyphens in the name field. This causes an error when the code is deployed.

I changed the structure of the examples to create examples that can be deployed in AWS.

Fixes #16866.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
The CloudFormation intrinsic function `Fn::FindInMap` only supports alphanumeric characters as name. However, the `CfnMapping` examples in the README file contain hyphens in the name field. This causes an error when the code is deployed.

I changed the structure of the examples to create examples that can be deployed in AWS.

Fixes aws#16866.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants