mike
mike

Reputation: 1663

Is there a way to get a github PR merge to trigger an EC2 instance to start?

We're trying to get an EC2 instance to start whenever we merge a Pull Request in github.

I have this in crontab

@reboot /home/user/server-start.sh

Which does everything I want to happen (like git pull origin master) when the instance starts up, and that script works well.

I've looked at AWS CodeDeploy but I can't see a way to have that just turn an instance on (this server will usually be turned off, it just needs to turn on and do some stuff when we merge a PR, then turn off again)

I'm not sure what the best approach for this would be, so any pointers would be great.

Here's the startup script and nginx server config file, in case they are helpful.

/home/user/server-startup.sh

#!/bin/bash

# get latest master branch
cd /path/to/repo
sudo git pull origin master

# run standard preparation script
sudo bash /home/user/scripts/prepare-app.sh

# run any custom commands for this update
sudo bash /path/to/repo/prepare-app.sh

# change http code to 200
sudo sed -i -e 's/return 500/return 200/g' /etc/nginx/sites-available/status

# restart nginx
sudo service nginx restart

# change http code to 500
sudo sed -i -e 's/return 200/return 500/g' /etc/nginx/sites-available/status

/etc/nginx/site-available/status

server {
    listen      80 default_server;
    server_name status.mydomain.com;

    location / {
        return 500;
    }
}

Upvotes: 1

Views: 453

Answers (1)

cjwfuller
cjwfuller

Reputation: 450

If your instance is just stopped, not terminated then you could use the AWS CLI to start your instance:

aws ec2 start-instances --instance-ids i-1348636c

Docs: https://aws.amazon.com/cli/

This code could be triggered with a git hook: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks.

The rest of your script would only run when the instance has finished starting up so you would need some sort of health check on the server.

You could also consider CI/CD tools if you want something a bit more manageable e.g. Bamboo / Codeship.

Upvotes: 2

Related Questions