I have created a library that allows you to create / update / delete / check the AWS CloudFormation stack from the CUI using the Python command line tool Fabric.
** fabricawscfn ・ ・ ・ Fabric task generator for AWS CloudFormation. **
CloudFormation is often used to manage AWS resources, but GUI operations from the AWS console are laborious and difficult to automate. It can be automated by using APIs such as AWS CLI, but it is also troublesome to write standard scripts one by one.
With fabricawscfn, all you have to do is specify the stack name and template file in the Fabric script (fabfile.py
) and the basic tasks for working with the stack will be automatically generated.
fabfile.py
from fabricawscfn import *
StackGroup('my-cfn-templates', 'example', 'templates')\
.define_stack('foo', 'example-foo', 'foo.yaml')\
.define_stack('bar', 'example-bar', 'bar.yaml', Tags=[{'Key':'example', 'Value':'EXAMPLE'}])\
.generate_task(globals())
With this, Fabric tasks such as stack creation / update / delete / list view / detail view are automatically generated and made available.
The execution result of each task basically looks like the UI of the AWS console is converted to CUI as it is.
$ fab -l
Available commands:
create_bar create stack bar.
create_foo create stack foo.
delete_bar delete stack bar.
delete_foo delete stack foo.
desc_stack Describe existing stack.
list_exports List exports.
list_resources List existing stack resources.
list_stacks List stacks.
params Set parameters. (Applies to all tasks)
sync_templates Synchronize templates local dir to S3 bucket.
update_bar update stack bar.
update_foo update stack foo.
validate_template Validate template on local dir.
Have Python 2.x and pip installed. Python 3.x has not been tested.
Install the AWS CLI (http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html).
Install Fabric.
$ pip install fabric
$ pip install git+https://github.com/crossroad0201/fabric-aws-cloudformation.git
Create a Fabric script fabfile.py
and use fabricawscfn
to define the stack as in the example above.
See the GitHub README (https://github.com/crossroad0201/fabric-aws-cloudformation) for more information.
$ fab list_stacks
Stacks:
+------------+----------------------+-----------------+----------------------------------+-------------+-------------+
| StackAlias | StackName | Status | CreatedTime | UpdatedTime | Description |
+------------+----------------------+-----------------+----------------------------------+-------------+-------------+
| foo | fabricawscfn-dev-foo | CREATE_COMPLETE | 2017-03-05 04:35:12.823000+00:00 | - | Foo bucket. |
| bar | fabricawscfn-dev-bar | Not created | - | - | - |
+------------+----------------------+-----------------+----------------------------------+-------------+-------------+
$ fab desc_stack:foo
Stack:
+----------------------+-----------------+----------------------------------+-------------+-------------+
| StackName | Status | CreatedTime | UpdatedTime | Description |
+----------------------+-----------------+----------------------------------+-------------+-------------+
| fabricawscfn-dev-foo | CREATE_COMPLETE | 2017-03-05 04:35:12.823000+00:00 | None | Foo bucket. |
+----------------------+-----------------+----------------------------------+-------------+-------------+
Parameters:
+---------+--------+
| Key | Value |
+---------+--------+
| Param4 | PARAM4 |
| Param3 | PARAM3 |
| Param2 | PARAM2 |
| Param1 | PARAM1 |
| EnvName | dev |
+---------+--------+
Outputs:
+--------+-----------------+-------------+
| Key | Value | Description |
+--------+-----------------+-------------+
| Bucket | sandbox-dev-foo | Foo bucket. |
+--------+-----------------+-------------+
Events(last 20):
+----------------------------------+--------------------+----------------------------+----------------------+-----------------------------+
| Timestamp | Status | Type | LogicalID | StatusReason |
+----------------------------------+--------------------+----------------------------+----------------------+-----------------------------+
| 2017-03-05 04:35:55.694000+00:00 | CREATE_COMPLETE | AWS::CloudFormation::Stack | fabricawscfn-dev-foo | None |
| 2017-03-05 04:35:53.009000+00:00 | CREATE_COMPLETE | AWS::S3::Bucket | Bucket | None |
| 2017-03-05 04:35:32.308000+00:00 | CREATE_IN_PROGRESS | AWS::S3::Bucket | Bucket | Resource creation Initiated |
| 2017-03-05 04:35:31.102000+00:00 | CREATE_IN_PROGRESS | AWS::S3::Bucket | Bucket | None |
| 2017-03-05 04:35:12.823000+00:00 | CREATE_IN_PROGRESS | AWS::CloudFormation::Stack | fabricawscfn-dev-foo | User Initiated |
+----------------------------------+--------------------+----------------------------+----------------------+-----------------------------+
$ fab create_bar
Creating stack...
Stack Name: fabricawscfn-dev-bar
Template : https://s3.amazonaws.com/crossroad0201-fabricawscfn/example/dev/subdir/bar.yaml
Parameters: [{'ParameterValue': 'dev', 'ParameterKey': 'EnvName'}]
Waiting for complete...
Finish.
It is still rough and if you use it in actual product development, small improvements should come out, so we will improve it from time to time.
Tips to help you use the Python deployment tool Fabric for the first time
[Make a pretty ASCII table with PrettyTable in Python](http://momijiame.tumblr.com/post/44704474054/python-Make a pretty -ascii-table with -pretty table-)