Skip to content

!Rain::Embed

!Rain::Embed <path> reads a file and inserts its contents into the template as a string. cfnrain removes the whitespace at the start and end of the file. The path must be a file, not a directory.

Use it for inline Lambda code, scripts, policies in a text format, and other text that is easier to edit in its own file.

Example

The template:

Resources:
  Function:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: python3.13
      Handler: index.main
      Role: !GetAtt FunctionRole.Arn
      Code:
        ZipFile: !Rain::Embed handler.py

handler.py, in the same directory:

import json


def main(event, context):
    return {"statusCode": 200, "body": json.dumps({"ok": True})}

The packaged template:

Resources:
  Function:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: python3.13
      Handler: index.main
      Role: !GetAtt FunctionRole.Arn
      Code:
        ZipFile: |-
          import json


          def main(event, context):
              return {"statusCode": 200, "body": json.dumps({"ok": True})}
  • !Rain::Include parses the file as YAML or JSON and inserts an object, not a string.
  • Fn::InsertFile also inserts a file as a string. Unlike !Rain::Embed, it keeps the whitespace at the end.