<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Naman Patel on Medium]]></title>
        <description><![CDATA[Stories by Naman Patel on Medium]]></description>
        <link>https://medium.com/@naman.it?source=rss-ed53db39701c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZYquIzdRHykovw9Y</url>
            <title>Stories by Naman Patel on Medium</title>
            <link>https://medium.com/@naman.it?source=rss-ed53db39701c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 05:10:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@naman.it/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[EC2 user-data script — Modify it to run on every reboot]]></title>
            <link>https://medium.com/@naman.it/ec2-user-data-script-modify-it-to-run-on-every-reboot-3205a28a840c?source=rss-ed53db39701c------2</link>
            <guid isPermaLink="false">https://medium.com/p/3205a28a840c</guid>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[aws-ec2-user-data]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Naman Patel]]></dc:creator>
            <pubDate>Mon, 08 Aug 2022 21:15:37 GMT</pubDate>
            <atom:updated>2022-08-09T14:40:30.952Z</atom:updated>
            <content:encoded><![CDATA[<h3>EC2 user-data script — Modify it to run on every reboot</h3><p>Most of AWS System admins are familiar with <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html">user-data</a> scripts. It is very handy tool for boot strapping EC2 instances. The user-data script gets called only when we first launch instance, it doesn&#39;t get called on repeated reboots. This is by design, but there are cases where we can use same script on first launch and subsequent reboots. Following is the one such use case.</p><p>I am setting up my EC2 instance mainly using docker &amp; docker compose service, following is my user-data file.</p><p><strong>High level Steps</strong></p><ol><li>Get docker image from ECR</li><li>Get Docker compose file from S3<br> — Docker compose needs 2 parameters:<br> — Queue: Supplied from EC2 Tag (needed for application docker)<br> — IP : Fetching from AWS metadata api</li><li>Start docker, using docker compose up</li></ol><pre>#!/bin/bash<br><br>#Get Instance Private IP &amp; Export it, used in docker compose as input<br>export IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)<br><br><br># Get Latest Docker Image<br>aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 999999999.dkr.ecr.us-east-1.amazonaws.com<br><br>docker pull 999999999.dkr.ecr.us-east-1.amazonaws.com/fme-engine:22618<br><br>#Get queue tag from EC2, and export as env variable. used in docker compose as input<br>TAG_NAME=&quot;QUEUE&quot;<br>INSTANCE_ID=&quot;`wget -qO- http://instance-data/latest/meta-data/instance-id`&quot;<br>REGION=&quot;`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed -e &#39;s:\([0-9][0-9]*\)[a-z]*\$:\\1:&#39;`&quot;<br>export QUEUE=&quot;`aws ec2 describe-tags --filters &quot;Name=resource-id,Values=$INSTANCE_ID&quot; &quot;Name=key,Values=$TAG_NAME&quot; --region $REGION --output=text | cut -f5`&quot;<br><br><br># Get Latest docker compose file from S3 bucket<br>aws s3 cp s3://mys3bucket/engine-docker/docker-compose.yaml /home/ubuntu/engine-docker<br><br><br>#Start Docker<br>cd /home/ubuntu/engine-docker<br>docker compose down<br>docker compose up -d</pre><h3><strong>Problem</strong></h3><p>This works fine for the first launch of the instance. If we need to reboot the instance, it will create problem because docker compose needs 2 input which are supplied through environment variable and they are not configured on subsequent reboots. Plus, It would be nice to get latest image from ECR on reboot and get latest docker compose file from S3 when we are rebooting.</p><p>Basically, I need to run user-data script on every reboot for docker containers to work properly and efficiently.</p><h3><strong>Solution 1 Using cloud-init per-boot script</strong></h3><p><strong>Step 1: Copy User-data script to cloud-init per-boot script folder and clean up code code</strong></p><p>I added following code to my user-data script, which copies user-data to /var/lib/cloud/scripts/per-boot folder , makes it executable. And, removes these code from user-data script so copy code is called only once on the launch of new instance and not on every reboot.</p><pre>#Copy this file and remove this copy commands, removes last 4 lines, so it is important to keep this in the end.<br>sudo cp /var/lib/cloud/instance/user-data.txt /var/lib/cloud/scripts/per-boot/user-data.sh<br>sudo head -n -3 /var/lib/cloud/scripts/per-boot/user-data.sh &gt; temp.txt &amp;&amp; sudo mv temp.txt /var/lib/cloud/scripts/per-boot/user-data.sh<br>sudo chmod +x /var/lib/cloud/scripts/per-boot/user-data.sh</pre><p>Complete user-data code</p><pre>#!/bin/bash<br><br>#Get Instance Private IP &amp; Export it <br>export IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)<br><br><br># Get Latest Docker Image<br>aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 999999.dkr.ecr.us-east-1.amazonaws.com<br><br>docker pull 999999.dkr.ecr.us-east-1.amazonaws.com/fme-engine:22618<br><br>#Get queue tag from EC2<br>TAG_NAME=&quot;QUEUE&quot;<br>INSTANCE_ID=&quot;`wget -qO- http://instance-data/latest/meta-data/instance-id`&quot;<br>REGION=&quot;`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed -e &#39;s:\([0-9][0-9]*\)[a-z]*\$:\\1:&#39;`&quot;<br>export QUEUE=&quot;`aws ec2 describe-tags --filters &quot;Name=resource-id,Values=$INSTANCE_ID&quot; &quot;Name=key,Values=$TAG_NAME&quot; --region $REGION --output=text | cut -f5`&quot;<br><br><br># Get Latest docker compose file<br>aws s3 cp s3://fme-docker/fme-server-2022/engine-docker/docker-compose.yaml /home/ubuntu/engine-docker<br><br><br>#Start Docker<br>cd /home/ubuntu/engine-docker<br>docker compose down<br>docker compose up -d<br><br>#Copy this file and remove this copy commands, removes last 4 lines, so it is important to keep this in the end.<br>sudo cp /var/lib/cloud/instance/user-data.txt /var/lib/cloud/scripts/per-boot/user-data.sh<br>sudo head -n -3 /var/lib/cloud/scripts/per-boot/user-data.sh &gt; temp.txt &amp;&amp; sudo mv temp.txt /var/lib/cloud/scripts/per-boot/user-data.sh<br>sudo chmod +x /var/lib/cloud/scripts/per-boot/user-data.sh</pre><h3><strong>Solution 2 Using Systemd</strong></h3><p><strong>Step 1: Copy user-data script, reload and enable systemd</strong></p><p>I added following code to my user-data script, which copies user-data to /usr/sbin folder (this can be any other folder, path will accordingly change), makes it executable. And, removes these code from user-data script so copy code is called only once on the launch of new instance and not on every reboot.</p><pre>#Copy user-data file and remove these copy commands i.e. last 4 lines, so it is important to keep this in the end.<br>sudo cp /var/lib/cloud/instance/user-data.txt /usr/sbin/user-data.sh<br>head -n -4 /usr/sbin/user-data.sh &gt; temp.txt &amp;&amp; mv temp.txt /usr/sbin/user-data.sh<br>sudo chmod +x /usr/sbin/user-data.sh<br>sudo systemctl daemon-reload &amp;&amp; sudo systemctl enable fme-engine.service</pre><p>Complete user-data</p><pre>#!/bin/bash<br><br>#Get Instance Private IP &amp; Export it <br>export IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)<br><br><br># Get Latest Docker Image<br>aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 999999.dkr.ecr.us-east-1.amazonaws.com<br><br>docker pull 999999.dkr.ecr.us-east-1.amazonaws.com/fme-engine:22618<br><br>#Get queue tag from EC2<br>TAG_NAME=&quot;QUEUE&quot;<br>INSTANCE_ID=&quot;`wget -qO- http://instance-data/latest/meta-data/instance-id`&quot;<br>REGION=&quot;`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed -e &#39;s:\([0-9][0-9]*\)[a-z]*\$:\\1:&#39;`&quot;<br>export QUEUE=&quot;`aws ec2 describe-tags --filters &quot;Name=resource-id,Values=$INSTANCE_ID&quot; &quot;Name=key,Values=$TAG_NAME&quot; --region $REGION --output=text | cut -f5`&quot;<br><br><br># Get Latest docker compose file<br>aws s3 cp s3://fme-docker/fme-server-2022/engine-docker/docker-compose.yaml /home/ubuntu/engine-docker<br><br><br>#Start Docker<br>cd /home/ubuntu/engine-docker<br>docker compose down<br>docker compose up -d<br><br>#Copy this file and remove this copy commands, removes last 4 lines, so it is important to keep this in the end.<br>sudo cp /var/lib/cloud/instance/user-data.txt /usr/sbin/user-data.sh<br>head -n -4 /usr/sbin/user-data.sh &gt; temp.txt &amp;&amp; mv temp.txt /usr/sbin/user-data.sh<br>sudo chmod +x /usr/sbin//user-data.sh<br>sudo systemctl daemon-reload &amp;&amp; sudo systemctl enable fme-engine.service</pre><p><strong>Step 2 Configure service on AMI:</strong></p><p>Create service unit file</p><p>sudo nano /etc/systemd/system/fme-engine.service</p><pre>[Unit]<br>Description=Starts FME Engine using userdata script<br>After=docker.service<br>[Service]<br>ExecStart=/usr/sbin/user-data.sh</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>Reload Daemon, and keep this service disable. This is very important, otherwise on first launch of EC2 instance user-data script and service will interfere with each other. So our goal here is to keep this service deactivated for first launch and activate it during user-data script execution.</p><pre>sudo systemctl daemon-reload &amp;&amp; sudo systemctl disable fme-engine.service</pre><p>Step 1 and 2 can be done in any order.</p><p><strong>Conclusion:</strong></p><p>There are 2 approaches we can use, systemd and cloud-init. With systemd you will have to tweak AMI little bit, which you don&#39;t need to do with cloud-init.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3205a28a840c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>