bashamazon-web-servicesamazon-ec2user-data

WGET seems not to work with user data on AWS EC2 launch


I launch an centos AMI I created, and try to add user data as a file which looks like this:

#!/bin/bash
mkdir /home/centos/testing
cd testing 
wget https://validlink

So simply, on launch, the user data creates a folder called testing and downloads this validURL which I will not put as it links to my data - however it is valid and accessible.

When I launch the instance, the folder testing is created successfully, however there is no file inside the directory.

When I ssh into the instance, and run the wget command as a sudo, the file is downloaded successfully inside the testing folder.

Why does the file not get downloaded on the ec2 launch through user data?


Solution

  • You have no way of knowing the current working directory when you execute the cd command. So specify full path:

    cd /home/centos/testing
    

    Try this:

    #!/bin/bash
    mkdir /home/centos/testing
    cd /home/centos/testing 
    wget https://validlink