pin-cnx/laravel-aws-deploy
Artisan command to deploy Laravel to AWS EC2 Auto Scaling with near zero downtime. Creates an AMI from a master instance, builds a new launch configuration, updates the ASG, spins up new instances, and can terminate old ones.
composer require pin-cnx/laravel-aws-deploy
.env:
EC2_KEY=your_aws_access_key
EC2_SECRET=your_aws_secret_key
EC2_REGION=ap-southeast-1
config/services.php:
Add the ec2 configuration block with your AWS profile (e.g., AnyName) and required parameters (e.g., InstanceId, AutoScalingGroupName).
Example:
'ec2' => [
'key' => env('EC2_KEY'),
'secret' => env('EC2_SECRET'),
'region' => env('EC2_REGION'),
'profiles' => [
'AnyName' => [
'InstanceId' => 'i-0123456789abcdef',
'AutoScalingGroupName' => 'my-auto-scale',
'SecurityGroups' => 'sg-123456',
'KeyName' => 'serverkey',
'AMI_PREFIX' => 'AWSDEPLOY',
'InstanceType' => 't3.nano',
],
],
],
php artisan ec2backup
Or deploy a specific profile:
php artisan ec2backup --profile AnyName
Zero-Downtime Deployment: Use this package to deploy updates to an Auto Scaling Group (ASG) without interrupting service. For example:
InstanceId).php artisan ec2backup to:
config/services.php for different environments (e.g., staging, production).php artisan ec2backup --profile=staging --profile=production
UserData to automate post-deployment tasks.
Example:
'UserData' => "#!/bin/bash\n" .
"cd /var/www/html && git pull origin main && " .
"composer install --no-dev --optimize-autoloader && " .
"php artisan config:cache && php artisan cache:clear",
AMI_TAGS for tracking or cost allocation.
Example:
'AMI_TAGS' => [
['Key' => 'Environment', 'Value' => 'Production'],
['Key' => 'Owner', 'Value' => 'DevOps'],
],
region) to distribute instances across Availability Zones (AZs).
Example:
'region' => ['ap-southeast-1a', 'ap-southeast-1b'],
IsTerminateCurrentInstance) for blue-green deployments.
Example:
'IsTerminateCurrentInstance' => false,
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('ec2backup --profile=production')
->dailyAt('3:00'); // 3 AM UTC
}
before/after hooks to run pre/post-deployment tasks (e.g., database migrations)..env.staging, .env.production) to override AWS credentials and profiles.
Example .env.production:
EC2_KEY=prod_aws_key
EC2_SECRET=prod_aws_secret
use PinCnx\LaravelAwsDeploy\Deployer;
class AwsDeployService {
public function deploy(string $profile)
{
try {
$deployer = new Deployer(config('services.ec2.profiles.'.$profile));
$deployer->deploy();
Log::info("Deployment successful for profile: {$profile}");
} catch (\Exception $e) {
Log::error("Deployment failed: {$e->getMessage()}");
throw $e;
}
}
}
# .github/workflows/deploy.yml
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-php@v2
- run: composer install
- run: php artisan ec2backup --profile=production
env:
EC2_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
EC2_SECRET: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
401 Unauthorized errors if IAM roles lack permissions for ec2:DescribeInstances, ec2:CreateImage, or autoscaling:UpdateAutoScalingGroup.AmazonEC2FullAccess and AmazonAutoScalingFullAccess policies to the IAM user/role.running state; otherwise, AMI creation fails.$instance = $ec2->describeInstances(['InstanceIds' => [$instanceId]]);
if ($instance['Instances'][0]['State']['Name'] !== 'running') {
throw new \RuntimeException("Master instance must be running.");
}
SecurityGroups ID is invalid or lacks required rules (e.g., SSH, HTTP).aws ec2 describe-security-groups --group-ids sg-123456
AWSDEPLOY-2023-01-01) if deployments run frequently.AMI_PREFIX:
'AMI_PREFIX' => 'AWSDEPLOY-'.now()->format('YmdHis'),
UserData scripts (e.g., missing dependencies) may leave instances in a broken state.#!/bin/bash
set -e # Exit on error
echo "Starting deployment..." >> /var/log/user-data.log 2>&1
# Your commands here
/health endpoint returns 500).Add this to config/services.php to log AWS API calls:
'ec2' => [
'debug' => true, // Enable AWS SDK debugging
// ... other config
],
How can I help you explore Laravel packages today?