AWS CodeDeploy: Een CI/CD-pipeline instellen voor implementatie op EC2 via GitHub
Applicaties handmatig implementeren is omslachtig, foutgevoelig en inefficiΓ«nt. AWS CodeDeploy maakt het automatiseren van implementaties eenvoudig, verhoogt de efficiΓ«ntie en minimaliseert downtime. In deze handleiding leert u stap voor stap hoe u een robuuste CI/CD-pipeline opzet om uw applicaties rechtstreeks vanuit GitHub naar een Amazon EC2-instantie te implementeren.
Wat u nodig heeft:
- AWS-account
- EC2-instantie met Amazon Linux
- GitHub-repository met uw applicatie
- AWS CLI geΓ―nstalleerd op uw lokale machine
Stap 1: Uw EC2-instantie voorbereiden
Maak verbinding met uw EC2-instantie via SSH:
ssh -i "your-key.pem" ec2-user@your-ec2-instance-public-ip
Installeer en start de CodeDeploy-agent:
sudo yum update
sudo yum install ruby -y
sudo yum install wget -y
cd /home/ec2-user
wget https://aws-codedeploy-region.s3.region.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status
Vervang region door uw AWS-regio, bijv. us-east-1.
Stap 2: Een IAM-rol aanmaken voor CodeDeploy
In de AWS-console:
- Navigeer naar IAM β Rollen β Rol aanmaken.
- Kies AWS-service en selecteer CodeDeploy.
- Koppel beleid: selecteer AWSCodeDeployRole.
- Geef uw rol een naam (bijv.
CodeDeployRole) en maak hem aan.
Stap 3: De CodeDeploy-applicatie instellen
- Ga naar AWS CodeDeploy β Applicaties β Applicatie aanmaken.
- Voer een applicatienaam in.
- Kies computerplatform: EC2/On-premises.
Maak een implementatiegroep aan:
- Naam van de implementatiegroep (bijv.
Production) - Selecteer uw IAM-rol (
CodeDeployRole) - Selecteer EC2-instanties op tag of handmatig
- Implementatietype: In-place
Stap 4: Uw GitHub-repository voorbereiden
Maak een appspec.yml-bestand aan in de root van uw repository:
version: 0.0
os: linux
files:
- source: /
destination: /var/www/html
hooks:
AfterInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
Maak de benodigde scripts aan, bijv. scripts/install_dependencies.sh:
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo service httpd start
sudo chkconfig httpd on
Zorg dat uw scripts uitvoerbaar zijn:
chmod +x scripts/*.sh
Commit en push naar GitHub:
git add .
git commit -m "Added appspec.yml and deployment scripts"
git push origin main
Stap 5: AWS CodeDeploy koppelen aan GitHub
- Selecteer in de CodeDeploy-console uw applicatie en implementatiegroep.
- Klik op Implementatie aanmaken.
- Selecteer GitHub als repositorytype.
- Verbind met GitHub en autoriseer AWS.
- Kies uw repository en commit-branch (bijv.
main). - Klik op Implementeren.
Stap 6: CI/CD automatiseren met GitHub Actions
Maak .github/workflows/deploy.yml aan in uw repository:
name: AWS Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: AWS CodeDeploy
uses: aws-actions/aws-codedeploy@v1
with:
application-name: your-codedeploy-app-name
deployment-group-name: your-deployment-group
region: your-region
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Stel AWS-inloggegevens in als GitHub-secrets:
- Ga naar GitHub β Instellingen β Secrets en variabelen β Acties β Nieuw repository-secret
- Voeg
AWS_ACCESS_KEY_IDenAWS_SECRET_ACCESS_KEYtoe.
Voortaan triggert elke push naar main automatisch een implementatie.
Stap 7: Uw implementatie verifiΓ«ren
Bezoek het publieke IP-adres van uw EC2-instantie in een webbrowser:
http://your-ec2-instance-public-ip
U zou uw succesvol geΓ―mplementeerde applicatie moeten zien.
Conclusie
Gefeliciteerd! U heeft met succes een AWS CodeDeploy CI/CD-pipeline opgezet die is geΓ―ntegreerd met GitHub voor automatische implementaties op EC2. Deze automatisering stroomlijnt uw implementatieproces aanzienlijk, vermindert downtime en vereenvoudigt updates.