4

I am creating an ec2 instance from an AMI using

aws ec2 run-instances [lots of arguments] --user-data file://my_sh_file.sh

my_sh_file.sh looks like this:

su ubuntu   # fails with or without this
cd /home/ubuntu/my_working_dir
git pull origin master
. app_startup_script.sh

Git is failing to connect with this error log (from /var/log/cloud-init-output.log):

Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

If I log in myself (as ubuntu) and call git pull it works fine.

I guess that either (a) su ubuntu isn't working, or (b) (this seems less likely) that the ssh config isn't loaded at this point in the boot sequence... but I'm stumped. Help?

4
  • 1
    How are you cloning the git repo to begin with? That response suggests it may be an issue with the ssh key.
    – oxr463
    Jun 5, 2019 at 15:31
  • git remote -v please. By the way, do you use ssh-agent?
    – kubanczyk
    Jun 5, 2019 at 18:00
  • I don't use ssh agent. Git remote: origin ec2-user@[ip of git server]:[repo name] (fetch) origin ec2-user@[ip of git server]:[repo name] (push) I agree it sounds like an access problem but when I log in myself and just call git pull origin master it works fine.
    – Omroth
    Jun 5, 2019 at 18:16
  • ssh key and config is in home/ubuntu/.ssh
    – Omroth
    Jun 5, 2019 at 18:17

1 Answer 1

4

The way you use sudo Isn’t correct.

First of all it should be sudo -u ubuntu not just sudo ubuntu. However even then it wouldn't work.

When you run sudo it spawns a new shell or if a command is given it runs that command under the ubuntu user. In your case sudo -u ubuntu doesn’t have a command to run and because it is in non-interactive mode it won’t spawn a shell either. It simply exits.

The following lines - cd and git - then run in the context of the UserData script, not under ubuntu user, that's why it fails.

You can do this for example:

sudo -u ubuntu sh -c "cd /home/...; git pull ...; ./app.sh"

That will run all the commands under the ubuntu user.

Hope that helps :)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .