Search code examples
amazon-web-servicesamazon-ec2startupyum

AWS EC2 - yum update does not work in AutoScaling LaunchConfig UserData


I can successfully run sudo yum update when ssh-ing to my EC2 instance. However, when I attach the same command to the userData of my launch configuration, I see the following error (in /var/log/cloud-init-output.log):

launch script..
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

My script:

#!/bin/bash
echo "launch script.."
sudo yum update -y
sudo yum install java-1.8.0 -y
aws s3 cp s3://bucket/app.jar ./app.jar
java -jar app.jar >> out.log

How can I run yum commands at EC2 instance startup?


Solution

  • Make sure that you actually have a route to the internet from your EC2 instance. That typically means either a public IP or a route to a NAT instance/gateway, and an Internet Gateway in your VPC.

    It may be that the userdata script begins to run before connectivity has been established. You may need to verify that the instance has outbound network access before your script runs yum update, for example:

    #!/bin/bash
    echo "launch script.."
    
    until ping -c1 www.google.com &>/dev/null; do
        echo "Waiting for network ..."
        sleep 1
    done
    
    yum update -y
    # other things here
    

    There are other options to wait for the network (here and here).

    Also note that user data scripts are executed as the root user, so you do not need sudo.