AWS PHP SDK - Create an Entry in Route53 - Laravel
Multi-tenant SaaS applications often need to provision DNS records dynamically — creating a subdomain for each new customer without manual intervention. AWS Route53 supports this through its API, and the PHP SDK makes it straightforward to integrate with a Laravel application.
This guide walks through the full setup: installing the SDK, configuring credentials, and creating a CNAME record in a Route53 hosted zone from a Laravel controller.
Step 1: Install the Required Packages
Add the AWS SDK and its Laravel adapter to your project:
composer require aws/aws-sdk-php
composer require aws/aws-sdk-php-laravel
Or add them directly to composer.json:
"aws/aws-sdk-php": "^3.148",
"aws/aws-sdk-php-laravel": "~3.0"
Step 2: Add AWS Credentials to .env
Add these variables to your .env file with values from your AWS IAM user:
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_DEFAULT_REGION=us-east-1
The IAM user must have route53:ChangeResourceRecordSets and route53:ListHostedZones permissions on the hosted zone you want to manage.
Step 3: Publish the AWS Config
Publish the package configuration file:
php artisan vendor:publish --provider="Aws\Laravel\AwsServiceProvider"
This creates config/aws.php which reads your environment variables.
Step 4: Create the Route53 Entry
In your controller or service class, import the Route53 client and create the DNS record:
use Aws\Route53\Route53Client;
$client = Route53Client::factory([
'credentials' => config('aws.credentials'),
'region' => config('aws.region'),
'version' => config('aws.version'),
]);
$result = $client->changeResourceRecordSets([
'HostedZoneId' => 'YOUR_HOSTED_ZONE_ID',
'ChangeBatch' => [
'Comment' => 'Auto-provisioned subdomain',
'Changes' => [
[
'Action' => 'CREATE',
'ResourceRecordSet' => [
'Name' => 'customer-name.yourdomain.com',
'Type' => 'CNAME',
'TTL' => 600,
'ResourceRecords' => [
['Value' => 'your-load-balancer-or-server.com'],
],
],
],
],
],
]);
Key Parameters
- HostedZoneId: Found in the AWS Route53 console under your hosted zone details. Looks like
Z1234ABCDE5678. - Action: Use
CREATEfor new records,UPSERTto create-or-update, orDELETEto remove. - Type: Supports
A,AAAA,CNAME,MX,TXT, and others. - TTL: Time-to-live in seconds. 600 (10 minutes) is a reasonable default for dynamic records.
- Value: For CNAME, the target hostname. For A records, the IP address.
Checking the Result
The API call is asynchronous — Route53 propagates the change across its nameservers. You can check the status with:
$changeId = $result['ChangeInfo']['Id'];
$status = $client->getChange([
'Id' => $changeId,
]);
echo $status['ChangeInfo']['Status']; // PENDING or INSYNC
Once the status is INSYNC, the DNS record is live globally.
Conclusion
The AWS PHP SDK makes Route53 DNS management accessible from any Laravel application. Combined with the UPSERT action and proper IAM permissions scoped to specific hosted zones, this pattern handles dynamic subdomain provisioning reliably — useful for any multi-tenant platform where each customer gets their own subdomain.