简体   繁体   中英

User-data script not executed when starting EC2 instance from AWS CLI

I am trying to use the AWS CLI to start an EC2 instance from an image I have that has Docker installed on it, and then supply that instance a script passed in by --user-data to pull an image from the docker hub and run that image. My AWS CLI command starts the instance, but it appears that the script passed to --user-data never runs.

The CLI command is:

aws ec2 run-instances --image-id ami-xxxxx --count 1 --instance-type t2.micro --key-name xxxxx --security-groups xxxxxxx --user-data file:///Users/xxxxxx/startlb.sh

And the contents of startlb.sh :

#!/bin/bash
docker pull xxxxxx && docker run -p 9000:9000 -p 9090:9090 -d xxxxxxx go run lbserver.go > /home/ec2-user/startup.log

I'm redirecting the output of the docker command into startup.log to see if any standard output occurs. When I log into the instance the log file is not created leading me to believe the user-data script never runs. docker images and docker ps both show that no images or containers are on the instance.

The command successfully starts the instance, just without pulling and running the docker container (Docker is running on the instance by default so there is no need to start it). After starting the instance if I pasted the contents of the script into the terminal and execute it pulls the container and runs it, so there doesn't appear to be anything wrong with the script.

Turns out it was working after all. I wasn't allowing enough time for the docker pull to download everything. Checking for the existence of the log file, image and container after a couple of minutes showed that everything was there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM