The missing CloudFormation tool
StackMaster is a CLI tool to manage CloudFormation stacks, with the following features:
Stack updates can cause a lot of damage if applied blindly. StackMaster helps with this by providing the operator with as much information about the proposed change as possible before asking for confirmation to continue. That information includes:
Stack parameters can be dynamically resolved at runtime using one of the built in parameter resolvers. Parameters can be sourced from other stacks outputs, or by querying various AWS APIs to get resource ARNs, etc.
gem install stack_master
# if you want linting capabilities:
pip install cfn-lintpip install cfn-lint if you need lint functionalitygem 'stack_master' to your Gemfile.bundle installbundle exec stack_master init to generate a directory structure and stack_master.yml fileStacks are defined inside a stack_master.yml YAML file. When running
stack_master, it is assumed that this file will exist in the current working
directory, or that the file is passed in with --config /path/to/stack_master.yml. Here's an example configuration file:
…StackMaster can optionally use S3 to store the templates before creating a stack. This requires you to configure an S3 bucket in stack_master.yml:
stack_defaults:
s3:
bucket: my_bucket_name
prefix: cfn_templates/my-awesome-app
region: us-west-2Additional files can be configured to be uploaded to S3 alongside the templates:
stacks:
production:
myapp-vpc:
template: myapp_vpc.rb
files:
- userdata.shtemplates - CloudFormation, SparkleFormation or CfnDsl templates.parameters - Parameters as YAML files.secrets - encrypted secret files.policies - Stack policy JSON files.StackMaster supports CloudFormation templates in plain JSON or YAML. Any .yml or .yaml file will be processed as
YAML, while any .json file will be processed as JSON. Additionally, YAML files can be pre-processed using ERB and
compile-time parameters.
By default, any template ending with .rb will be processed as a SparkleFormation
template. However, if you want to use CfnDsl templates you can add
the following lines to your stack_master.yml.
template_compilers:
rb: cfndslBy default, parameters are loaded from multiple YAML files, merged from the following lookup paths from bottom to top:
A simple parameter file could look like this:
key_name: myapp-us-east-1Alternatively, a parameter_files array can be defined to explicitly list
parameter files that will be loaded. If parameter_files are defined, the
automatic search locations will not be used.
parameters_dir: parameters # the default
stacks:
us-east-1:
my-app:
parameter_files:
- my-app.yml # parameters/my-app.ymlParameters can also be defined inline with stack definitions:
stacks:
us-east-1:
my-app:
parameters:
VpcId:
stack_output: my-vpc/VpcIdCompile time parameters can be defined in a stack's parameters file, using the key compile_time_parameters. Keys in
parameter files are automatically converted to camel case.
As an example:
# parameters/some_stack.yml
vpc_cidr: 10.0.0.0/16
compile_time_parameters:
subnet_cidrs:
- 10.0.0.0/28
- 10.0.2.0/28Compile time parameters can be used for SparkleFormation templates. It conforms and allows you to use the Compile Time Parameters feature.
Compile time parameters can be used to pre-process YAML CloudFormation templates. An example template:
# templates/some_stack_template.yml.erb
Parameters:
VpcCidr:
Type: String
Resources:
Vpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
# Given the two subnet_cidrs parameters, this creates two resources:
# SubnetPrivate0 with a CidrBlock of 10.0.0.0/28, and
# SubnetPrivate1 with a CidrBlock of 10.0.2.0/28
SubnetPrivate:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref Vpc
AvailabilityZone: ap-southeast-2
CidrBlock:
Parameter values can be sourced dynamically using parameter resolvers.
One benefit of using parameter resolvers instead of hard coding values like VPC IDs and resource ARNs is that the same configuration works cross region/account, even though the resolved values will be different.
One way to resolve parameter values from different accounts to the one StackMaster runs in, is to assume a role in another account with the relevant IAM permissions to execute successfully.
This is supported in StackMaster by specifying the role and account properties for the
parameter resolver in the stack's parameters file.
All parameter resolvers are supported.
…An example of use case where cross-account parameter resolving is particularly useful is when setting up VPC peering where you need the VPC ID of the peer. Without the ability to assume a role in another account, the only option was to hard code the peer's VPC ID.
The stack output parameter resolver looks up outputs from other stacks in the
same or different region. The expected format is [(region|region-alias):]stack-name/(OutputName|output_name).
vpc_id:
# Output from a stack in the same region
stack_output: my-vpc-stack/VpcId
bucket_name:
# Output from a stack in a different region
stack_output: us-east-1:init-bucket/bucket_name
zone_name:
# Output from a stack in a different region using its alias
stack_output: global:hosted-zone/ZoneNameThis is the most used parameter resolver because it enables stacks to be split up into their separated concerns (VPC, web, database etc) with outputs feeding into parameters of dependent stacks.
Note: The GPG parameter resolver has been extracted into a dedicated gem. Please install and follow the instructions for the stack_master-gpg_parameter_resolver gem.
An alternative to the secrets store, uses the AWS SSM Parameter store to protect
secrets. Expects a parameter of either String or SecureString type to be present in the
same region as the stack. You can store the parameter using a command like this
aws ssm put-parameter --region --name --value --type (String|SecureString)
When doing so make sure you don't accidentally store the secret in your .bash_history and
you will likely want to set the parameter to NoEcho in your template.
db_password:
parameter_store: ssm_parameter_nameAn alternative to the secrets store is accessing 1password secrets using the 1password cli (op).
You declare a 1password lookup with the following parameters in your parameters file:
# parameters/database.yml
database_password:
one_password:
title: production database
vault: Shared
type: password1password stores the name of the secret in the title. You can pass the vault you expect the secret to be in.
Currently we support two types of secrets, passwords and secureNotes. All values must be declared, there are no defaults.
For more information on 1password cli please see here
ejson is a tool to manage asymmetrically encrypted values in JSON format.
This allows you to keep secrets securely in git/Github and gives anyone the ability the capability to add new
secrets without requiring access to the private key. ejson_wrapper
encrypts the underlying EJSON private key with KMS and stores it in the ejson file as _private_key_enc. Each
time an ejson secret is required, the underlying EJSON private key is first decrypted before passing it onto
ejson to decrypt the file.
First, generate an ejson file with ejson_wrapper, specifying the KMS key ID to be used:
gem install ejson_wrapper
ejson_wrapper generate --region us-east-1 --kms-key-id [key_id] --file secrets/production.ejsonThen, add the ejson_file argument to your stack in stack_master.yml:
stacks:
us-east-1:
my_app:
template: my_app.json
ejson_file: production.ejsonfinally refer to the secret key in the parameter file, i.e. parameters/my_app.yml:
my_param:
ejson: "my_secret"Additional configuration options:
ejson_file_region The AWS region to attempt to decrypt private key withejson_file_kms Default: true. Set to false to use ejson without KMS.Looks up a security group by name and returns the ARN.
ssh_sg:
security_group: SSHSecurityGroupAn array of security group names can also be provided.
ssh_sg:
security_groups:
- SSHSecurityGroup
- WebAccessSecurityGroupLooks up AWS Identity Center group name in the configured Identity Store and returns the ID suitable for use in AWS IIC assignments. It is likely that account and role will need to be specified to do the lookup, the region specification is optional it defaults to stack region.
GroupId:
sso_group_id: '[region:]identity-store-id/SSO Group Name'e.g.
GroupIdNotInStackRegion:
sso_group_id: 'us-east-1:d-123456df8:Okta-App-AWS-FooBar'
GroupIdInStackRegion:
sso_group_id: 'd-123456df8:Okta-App-AWS-FooBar'Looks up an SNS topic by name and returns the ARN.
notification_topic:
sns_topic_name: PagerDutyLooks up the latest AMI ID by a given set of tags.
web_ami:
latest_ami_by_tags: role=web,application=myappNote that the corresponding array resolver is named latest_amis_by_tags.
Looks up the latest AMI ID by a given set of attributes. By default it will only return AMIs from the account the stack is created in, but you can specify the account ID or certain keywords mentioned in the aws documentation
This selects the latest wily hvm AMI from Ubuntu (using the account id):
bastion_ami:
latest_ami:
owners: 099720109477
filters:
name: ubuntu/images/hvm/ubuntu-wily-15.10-amd64-server-*A set of possible attributes is available in the AWS documentation.
Any
No open issues yet, or sync has not completed.