title: Using for loop and other declared variables in ec2 userdata cloudformation
body: <p>I am new to cloudformation yaml.<br>
  I have the following which I need to use as userdata in launch configuration:</p>
  
  <pre><code>  UserData:
      Fn::Base64: !Sub 
        - |
          #!/bin/bash -x
          apt-get -y update &amp;&amp; apt install -y awscli mysql-client libmysqlclient-dev python-pip
            for item in certs user_prop config log
            do
              echo "... preparing ${item} database and config"
              MYSQL_DB_NAME="ext_as_${item}"
              LOCAL_DB_NAME=$(echo ${item}|tr -d '_')
              LOCAL_DB_FILE="/path/to/db/${LOCAL_DB_NAME}"
              DB_KEY="${item}_db"
              #- set db configuration value
              sed -i "s|${DB_KEY}=.*|${DB_KEY}==mysql://${DB_FQDN}/${MYSQL_DB_NAME}|" /path/to/config.conf
              #- create mysql db
              mysql --defaults-file=${MYSQL_PREF} -e "CREATE DATABASE IF NOT EXISTS $${MYSQL_DB_NAME};"
              #- import local DB schema into MySql if no tables exist
              mysql --defaults-file=$${MYSQL_PREF} --silent --skip-column-names \
              -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${MYSQL_DB_NAME}';"|grep -e ^0 -q \
              &amp;&amp; ./dbcvt -t ${item} -s sqlite:///${LOCAL_DB_FILE} -d mysql://${DB_FQDN}/${MYSQL_DB_NAME} -p ${MYSQL_PREF}
            done
          popd
        - 
          DB_FQDN:
            'Fn::ImportValue': 'OVPNConfigDbEndpoint'
  </code></pre>
  
  <p>Now the problem is <code>DB_FQDN</code> gets resolved but <code>LOCAL_DB_FILE</code>, <code>LOCAL_DB_NAME</code>, <code>DB_KEY</code>, <code>MYSQL_DB_NAME</code>, and <code>MYSQL_PREF</code> don't get resolved. Now the questions are:</p>
  
  <ol>
  <li>If I hard-code the other values, how do I use a for loop in a userdata shell script ?  </li>
  <li>Do I have to always use a Fn::Join as mentioned in <a href="https://stackoverflow.com/a/54535452/4035062">https://stackoverflow.com/a/54535452/4035062</a> by Alex ?<br>
  using <code>Fn::Join</code> to write a simple shell script seems quite complicated.  </li>
  <li>What does <code>+</code> mean in <code>!Sub |+</code> ?</li>
  </ol>