Rain CLI
repository·main·Indexed 21 days ago
https://github.com/aws-cloudformation/rainA command line tool for working with AWS CloudFormation templates and stacks. Rain provides interactive deployments, template formatting, generative AI-powered template building, and experimental features like Cloud Control API (CCAPI) deployments and deployment failure prediction via the `rain forecast` command. Note: This project is deprecated and no longer actively maintained.
What's inside rain
- Rain is a command line tool designed for working with AWS CloudFormation templates and stacks. It provides a suite of commands to manage the lifecycle of CloudFormation resources, including building, deploying, inspecting, and deleting stacks.
Understand CCAPI state management and drift
mainBecause CCAPI deployments do not use CloudFormation stacks, Rain maintains a local state file in the
rain-artifactsbucket to track resources. This allows for features like drift detection and manual state remediation.State File Structure
The state file is a YAML file mirroring your CloudFormation template with an added
Statesection:State: LastWriteTime: ... ResourceModels: MyResource: Identifier: Model: ...Drift Detection
Drift detection compares the actual properties of the deployed AWS resources against the properties stored in the Rain state file. When you run
rain cc deploy, Rain also checks for drift and allows you to resolve it before proceeding with updates. This is useful for remediating complex drift situations or unexpected deployment failures.How Rain modules work
mainRain modules provide multi-file support for CloudFormation, allowing you to compose templates from reusable components. Modules are defined in a
Modulessection and can be local files or HTTPS URLs.Key Concepts
- Composition: Modules support
Parameters,Resources,Conditions, andOutputs. They can be nested, though circular dependencies are prohibited. - Parameters & Overrides: You pass values to modules via
Properties. TheOverridesattribute allows you to modify the internal structure of a module (e.g., changing a resource's metadata) if you know its internal logical IDs. - Constants: You can define a
Constantssection in both modules and parent templates. Constants are processed before modules and can be referenced usingConst::namein!Substrings or!Ref Const::namefor objects. - Looping: Modules support a shorthand
ForEachattribute in theModulesconfiguration, which uses$Identifierand$Indexvariables to iterate over a list. - Module Outputs: Module outputs can be accessed in the parent template using
!GetAtt <ModuleName>.<OutputName>or!Ref <ModuleName>. - Conditional Visibility: Inside a module, you can use
IfParamorIfNotParamin theMetadatasection to show or hide resources based on whether a specific parameter is set in the parent template.
Note: To use modules, you must use the
--experimentalflag withrain pkg.# Example of importing a module with Overrides and Outputs Modules: Content: Source: ./module.yaml Properties: Name: foo Overrides: Bucket: Metadata: OverrideMe: def Outputs: TheArn: Value: !GetAtt Content.BucketArn- Composition: Modules support
Use Rain Packages for collections of modules
mainIf you have a collection of modules bundled in a zip file, you can reference them using a
Packagessection in your template. This allows you to manage multiple modules under a single alias.- Define the package in the
Packagessection using a local path or URL. - Reference modules from that package using the
$prefix followed by the package alias.
Packages: abc: Source: ./package.zip def: Source: https://example.com/packages/package.zip Modules: Foo: Source: $abc/foo.yaml Bar: Source: $def/a/b/bar.yaml- Define the package in the
Use the Cloud Control API with rain cc
mainThe
rain cccommand allows you to interact with templates using the AWS Cloud Control API instead of the standard CloudFormation API.Warning: This feature is experimental and likely to be unstable. You must pass the
--experimental(or-x) flag to use anyccsubcommands.Use Rain directives in templates
mainWhen using
rain pkg, you can use special directives within your CloudFormation templates to dynamically inject content:!Rain::Embed <path>: Embeds the file contents at<path>as a string.!Rain::Include <path>: Reads the file at<path>as YAML/JSON and inserts the resulting object into the template.!Rain::Env <name>: Reads the environment variable<name>and inserts its value as a string.!Rain::S3Http <path>: Uploads<path>(zipping if it's a directory) to S3 and embeds the S3 HTTP URL as a string.!Rain::S3 <path>: Uploads<path>(zipping if it's a directory) to S3 and embeds the S3 URI as a string.!Rain::S3 <object>: Uploads an object to S3 with advanced configuration. Use this to return an object containing metadata instead of just a string. Supported properties:Path: File or directory to upload.Zip:true|false(forces zipping even if it's a single file).BucketProperty:<bucket>(name of the property in the resulting object for the bucket).KeyProperty:<key>(name of the property in the resulting object for the key).Format:Uri|Http(defaults toUri). Note: Do not useFormatifBucketPropertyorKeyPropertyare provided.
!Rain::Module <url>: (Experimental) Supplies a URL to a rain module. The module must contain a resource namedModuleExtensionwith aMetadataentryExtendsspecifying the type to extend. Requires the--experimentalflag.
Manage CloudFormation StackSets with rain stackset
mainThe
rain stacksetcommand serves as a parent command for manipulating AWS CloudFormation StackSets. It does not perform any actions on its own; you must append a specific sub-command to perform a task.Available sub-commands include:
rain stackset deploy: Deploy a CloudFormation stack set from a local template.rain stackset ls: List CloudFormation stack sets in a given region.rain stackset rm: Delete a CloudFormation stack set and/or its instances.
Use constants in CloudFormation templates with Rain
mainYou can define reusable values in a top-level
Rainsection within your template. This allows you to centralize strings or objects and reference them throughout your resources, reducing duplication and making templates easier to maintain.Referencing Constants
- Direct Reference: Use the
!Rain::Constantdirective to inject a constant value directly into a property. - Inside
!Substrings: Use the${Rain::ConstantName}pseudo-parameter syntax to embed constants within a!Subfunction. This works for both simple strings and constants that themselves contain other constants.
Lifecycle
- The
Rainsection is a build-time construct. When you runrain pkg, theRainsection is removed, and all references are replaced with their actual values, resulting in a standard CloudFormation template.
Example
Defining constants in a
Rainblock and using them via both!Rain::Constantand!Sub:Parameters: Prefix: Type: String Rain: Constants: Test1: ezbeard-rain-test-constants Test2: !Sub ${Prefix}-${Rain::Test1}-SubTest Resources: Bucket: Type: AWS::S3::Bucket Properties: BucketName: !Rain::Constant Test1 Bucket2: Type: AWS::S3::Bucket Properties: BucketName: !Rain::Constant Test2 Bucket3: Type: AWS::S3::Bucket Properties: BucketName: !Sub "pre-${Prefix}-${Rain::Test1}-suffix"- Direct Reference: Use the
Deploy a template using the Cloud Control API with `rain cc deploy`
mainThe
rain cc deploycommand allows you to create or update resources directly using the Cloud Control API from a local template file.Warning: This feature is marked as Experimental. You must explicitly acknowledge this by passing the
--experimentalor-xflag. Because it is experimental, the behavior may be unstable.To use this command, provide the path to your template file and a name for the deployment.
rain cc deploy <template> <name> # Example usage with experimental flag: rain cc deploy my-template.yaml my-stack-name -xPublish Rain modules to CodeArtifact
mainUse the
rain module publishcommand to upload a directory containing Rain modules to an AWS CodeArtifact repository. This allows you to share and version your modules centrally.To publish a module, specify the module name and use flags to define the version, the local path of the module files, and the target AWS CodeArtifact repository and domain.
rain module publish <name> [flags]Watch the progress of a CloudFormation stack with rain watch
mainThe
rain watchcommand provides a real-time, updating view of a CloudFormation stack's status. This is particularly useful for monitoring the progress of a deployment that was initiated outside of Rain (e.g., via the AWS CLI or Console) to see updates as they happen.rain watch <stack>Deploy a CloudFormation stack set with rain stackset deploy
mainUse the
rain stackset deploycommand to create or update a CloudFormation stack set from a local template file.Key Behaviors:
- Stack Set Naming: If you don't specify a stack set name,
rainuses the template filename (minus its extension). - Instance Management: If no template file is provided,
rainassumes you want to add a new instance to an existing stack set. - Automatic Packaging: If the template requires packaging (e.g., contains local assets),
rainwill package it first. - Artifact Storage:
rainattempts to create an S3 bucket for packaged artifacts using the formatrain-artifacts-<AWS account id>-<AWS region>unless a specific bucket is provided via--s3-bucket. - Configuration Precedence:
- Account and region flags OVERRIDE values found in configuration files.
- Tags and parameters from configuration files are MERGED with CLI flag values.
rain stackset deploy <template> [stackset] [flags]- Stack Set Naming: If you don't specify a stack set name,