EC2 User Data to fetch S3 Object
<p>I have an <strong>EC2 instance in a Public subnet</strong>,<br>
The EC2 has a <strong>S3 Full Access Role</strong> attached to it.<br>
I have a <strong>.jar in a S3 folder</strong> location.<br>
The S3 Bucket is with default permissions, which are applied when the bucket is created.<br><br></p>

<p>What I want to do is implement a User Data script,<br>
that will <strong>download the .jar on EC2 from S3</strong> location.<br>
I am <strong>able to wget the .jar only when I make the .jar Public</strong>,<br><br></p>

<p><strong>If I do not make the .jar on the S3 Public, I am not able to wget it from the EC2</strong>,<br>
I do not want to make the .jar Public for security reasons.</p>

<p>What should I do to get the .jar from S3, without making the .jar Public?</p>

<p>Here's my User Data script - </p>

<pre><code>#! /bin/bash

sudo timedatectl set-timezone Asia/Kuala_Lumpur

sudo rm -rf /home/ubuntu/jarName.jar

sudo rm -rf /home/ubuntu/nohup.out

/usr/local/bin/aws s3api get-object --bucket myDemoBucket --key folderNameDemo/jarName.jar /home/ubuntu/jarName.jar

cd /home/ubuntu/

sudo nohup java -jar -Dspring.profiles.active=uat jarName.jar &gt; nohup.out &amp;

exit
</code></pre>