I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)

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.

setup

Installation of required software

$ pip install fabric

Installation of fabricawscfn

$ pip install git+https://github.com/crossroad0201/fabric-aws-cloudformation.git

Example of use

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.

Check the list of stacks

$ 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   |                -                 |      -      | -           |
+------------+----------------------+-----------------+----------------------------------+-------------+-------------+

Check stack details

$ 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              |
+----------------------------------+--------------------+----------------------------+----------------------+-----------------------------+

Create a stack

$ 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.

in conclusion

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.

The site that I used as a reference

Related Links

Recommended Posts

I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
I made a python library to do rolling rank
I made a library to easily read config files with Python
I made a library that adds docstring to a Python stub file.
I made a library to separate Japanese sentences nicely
I made a Python module to translate comment outs
I wanted to use the Python library from MATLAB
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I made a python text
I made a package to filter time series with python
How to launch AWS Batch from a python client app
I want to start a lot of processes from python
I made a tool to create a word cloud from wikipedia
I want to send a message from Python to LINE Bot
I made you to execute a command from a web browser
I made a server with Python socket and ssl and tried to access it from a browser
I made a Line-bot using Python!
I made my own Python library
I made a fortune with Python.
I made a daemon with Python
A story that I was addicted to calling Lambda from AWS Lambda.
I created a Python library to call the LINE WORKS API
I want to make a parameter list from CloudFormation code (yaml)
I made a Python wrapper library for docomo image recognition API.
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
I made a plugin to generate Markdown table from csv in Vim
I made a new AWS S3 bucket
[Python] I made a decorator that doesn't seem to have any use.
I made Othello to teach Python3 to children (4)
I made a payroll program in Python!
I made a web application in Python that converts Markdown to HTML
I made a character counter with Python
A story I was addicted to when inserting from Python to a PostgreSQL table
Send a message from Python to Slack
I made Othello to teach Python3 to children (2)
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I want to use jar from python
I want to build a Python environment
I want to play with aws with python
I made Othello to teach Python3 to children (5)
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a script in python to convert .md files to Scrapbox format
I made a script to display emoji
I made a Hex map with Python
I made a library konoha that switches the tokenizer to a nice feeling
I made a program to check the size of a file in Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a neuron simulator with Python
I made a function to see the movement of a two-dimensional array (Python)
I made a Line Bot that uses Python to retrieve unread Gmail emails!
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I made a Docker image that can call FBX SDK Python from Node.js
I made a module in C language to filter images loaded by Python
I made a tool to generate Markdown from the exported Scrapbox JSON file
[Python] I tried to get the type name as a string from the type function