runway.cfngin.lookups.handlers.file module

File lookup.

class runway.cfngin.lookups.handlers.file.FileLookup[source]

Bases: runway.lookups.handlers.base.LookupHandler

File lookup.

classmethod handle(value, context=None, provider=None, **kwargs)[source]

Translate a filename into the file contents.

Parameters

Fields should use the following format:

<codec>:<path>

Example:

# We've written a file to /some/path:
$ echo "hello there" > /some/path

# With CFNgin we would reference the contents of this file with the
# following
conf_key: ${file plain:file://some/path}

# The above would resolve to
conf_key: hello there

# Or, if we used wanted a base64 encoded copy of the file data
conf_key: ${file base64:file://some/path}

# The above would resolve to
conf_key: aGVsbG8gdGhlcmUK

Supported codecs:

plain

Plain Text

base64

Encode the plain text file at the given path with base64 prior to returning it.

parameterized

The same as plain, but additionally supports referencing template parameters to create userdata that’s supplemented with information from the template, as is commonly needed in EC2 UserData. For example, given a template parameter of BucketName, the file could contain the following text:

#!/bin/sh
aws s3 sync s3://{{BucketName}}/somepath /somepath

Then you could use something like this in the YAML config file:

UserData: ${file parameterized:/path/to/file}

Resulting in the UserData parameter being defined as:

{ "Fn::Join" : ["", [
    "#!/bin/sh\\naws s3 sync s3://",
    {"Ref" : "BucketName"},
    "/somepath /somepath"
]] }
parameterized-b64

The same as parameterized, with the results additionally wrapped in { "Fn::Base64": ... } , which is what you actually need for EC2 UserData.

When using parameterized-b64 for UserData, you should use a variable defined as such:

from troposphere import AWSHelperFn

"UserData": {
    "type": AWSHelperFn,
    "description": "Instance user data",
    "default": Ref("AWS::NoValue")
}

Then assign UserData in a LaunchConfiguration or Instance to self.get_variables()["UserData"]. Note that we use AWSHelperFn as the type because the parameterized-b64 codec returns either a Base64 or a GenericHelperFn troposphere object.

json

Decode the file as JSON and return the resulting object

json-parameterized

Same as json, but applying templating rules from parameterized to every object value. Note that object keys are not modified. Example (an external PolicyDocument):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "some:Action"
            ],
            "Resource": "{{MyResource}}"
        }
    ]
}
yaml

Decode the file as YAML and return the resulting object. All strings are returned as unicode even in Python 2.

yaml-parameterized

Same as json-parameterized, but using YAML. Example:

Version: 2012-10-17
Statement:
  - Effect: Allow
    Action:
      - "some:Action"
    Resource: "{{MyResource}}"
runway.cfngin.lookups.handlers.file.parameterized_codec(raw, b64)[source]

Parameterize a string, possibly encoding it as Base64 afterwards.

Parameters
  • raw (Union[bytes, str]) – String to be processed. Byte strings will be interpreted as UTF-8.

  • b64 (bool) – Whether to wrap the output in a Base64 CloudFormation call.

Returns

Output to be included in a CloudFormation template.

Return type

troposphere.AWSHelperFn

class runway.cfngin.lookups.handlers.file.SafeUnicodeLoader(stream)[source]

Bases: yaml.loader.SafeLoader

Safe unicode loader.

Initialize the scanner.

construct_yaml_str(node)[source]

Construct yaml str.

runway.cfngin.lookups.handlers.file.yaml_codec(raw, parameterized=False)[source]

YAML codec.

runway.cfngin.lookups.handlers.file.json_codec(raw, parameterized=False)[source]

JSON codec.