Rich C
Rich C

Reputation: 804

How to dynamically generate key names in Cloudformation template?

I would like to dynamically set the path of the sql file in the code below using parameters.

files: 
  /tmp/setup.mysql: 
    content: !Sub |
      CREATE DATABASE ${DBName};
      CREATE USER '${DBUsername}'@'localhost' IDENTIFIED BY '${DBPassword}';
      GRANT ALL ON ${DBName}.* TO '${DBUsername}'@'localhost';
      FLUSH PRIVILEGES;
    mode: "000644"
    owner: "root"
    group: "root"

I have tried simplistically adding a Reference to a custom Path parameters but, unfortunately, I am getting the following error from the Template Designer.

Template contains errors.: Template format error: [/Resources/MyLaunchConfig/Metadata/AWS::CloudFormation::Init/config/files] map keys must be strings; received a map instead

How can I move a path like this into the template parameters?

Edit:

The Note on the CloudFormation documentation on Intrinsic functions says the following:

You can use intrinsic functions only in specific parts of a template. Currently, you can use intrinsic functions in resource properties, outputs, metadata attributes, and update policy attributes. You can also use intrinsic functions to conditionally create stack resources.

This seems to indicate that the functions should be available in resource properties and metadata attributes. It does not specifically restrict it to the values in these objects rather than the keys (although this could be assumed by the inherent nature of keys vs values).

Upvotes: 3

Views: 1728

Answers (1)

Marcin
Marcin

Reputation: 239000

Based on the comments.

The issue is that according to the CFN format specification, a map can only be string:

A map is a collection of key-value pairs, where the keys are always strings.

Therefore, a proposed solution was to use UserData to define the dynamically generate file, instead of using CFN metadata.

Upvotes: 1

Related Questions