项目作者: SparkFund

项目描述 :
Using infrastructure-as-data to demystify CloudFormation operations
高级语言: Clojure
项目地址: git://github.com/SparkFund/tools-aws.git
创建时间: 2020-05-26T14:15:52Z
项目社区:https://github.com/SparkFund/tools-aws

开源协议:Apache License 2.0

下载


tools-aws

Clojars Project
CircleCI

Using infrastructure-as-data to demystify CloudFormation operations.

Inspired by Terraform and driven by a need for guardrails,
friendliness, and interactivity, this project outlines how Sparkfund
does CloudFormation operations.

Although the source code for this library is now open source, it is
offered as a demonstration, not as a promise of continued development
or support.

Click the Clojars badge above to see available versions and to get
Lein/Boot/deps.edn information.

Usage

Models

  1. (def model (model/build-model spec-file constants-file ifns-file))
  2. model
  3. ;; => {:stacks ... :constants ...}

Example spec.edn file:

  1. {:structure
  2. {:account #{"sandbox" "prod"}
  3. :env {"sandcastle" {:account "sandbox"}
  4. "qa" {:account "sandbox"}
  5. "prod" {:account "prod"}}
  6. :microservice #{"aaa" "bbb"}
  7. :db #{"db"}}
  8. :order
  9. [:account :env :microservice]
  10. :stacks
  11. [[:template "templates/account.edn"
  12. :once-per [:account]
  13. :name ["account"]]
  14. [:template "templates/environment.edn"
  15. :once-per [:account :env]
  16. :name ["environment"]]
  17. [:template "templates/database.edn"
  18. :once-per [:account :env :db]
  19. :name [:env :db]]
  20. [:template "templates/microservice.edn"
  21. :once-per [:account :env :microservice]
  22. :name [:microservice :env]
  23. :params {"Name" :microservice}]
  24. ]}

The only required keyword is :account, as that is special-cased in
several places.

Example constants.edn file:

  1. [{true
  2. {"ClojureVersion" "1.10.0442"}}
  3. {:account
  4. {"prod"
  5. {:id "123"
  6. :name "prod"}
  7. "sandbox"
  8. {:id "456"
  9. :name "sandbox"}}}
  10. {:env
  11. {"sandcastle"
  12. {"Env" "sandcastle"
  13. "EC2InstanceClass" "t3.small"
  14. "SlackAlarmChannel" "#dev-null"}
  15. "qa"
  16. {"Env" "qa"
  17. "EC2InstanceClass" "t3.medium"
  18. "SlackAlarmChannel" "#dev-staging-alerts"}
  19. "prod"
  20. {"Env" "prod"
  21. "EC2InstanceClass" "t3.large"
  22. "SlackAlarmChannel" "#dev-prod-alerts"}}}
  23. ]

The [:account :id] and [:account :name] paths are optional to
provide shorthands for ifns, where :id is the actual Account ID and
:name is a consistent string shorthand for the account.

Example ifns.edn file:

  1. {iam-policy sparkfund.aws.cfn.ifns/iam-policy
  2. assume-roles-policy sparkfund.aws.cfn.ifns/assume-roles-policy
  3. }

The map values should be eval-able in the environment where the
model is built.

See sparkfund.aws.cfn.ifns for
the full list of built-in ifn support.

For example, in your EDN templates you can write:

  1. (join "," ["abc" "xyz"])
  2. ;; =>
  3. {"Fn::Join" ["," ["abc" "xyz"]]}
  4. ;; =>
  5. ;; JSON

Ensure Stack

Once you’ve defined the model, you can use model/filter-stacks,
model/print-stacks, cfn/ensure-stacks! and other
sparkfund.cli helpers in
any command-line scripts that need to update CloudFormation stacks.

For example:

  1. (let [filters {:name "aaa-sandcastle"}
  2. stacks (model/filter-stacks (:stacks model) filters)]
  3. (println "Found" (count stacks) "matching" (if (= 1 (count stacks)) "stack" "stacks"))
  4. (model/print-stacks shared/model stacks)
  5. (do (cfn/ensure-stacks!
  6. shared/model
  7. stacks
  8. {:block? true
  9. :params {"Param3" 1}})
  10. (cli/exit! 0)))

This expression might output the following interactive dialogue:

  1. Found 1 matching stack
  2. | :name | :account | :env | :microservice | :template |
  3. |----------------+----------+------------+-------------------------------------------|
  4. | aaa-sandcastle | sandbox | sandcastle | aaa | microservice.edn |
  5. Using constants file constants.edn
  6. Using spec file spec.edn
  7. Ensuring 1 stack exists: aaa-sandcastle
  8. 🥞 aaa-sandcastle 🥞
  9. Stack description..
  10. account sandbox
  11. env sandcastle
  12. microservice aaa
  13. template microservice.edn
  14. Checking template..
  15. Path:
  16. CLOUD: {"Resources" {"Ex" "Value1"}}
  17. LOCAL: {"Resources" {"Ex" "Value2"}}
  18. Checking parameters..
  19. Param1 previous 1000
  20. Param2 default 4
  21. Param3 changed 0 => 1
  22. Apply changes? [y/n] y

Followed by a summary of the CloudFormation events.

License

Copyright © Sparkfund 2020

Distributed under the Apache License, Version 2.0. See LICENSE for details.