title: How to run ec2 UserData script on cloudformation update
body: <p>I have ec2 userData script that doing docker-compose pull and up.</p>
  <p>I want to run &quot;aws cloudformation update-stack&quot; and load new docker images.
  Each time ${imageTag} property changes.</p>
  <p>This is my cloudformation instance yml:</p>
  <pre><code> myInstance:
      Type: 'AWS::EC2::Instance'
      Metadata:
        'AWS::CloudFormation::Init':
          configSets:
            configs:
              - &quot;configDockers&quot;
              - &quot;configOther&quot;
          configDocker:
            commands:
              a:
                command: 'echo -e IMAGE_TAG=${imageTag} &gt;&gt; .env'
              b:
                command: 'docker-compose pull'
              c:
                command: 'docker-compose up'
      Properties:
        UserData:
          Fn::Base64:
            !Sub |
            runcmd:
             - /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource myInstance --region ${AWS::Region} -c configs
             - /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource myInstance --region ${AWS::Region}
  </code></pre>
  <p>I tried to add &quot;docker-compose down&quot;, remove old images in the UserData script, and add cloud_final_modules to run UserData each startup:</p>
  <pre><code> myInstance:
      Type: 'AWS::EC2::Instance'
      Metadata:
        'AWS::CloudFormation::Init':
          configSets:
            configs:
              - &quot;configDockers&quot;
              - &quot;configOther&quot;
          configDocker:
            commands:
              a:
                command: 'echo -e IMAGE_TAG=${imageTag} &gt;&gt; .env'
              b:
                command: 'docker-compose down'
              c:
                command: 'docker images -q | xargs -r sudo docker rmi'
              d:
                command: 'docker-compose pull'
              e:
                command: 'docker-compose up'
      Properties:
        UserData:
          Fn::Base64:
            !Sub |
            #cloud-config
            cloud_final_modules:
             - [scripts-user, always]
            runcmd:
             - /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource myInstance --region ${AWS::Region} -c configs
             - /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource myInstance --region ${AWS::Region}
  </code></pre>
  <p>But after &quot;aws cloudformation update-stack&quot; the script does not run again dispite the imageTag changed. it runs only if I change some value under runcmd.</p>
  <p>How can I run the UserData script each time &quot;aws cloudformation update-stack&quot; running (each time imageTag changes)?</p>
  <p>How can I run some UserData commands only on first startup, other commands on each reboot and other commands on stop instance?</p>
  <p>for example: I want to run &quot;docker-compose down&quot; <strong>only on instance stop</strong> but other commands command: 'docker-compose pull/up' <strong>on each instance reboot or &quot;aws cloudformation update-stack&quot;</strong> and some initial commands <strong>only on first setup</strong>.</p>