In a previous post we discussed continuous integration with Laravel and Travis CI.
Today, I’d like to show you how to quickly and easily setup continuous deployment from your Github repository to an AWS EC2 instance using AWS CodeDeploy.
Note:
We are assuming that you have already completed the following steps:
- You have already provisioned an EC2 instance
- You already have a Github Repository
If you have not completed either of these steps do so now.
1. Identity and Access Management (IAM)
We need to setup a role so that AWS CodeDeploy can communicate with our EC2 instance.
In the AWS console create a new role called DeployRole and attach the AWSCodeDeployRole policy.
2. Install the CodeDeploy agent
SSH into your EC2 server and run:
sudo apt install ruby
wget https://aws-codedeploy-eu-west-1.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto
Check its running:
service codedeploy-agent status
3. AWS CodeDeploy AppSpec File
Create an appspec.yml file in your github repo (root) and add the following:
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/
This will deploy your entire Github repository to the /var/www/html directory on our EC2 instance.
4. Create deployments application
In the AWS console create a new CodeDeploy application:
- Call it codedeploy
- Select EC2/On-premises
- Create a deployment group called githubdeploy
- Attach our EC2 instance
- Attach our DeployRole we created earlier
5. Create a deployment
In the AWS console create a new deployment:
- Select our codedeploy application
- Select our githubdeploy deployment group
- Connect your github and set repository and commit id
You should then see a successful deployment
6. Create a new user for deployments
In the AWS console create a new policy:
- Call it github-access and allow all CodeDeploy actions
In the AWS console create a new user:
- attach the github-access policy we just created
- when the user is created you need to copy the Access key ID and secret key for use later
7. Auto deployment
Over to Github now where we can setup automatic deployments.
- Go to your developer settings and generate a new personal access token
- note it down you will need it shortly
Goto your repo settings and click Integrations & services
- Add a new service and select GitHub Auto-Deployment
- Add another service and select AWS CodeDeploy
- Enter all the details you have gathered, as shown below
Now make a change to your repository. Your changes should automatically deploy to your EC2 server.
If you need help with AWS deployments or other AWS services, see our AWS solutions page.