Overview
Since Lambda functions are stateless and short-lived, you cannot run the Nightwatch Agent directly within your Vapor application. Instead, you will need a long-running compute instance in the same network to host the Nightwatch Agent.We are building first-party support for this setup directly into Vapor. Until
that’s available, you can provision an EC2 instance to run the Nightwatch
Agent either using Laravel Forge, or manually in the AWS console.
Architecture
Your setup will consist of three components:- EC2 Instance: A long-running server to host the Nightwatch Agent
- Vapor Application: Your serverless Laravel app configured to communicate with the EC2-hosted agent
- Vapor Network: To allow private communication between your Vapor application and the Nightwatch Agent
Forge Installation (recommended)
Step 1: Provision a Forge Worker
- In the Forge dashboard, select Create Server -> AWS, and then configure as follows:
Setting | Value |
---|---|
Type | Worker Server |
Region | Same as your Vapor application |
Server Size | t3.micro (the Nightwatch Agent is lightweight, and should not require a large instance) |
Private Network | The Vapor network to which your Vapor application is connected |
- Leave the other default settings, and select Create Server
Step 2: Add a Site on Your Worker
- On your newly created worker server, select Sites -> Add Site using the default settings
- Set the application repo and branch to mirror those used in the Vapor application
- Ensure that Install composer dependencies is checked, then select Install Repository
Do not enable Nightwatch under the newly created Application’s Laravel
section, as it will interfere with the configuration needed for Vapor.
- In your newly created site, Select Environment and add the following lines:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
is your Nightwatch token, and 10.0.0.x
is the private IP address of your worker server (you can find this under the server’s name above the Environment dialogue).
- Select Deploy Now
While creating a Site using your application source code is not strictly
necessary, it offers a simple way to install the Nightwatch Agent, and helps
ensure that the version matches that of the Nightwatch package used in your
Vapor deployment.
Step 3: Configure a Daemon to run the Nightwatch Agent
-
Noting the name of your newly created site, return to your server and select Daemons, then configure as follows:
Setting Value Command php artisan nightwatch:agent
Directory /home/forge/YOUR_SITE_NAME
(whereYOUR_SITE_NAME
is the name of the site you created in Step 2) - Leave the other default settings, and select Create
- Select Network, and create a new firewall rule with the following settings:
- Port:
2407
- From IP Address:
10.0.0.0/21
- Rule Type:
Allow
If your Vapor Network is configured with a custom IP range, you will need to
adjust the From IP Address setting accordingly.
Manual Installation (Coming Soon)
We are still working on a manual EC2 setup guide - watch this space!
Configure Your Vapor Application
Step 1: Install Nightwatch in Your Laravel App
Add Nightwatch to your Laravel application:Step 2: Configure Environment Variables
In your Vapor environment configuration, add the following variables:10.0.0.x
is the private IP address of your worker server
Step 3: Verify Network Configuration
In yourvapor.yml
file ensure that the network:
directive matches the Vapor network which you specified when creating your Forge worker server. This ensures that your Vapor application can communicate with the Nightwatch Agent running on that worker.
Step 4: Synchronize Vapor Deployment With EC2 Instance
The Nightwatch Agent version running on your EC2 instance must be kept in sync with the Nightwatch package version in your Vapor application. Version mismatches will prevent the agent from ingesting data. If you are using Forge, we recommend pinging Forge’s Deployment Trigger URL for your site when deploying with Vapor. To find the Deployment Trigger URL, visit the site you created and select the Deployments tab. There you can copy the Deployment Trigger URL. You may then usecurl
to ping the Deployment Trigger URL during the deploy step in the vapor.yml
:
Important Considerations
Verify Network Configuration
You must ensure that thenetwork:
directive in your vapor.yml
file matches the network specified when creating your Forge worker server. This ensures that the Vapor application can communicate with the Nightwatch Agent running on your Forge worker.
You must also ensure that the firewall rule created in the Forge worker allows traffic from the Vapor network to the Nightwatch Agent port (default 2407
).
Multiple Vapor Applications/Environments
If you wish to monitor multiple Vapor applications, you may run multiple agents on the same Forge worker by creating a separate Site and Daemon for each application, setting a unique port in theNIGHTWATCH_INGEST_URI
environment variables in Forge/Vapor, and adding the respective firewall rules in Forge. Remember also to ensure that the NIGHTWATCH_TOKEN
specified in each Forge Site is correct for the corresponding Vapor application/environment.