AWS Elastic Beanstalk
Middleware monitoring for AWS Elastic Beanstalk combines infrastructure collection with Laravel application instrumentation. Host metrics and application telemetry help you investigate resource usage, slow requests, and application errors.
This installation uses Elastic Beanstalk deployment configuration to automatically install the Middleware Agent and configure application instrumentation whenever your environment is deployed or updated.
How it works#
AWS Elastic Beanstalk manages EC2 instances and may replace them during deployments, scaling, or health-related events. Changes made manually to an instance can be lost when it is replaced.
Middleware uses .ebextensions configuration files to automate installation during deployment:
- Middleware Agent: Runs on the EC2 instance and collects telemetry.
- Laravel instrumentation: Enables OpenTelemetry tracing for your application.
- OTLP exporter: Sends application telemetry to the local Middleware Agent, which forwards it to Middleware.
This ensures instrumentation is reapplied during Elastic Beanstalk deployments.
Supported telemetry#
| Traces | Metrics | App Logs | Custom Logs | Profiling |
|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✖ |
Prerequisites#
Before installing Middleware, make sure you have:
- An existing AWS Elastic Beanstalk environment running a PHP Laravel application.
- Laravel version 8.x or later.
- Access to the application source code and deployment configuration.
- Permissions to update Elastic Beanstalk environment properties and deploy application versions.
- A Middleware account with an API key and target URL.
- Outbound HTTPS connectivity from your Elastic Beanstalk EC2 instances to Middleware on port 443.
- PHP 8.0 or later.
- PHP development tools (
phpize) and build tools. The installer compiles the OpenTelemetry PHP extension and stops if these are missing. - PHP extensions enabled:
zlib,mbstring,simplexml,json,dom,openssl,phar,fileinfo,pcre,xmlwriter,gd.
Note: This installation method is intended for Elastic Beanstalk environments using RPM-based EC2 instances.
Installation#
1 Create the Elastic Beanstalk configuration file#
In your Laravel project root, create a directory named .ebextensions if it does not already exist.
Inside it, create a file named 99-mwinstrumentation.config.
Your project structure should look like this:
1your-laravel-project/
2├── .ebextensions/
3│ └── 99-mwinstrumentation.config
4├── app/
5├── config/
6├── public/
7└── ...Add the following configuration:
1container_commands:
2 01_install_agent:
3 command: |
4 MW_API_KEY="<MW_API_KEY>" MW_TARGET=https://<MW_UID>.middleware.io:443 bash -c "$(curl -L https://install.middleware.io/scripts/rpm-install.sh)"
5
6 02_run_instrumentation:
7 command: |
8 cd /var/app/staging
9 curl -O https://install.middleware.io/apm/php/laravel-instrument.php
10 COMPOSER_ALLOW_SUPERUSER=1 php laravel-instrument.php installReplace <MW_API_KEY> and <MW_UID> with your Middleware account details.
This configuration performs two actions during deployment:
01_install_agent: Installs the Middleware Agent on the Elastic Beanstalk EC2 instance.02_run_instrumentation: Downloads and runs the Laravel instrumentation installer.
Note: The installer updates composer.json and composer.lock. It adds the OpenTelemetry and Middleware packages, sets minimum-stability to dev, and turns off the php-http/discovery Composer plugin.
The numeric prefix in 99-mwinstrumentation.config helps control the order in which Elastic Beanstalk processes configuration files.
Note: Avoid storing the Middleware API key directly in source control. For production deployments, supply credentials through Elastic Beanstalk environment properties or a supported secret manager and reference them from your deployment configuration.
2 Configure environment variables#
Open the AWS Management Console and navigate to:
Elastic Beanstalk → Environments → Your Environment → Configuration
In the Updates, monitoring, and logging section, choose Edit. Under Platform software → Environment properties, add the following variables:
| Variable | Value |
|---|---|
OTEL_PHP_AUTOLOAD_ENABLED | true |
OTEL_SERVICE_NAME | <your-service-name> |
OTEL_EXPORTER_OTLP_ENDPOINT | http://localhost:9320 |
These variables configure OpenTelemetry instrumentation for your Laravel application.
OTEL_PHP_AUTOLOAD_ENABLEDenables the OpenTelemetry PHP auto-loader.OTEL_SERVICE_NAMEidentifies the application in Middleware.OTEL_EXPORTER_OTLP_ENDPOINTsends telemetry to the Middleware Agent running locally.
Save the environment properties.
3 Enable frontend and backend trace correlation (optional)#
If you use Middleware RUM to monitor your frontend application, configure trace propagation to connect frontend requests with backend traces.
Add the following environment variable in Elastic Beanstalk:
1OTEL_PROPAGATORS=baggage,tracecontext,b3multiThis enables W3C and B3 multi-header propagation for distributed tracing.
Make sure your frontend application is also configured to propagate compatible trace headers.
4 Enable Laravel trace-related metrics (optional)#
Laravel tracing is enabled automatically when the instrumentation installer runs.
To collect additional trace-related metrics, update your Laravel application configuration.
1. Register the service provider
Open config/app.php and add the Middleware service provider to the providers array:
1'providers' => [
2 // ...
3 Middleware\LaravelApm\LaravelApmServiceProvider::class,
4],2. Add the metrics middleware
Open app/Http/Kernel.php and add the following line at the end of the $middleware array:
1protected $middleware = [
2 // ...
3 \Middleware\LaravelApm\Middleware\MetricsMiddleware::class,
4];Laravel 11+ applications do not have app/Http/Kernel.php.
1. Register the service provider
In bootstrap/providers.php, add the service provider:
1return [
2 App\Providers\AppServiceProvider::class,
3 Middleware\LaravelApm\LaravelApmServiceProvider::class,
4];2. Add the metrics middleware
In bootstrap/app.php, add the metrics middleware:
1->withMiddleware(function (Middleware $middleware) {
2 $middleware->append(\Middleware\LaravelApm\Middleware\MetricsMiddleware::class);
3})5 Deploy the application#
Deploy your updated Laravel application to AWS Elastic Beanstalk using your existing deployment method.
During deployment, Elastic Beanstalk executes the commands in 99-mwinstrumentation.config to:
- Install the Middleware Agent.
- Configure Laravel instrumentation.
- Start the application with the configured OpenTelemetry environment variables.
After deployment, verify that your Elastic Beanstalk environment is healthy.
6 Verify data collection#
- Open your deployed Laravel application.
- Generate a few requests to application endpoints.
- Log in to Middleware.
- Go to APM → Services.
- Find the service configured using
OTEL_SERVICE_NAME. - Open the service to view traces, latency, and errors.
You can also verify collected telemetry in:
- APM → Traces: View individual application requests.
- Log Monitoring: View collected application logs.
- Infrastructure Monitoring → Hosts: View host metrics from the Elastic Beanstalk EC2 instances.
Allow a few minutes for telemetry to appear after deployment.
Environment variables reference#
| Variable | Required | Description |
|---|---|---|
MW_API_KEY | Yes | Middleware API key used during Agent installation |
MW_TARGET | Yes | Middleware ingestion endpoint |
OTEL_PHP_AUTOLOAD_ENABLED | Yes | Enables OpenTelemetry PHP automatic instrumentation |
OTEL_SERVICE_NAME | Yes | Service name displayed in Middleware |
OTEL_EXPORTER_OTLP_ENDPOINT | Yes | Local Middleware Agent OTLP endpoint |
OTEL_PROPAGATORS | No | Configures distributed trace context propagation |
Example configuration#
1MW_TARGET=https://<MW_UID>.middleware.io:443
2OTEL_PHP_AUTOLOAD_ENABLED=true
3OTEL_SERVICE_NAME=laravel-production
4OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9320
5OTEL_PROPAGATORS=baggage,tracecontext,b3multiThe Middleware API key should be supplied securely during Agent installation.
Troubleshooting#
Middleware Agent is not installed#
If the Agent is missing after deployment:
- Check
/var/log/cfn-init-cmd.logfor the output of the01_install_agentcommand, and/var/log/eb-engine.logfor deployment errors. You can also download them from the console: Logs → Request logs → Full logs. - Verify that the Elastic Beanstalk instances can download the installation script.
- Confirm that the Middleware API key and target URL are correct.
- Check outbound HTTPS connectivity to your Middleware endpoint.
OpenTelemetry PHP extension is missing#
If Laravel instrumentation does not initialize, verify that the OpenTelemetry PHP extension is installed:
1php -m | grep opentelemetryIf no output is returned:
- Check the output of
02_run_instrumentationin the Elastic Beanstalk deployment logs. - Verify that the instrumentation installer completed successfully.
- Confirm that the extension is enabled in the PHP configuration used by your application.
No traces in Middleware#
If your Laravel application is running but traces do not appear:
- Verify that
OTEL_PHP_AUTOLOAD_ENABLEDis set totrue. - Confirm that
OTEL_SERVICE_NAMEis configured. - Check that
OTEL_EXPORTER_OTLP_ENDPOINTpoints tohttp://localhost:9320. - Verify that the Middleware Agent is running.
- Generate requests to actual application routes, since health check endpoints may be excluded.
- Restart or redeploy the application after updating environment variables.
Frontend and backend traces are not connected#
If Middleware RUM and Laravel traces appear separately:
- Verify that
OTEL_PROPAGATORSincludesbaggage,tracecontext,b3multi. - Confirm that the frontend SDK propagates the required trace headers.
- Check that proxies and load balancers forward the tracing headers.
Instrumentation stops working after deployment#
If telemetry collection stops after an Elastic Beanstalk update or instance replacement:
- Verify that
.ebextensions/99-mwinstrumentation.configis included in the application deployment package. - Check the Elastic Beanstalk deployment logs for installation errors.
- Avoid installing or modifying Middleware manually over SSH, as these changes may be lost when instances are replaced.
Application cannot connect to Middleware#
If the Agent reports connection or export errors:
- Verify that the Middleware target URL is correct.
- Confirm that the EC2 instances have outbound HTTPS access on port 443.
- Check VPC routing, NAT Gateway configuration, and security group rules.
- Verify that the local OTLP endpoint is accessible on port 9320.
If the issue persists, contact Middleware Support at [email protected].
Need assistance or want to learn more about Middleware? Get in touch with us via our Contact Us or join our Slack channel.
What did you think of this content?