Search code examples
amazon-web-servicesshellmimeautoscalinguser-data

How to MIME encode a Shell Scrip to deploy it on EC2 intances


I am creating a Auto Scaling Group on AWS and need that my EC2 instances already came with my application image deployed on them, so I have created an user data script to be able to execute it

#!/bin/bash

before_reboot(){
    sudo amazon-linux-extras install docker -y
    sudo service docker start
    sudo usermod -a -G docker ec2-user
    sudo chkconfig docker on
    sudo yum install -y git
    sudo reboot
}

after_reboot(){
    sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose version
    git clone https://github.com/augustoaccorsi/microservices.git
    cd microservices
    docker-compose -f docker-compose-front.yml up --build --detach
}

if [ -f /var/run/rebooting-for-updates ]; then
    after_reboot
    rm /var/run/rebooting-for-updates
    update-rc.d myupdate remove
else
    before_reboot
    touch /var/run/rebooting-for-updates
    update-rc.d myupdate defaults
    sudo reboot
fi

But when the auto scaling create an instance, my image is not deployed and if I shh the instance and check the /var/log/cloud-init-output.log, I got the following

__init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'before_reboot(){...'

I have research that I need to MIME encode my shell script in order to make it executable for the instance, but I do not know hot to do that on my windows machine, can anyone help me?


Solution

  • After reading carefully the AWS docs, I noticed that I was missing some configuration in my user data file, like cloud-config for instance, so I added these missing configs and I was able to start my EC2 instance with the required code. Like this:

    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash
    sudo amazon-linux-extras install docker -y
    sudo service docker start
    sudo usermod -a -G docker ec2-user
    sudo chkconfig docker on
    sudo yum install -y git
    sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose version
    git clone https://github.com/augustoaccorsi/microservices.git
    cd microservices
    docker-compose -f docker-compose-front.yml up --build --detach
    --//
    

    My error was that I was missing to configure some header info, file MIME version and so on.