Cloud Formation — Helper Scripts

June Chung
2 min readNov 5, 2020

--

  • cfn-init : will look at metadata and install/configure dependencies (yum, files, etc)
  • cfn-signal : gives signal to the “waiting” cf template ( to proceed .. etc)
  • cfn-get-metadata : to retrieve metadata
  • cfn-hup : detect changes in metadata and action on changes

usually : cfn-init → cfn-signal → cfn-hup
cfn init will go ahead and configure the instance
cfn-signal will let CF template that configuration has been completed

  1. CFN-INIT

Configured by specifying “config” in metadata section
And triggered at the “user data” section
multiple configs → configsets

  • EXECUTION IN THIS ORDER
  • packages : install list of package
  • groups : user group
  • user : user
  • source : download external file from somewhere else (zip archive in S3, github repository)
  • files : create files on instance OR pull from URL
    *** Sub! function → will substitute the variables dynamically
  • !!! No need to create parent directory before “files” part. “files” will automatically create the directory for you
  • commands : series of command
  • ** !Sub == Fn::Sub

** Tips on “commands”
- commands need “test” to run (inside nested scope)
- depending on test pass or fail → determines if they will run command or not
- USE linux test command
- ex. test ! -e /home/petclinic → Petclinic files does not exists
- ex. test 10 = 10 → is 10 equal 10 ?
- → type in >> echo $? >> to get the boolean value (to get the result, or nothing will pop up )
- concatenated : test 10 = 10; echo $?

2. CFN-SIGNAL
after cfn-init finishes it’s job → notifies the cloudformation template
Used with creation Policy

Means it will wait for 5 minutes for the health check !

3. CFN-HUP
: looks for metadata change every 15 minutes
need to configure via files1. /etc/cfn/cfn-hup.conf 2. /etc/cfn/hooks.d/cfn-auto-reloader.conf

--

--