Shell Script for AWS EC2 init

1.5k views Asked by At

I'm trying to initialize a AWS EC2 instance using a shell script. My ultimate goal is to automate this process with boto (the python SDK) and so far I have a simple test script that will run a new ubuntu instance, and then with the shell script will update and download some tools (that works fine) but then I try to have the script create a folder, create a file and ultimately run an AWS cli command to download a file from an S3 bucket.

Once the instance is created, I can ssh in and run the commands that create the folder, the file and download the file from the bucket, but, the script won't do that for me and I can't figure out why.

My script looks like this:

#!/bin/bash
apt-get -y update
apt-get -y install awscli
mkdir ./test_folder 
echo -e "data" > ./test_folder/test_file
aws s3 ls

For the purpose of this post I replaced access key data with "data" so ultimately the s3 command would not work but if someone can help me understand why the mkdir and echo commands won't run I'm sure I can figure the rest out.

1

There are 1 answers

0
Norman Ettedgui On

I managed to figure out, thanks for the help. For non native commands I needed to specify the user using su 'user' -c 'command' that allowed me to run commands like pip install boto and aws s3 ls. Hope if anyone runs into this problem this will help!