Skip to content

Modules

A module is a template file that another template includes. cfnrain inserts the module's resources into the parent template when it packages it. Modules let you split a large template into files and reuse a group of resources in many stacks.

Modules are a client-side feature. CloudFormation only sees the packaged template, so run cfnrain pkg to see the result. Run linters such as cfn-lint on the packaged template, because they do not know the Modules section.

Example

The module, bucket-module.yaml:

Parameters:
  Name:
    Type: String

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref Name
      VersioningConfiguration:
        Status: Enabled

Outputs:
  Arn:
    Value: !GetAtt Bucket.Arn

The parent template:

Modules:
  Logs:
    Source: ./bucket-module.yaml
    Properties:
      Name: billing-logs
    Overrides:
      Bucket:
        DeletionPolicy: Retain
        Properties:
          VersioningConfiguration:
            Status: Suspended

Outputs:
  LogsArn:
    Value: !GetAtt Logs.Arn

The packaged template:

Resources:
  LogsBucket:
    DeletionPolicy: Retain
    Type: AWS::S3::Bucket
    Properties:
      BucketName: billing-logs
      VersioningConfiguration:
        Status: Suspended

Outputs:
  LogsArn:
    Value: !GetAtt LogsBucket.Arn

The Modules section

Each entry in Modules has a name and a configuration:

Key What it does
Source Where the module file is. See Sources.
Properties Values for the module's Parameters
Overrides Changes to the module's resources. See Overrides.
ForEach Make one copy of the module for each item in a list. See ForEach.

Experimental status

The Modules section is not experimental and needs no flag. Only the older !Rain::Module directive needs --experimental. See Experimental features.

The feature is still young. The format can change in a minor release.

Next steps