Class: AwsCftTools::Template

Inherits:
Object
  • Object
show all
Includes:
FileSystem, Metadata, Properties
Defined in:
lib/aws_cft_tools/template.rb,
lib/aws_cft_tools/template/metadata.rb,
lib/aws_cft_tools/template/properties.rb,
lib/aws_cft_tools/template/dsl_context.rb,
lib/aws_cft_tools/template/file_system.rb

Overview

TODO:

Fetch templates from deployed stacks and consider them in the dependency tree for removing or deploying templates in the repo. Flag stacks with no local source to not be updated on deployment.

TODO:

Add ability to init to fetch templates from stacks and put them in files.

Note:

Stacks should be removed (“retracted”) from AWS before they are removed from the set of templates. Otherwise, they won’t be considered in the set of available templates or stacks.

The AwsCftTools::Template class wraps a CloudFormation template source to provide support for various operations by the toolset.

CloudFormation Templates

As much as possible, this tool uses CloudFormation templates as-is and makes as many inferences as reasonable. However, some things aren’t captured in stock template information.

Allowed Environments

The environments in which a template should be deployed is provided by the AllowedValues or the AllowedPattern key of the Environment template parameter.

Allowed Regions

A template can be pinned to a particular region or set of regions by providing a list of values for the Region key in the template metadata. If no such key is present, then the template can be deployed or otherwise used in all regions.

Explicit Template Dependencies

As much as possible, dependencies between templates are inferred based on exported and imported values. However, some templates might depend on another template in a way that isn’t captured by these values. For those dependencies, the templates that should be run first can be listed under the DependesOn.Templates metadata key.

Template Parameters

Rather than require mappings in templates to hold environment-specific values, a template has a corresponding parameters file that holds the value for the stack parameter for each environment. This parameters file is in YAML format and passed through ERB before parsing, so it can incorporate environment variables and other logic into specifying parameter values.

Examples:

Allowed Environments (explicit list)

---
Parameters:
  Environment:
    AllowedValues:
      - QA
      - Staging
      - Production

Allowed Environments (implicit pattern)

---
Parameters:
  Environment:
    AllowedPattern: ^(QA|Staging|Production|Dev-.+)$

Allowed Regions

---
Metadata:
  Region:
    - us-east-1
    - us-west-1

Explicit Template Dependency

---
Metadata:
  DependsOn:
    Templates:
      - network/peering.yaml

Defined Under Namespace

Modules: DSLContext, FileSystem, Metadata, Properties

Constant Summary

Constants included from Metadata

Metadata::CONTENT_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Properties

#allowed_environments, #allowed_environments_regex, #allowed_regions, #default_parameters, #environment?, #inputs, #outputs, #region?, #role, #role?, #template_dependencies

Methods included from Metadata

#name, #parameters, #template, #template?, #template_source_for_aws, #template_type

Methods included from FileSystem

#parameter_file, #parameters_source, #template_file, #template_source

Constructor Details

#initialize(filename, options = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

  • filename (String)

    path to template relative to the template_dir path

  • options (Hash) (defaults to: {})

    runbook options

Options Hash (options):

  • :environment (String)

    environment in which parameters should be fetched

  • :parameter_dir (String)

    directory relative to the root path in which parameter files are found

  • :root (Pathname)

    path to the root of the project

  • :template_dir (String)

    directory relative to the root path in which template sources are found



101
102
103
104
# File 'lib/aws_cft_tools/template.rb', line 101

def initialize(filename, options = {})
  @options = options
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



80
81
82
# File 'lib/aws_cft_tools/template.rb', line 80

def filename
  @filename
end

Instance Method Details

#stack_parametersHash

Returns parameters to provide to the AWS client to deploy the template.

Returns:

  • (Hash)

    parameters to provide to the AWS client to deploy the template



119
120
121
122
123
124
125
126
# File 'lib/aws_cft_tools/template.rb', line 119

def stack_parameters
  {
    stack_name: name,
    template_body: template_source_for_aws,
    parameters: hash_to_param_list(parameters || {}),
    tags: tags
  }
end

#tagsArray<Hash>

Returns template tags suitable for use in deploying a stack.

Returns:

  • (Array<Hash>)

    template tags suitable for use in deploying a stack



109
110
111
112
113
114
# File 'lib/aws_cft_tools/template.rb', line 109

def tags
  [
    { key: 'Environment', value: @options[:environment] },
    { key: 'Source', value: ('/' + filename.to_s).gsub(%r{/+}, '/') }
  ] + role_tag
end