P4 Code Review Packer Template¶
This Packer template creates an Amazon Machine Image (AMI) for P4 Code Review (Helix Swarm) on Ubuntu 24.04 LTS. The AMI includes all necessary software pre-installed, with runtime configuration handled automatically during instance launch.
Table of Contents¶
- Prerequisites
- Quick Start
- What Gets Installed
- Building the AMI
- Finding Your AMI
- Next Steps
- Troubleshooting
Prerequisites¶
Before building the AMI, ensure you have:
- AWS CLI configured with valid credentials:
aws configure
# Verify access
aws sts get-caller-identity
- Packer installed (version >= 1.8.0):
packer version
If not installed, download from https://www.packer.io/downloads
- VPC Access:
- Default VPC in your region (Default behaviour)
-
OR custom VPC with a public subnet (Can be configured by passing the VPC id through the
vpc_idvariable) -
IAM Permissions: Your AWS credentials need permissions to:
- Launch EC2 instances
- Create AMIs
- Create/delete security groups
- Create/delete key pairs
Quick Start¶
From the repository root, run:
# 1. Navigate to Packer template directory
cd assets/packer/perforce/p4-code-review
# 2. Initialize Packer (downloads required plugins)
packer init p4_code_review_x86.pkr.hcl
# 3. Validate the template
packer validate p4_code_review_x86.pkr.hcl
# 4. Build the AMI (takes ~10-15 minutes)
packer build p4_code_review_x86.pkr.hcl
At the end of the build, Packer will output the AMI ID:
==> amazon-ebs.ubuntu2404: AMI: ami-0abc123def456789
Save this AMI ID - you'll need it for Terraform deployment.
What Gets Installed¶
The AMI includes a complete P4 Code Review installation:
Software Components¶
- Perforce Repository: Official Perforce package repository (Ubuntu jammy/22.04 compatible)
- PHP 8.x: PHP runtime with all required extensions:
- Core: curl, mbstring, xml, intl, ldap, bcmath
- Database: mysql
- Graphics: gd
- Archive: zip
- PECL: igbinary, msgpack, redis
- Helix Swarm: Native DEB installation via
helix-swarmpackage - Apache2: Web server with mod_php and required modules (rewrite, proxy, proxy_fcgi)
- PHP-FPM: FastCGI Process Manager for PHP
- helix-swarm-optional (optional, installed by default): LibreOffice for document preview (.docx, .xlsx, .pptx) and ImageMagick for image preview (.png, .jpg, .tiff, etc.) (~500MB)
- AWS CLI v2: Required for Secrets Manager access and EBS volume operations at runtime
- Configuration Script:
/home/ubuntu/swarm_scripts/swarm_instance_init.shfor runtime setup (see Runtime Configuration Details below)
System Configuration¶
- AppArmor: Ubuntu's security module (less restrictive by default for
/opt) - Services: Apache2 and PHP-FPM enabled for automatic startup
- User:
swarmsystem user created with proper permissions - Directories:
/opt/perforce/swarmprepared with correct ownership
What's NOT Configured Yet¶
The following are configured at deployment when you launch an instance:
- P4 Server connection details
- P4 user credentials (fetched from AWS Secrets Manager)
- Redis cache connection
- External hostname/URL
- SSO settings
- EBS volume mounting for persistent data
- Queue worker configuration (cron job and endpoint)
- File permissions for worker processes
- P4 Server extension installation (Swarm triggers)
Runtime Configuration Details¶
When an EC2 instance launches, the user-data script performs the following steps:
- EBS Volume Attachment: Finds and attaches the persistent data volume by tags
- Filesystem Setup: Creates ext4 filesystem (first launch) or mounts existing one
- Swarm Configuration: Executes
/home/ubuntu/swarm_scripts/swarm_instance_init.shwhich: - Retrieves P4 credentials from AWS Secrets Manager
- Runs Perforce's official
configure-swarm.shto:- Connect to P4 Server and validate credentials
- Install Swarm extension on P4 Server (enables event triggers)
- Create initial configuration file
- Set up Apache VirtualHost
- Create cron job for queue workers
- Configures file permissions for queue worker functionality
- Updates configuration with Redis connection details
- Configures queue workers to use localhost endpoint
- Starts Apache and PHP-FPM services
Queue Workers: P4 Code Review requires background workers to process events, send notifications, and index files. These are spawned by a cron job (created by configure-swarm.sh) that runs every minute. The runtime configuration ensures workers have proper permissions and connect to the correct endpoint.
Building the AMI¶
Option 1: Using Default VPC (Recommended)¶
If your AWS region has a default VPC:
cd assets/packer/perforce/p4-code-review
packer init p4_code_review_x86.pkr.hcl
packer build p4_code_review_x86.pkr.hcl
Option 2: Using Custom VPC¶
If you don't have a default VPC, specify your own:
packer build \
-var="region=us-west-2" \
-var="vpc_id=vpc-xxxxx" \
-var="subnet_id=subnet-xxxxx" \
-var="associate_public_ip_address=true" \
-var="ssh_interface=public_ip" \
p4_code_review_x86.pkr.hcl
Requirements for custom VPC:
- Subnet must be in a public subnet (has route to Internet Gateway)
associate_public_ip_address=trueif subnet doesn't auto-assign public IPs- Security group allows outbound internet access (for package downloads)
Option 3: Using Variables File¶
Create a my-vars.pkrvars.hcl:
region = "us-west-2"
vpc_id = "vpc-xxxxx"
subnet_id = "subnet-xxxxx"
associate_public_ip_address = true
ssh_interface = "public_ip"
Then build:
packer build -var-file="my-vars.pkrvars.hcl" p4_code_review_x86.pkr.hcl
Build Output¶
Successful build output looks like:
==> amazon-ebs.ubuntu2404: Stopping the source instance...
==> amazon-ebs.ubuntu2404: Waiting for the instance to stop...
==> amazon-ebs.ubuntu2404: Creating AMI p4_code_review_ubuntu-20231209123456 from instance i-xxxxx
==> amazon-ebs.ubuntu2404: AMI: ami-0abc123def456789
==> amazon-ebs.ubuntu2404: Waiting for AMI to become ready...
==> amazon-ebs.ubuntu2404: Terminating the source AWS instance...
Build 'amazon-ebs.ubuntu2404' finished after 12 minutes 34 seconds.
==> Wait completed after 12 minutes 34 seconds
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs.ubuntu2404: AMIs were created:
us-west-2: ami-0abc123def456789
Copy the AMI ID (e.g., ami-0abc123def456789) - you'll need this for Terraform.
Finding Your AMI¶
List All P4 Code Review AMIs¶
aws ec2 describe-images \
--owners self \
--filters "Name=name,Values=p4_code_review_ubuntu-*" \
--query 'Images[*].[ImageId,Name,CreationDate]' \
--output table
Output:
+-----------------------------------------------------------------------+
| DescribeImages |
+----------------------+---------------------------------------+--------+
| ami-0abc123def456 | p4_code_review_ubuntu-20231209 | 2023...|
| ami-0def456abc789 | p4_code_review_ubuntu-20231208 | 2023...|
+----------------------+---------------------------------------+--------+
Get the Latest AMI¶
aws ec2 describe-images \
--owners self \
--filters "Name=name,Values=p4_code_review_ubuntu-*" \
--query 'Images | sort_by(@, &CreationDate) | [-1].[ImageId,Name,CreationDate]' \
--output table
Get Details About a Specific AMI¶
aws ec2 describe-images --image-ids ami-0abc123def456789
Next Steps¶
Now that you have an AMI, proceed to deploy P4 Code Review infrastructure:
-
Read the P4 Code Review Module Documentation
-
Follow the deployment guide in the module README, which covers:
- Creating AWS Secrets Manager secrets for P4 credentials
- Writing Terraform configuration
- Deploying the infrastructure
- Accessing the P4 Code Review web console
Troubleshooting¶
"No default VPC available"¶
Error: Packer fails with "No default VPC for this user"
Solution: Use Option 2 or 3 above to specify your VPC and subnet:
packer build \
-var="vpc_id=vpc-xxxxx" \
-var="subnet_id=subnet-xxxxx" \
p4_code_review_x86.pkr.hcl
"Unable to connect to instance"¶
Error: Packer times out connecting to the instance
Possible causes:
- Subnet is not public (no route to Internet Gateway)
- Security group blocks SSH (port 22)
- No public IP assigned to instance
Solution: Verify your subnet has:
# Check if subnet has route to IGW
aws ec2 describe-route-tables \
--filters "Name=association.subnet-id,Values=subnet-xxxxx" \
--query 'RouteTables[*].Routes[?GatewayId!=`local`]'
"Package installation failed"¶
Error: APT/DEB errors during build
Possible causes:
- No internet access from instance
- Perforce repository temporarily unavailable
- Package version conflicts
Solution:
- Check build instance has outbound internet access
- Try rebuilding (temporary outages resolve themselves)
- Review
/var/log/swarm_setup.logon build instance
"AMI already exists with that name"¶
Error: "AMI name 'p4_code_review_ubuntu-TIMESTAMP' already exists"
This shouldn't happen (timestamp should be unique), but if it does:
# List your AMIs
aws ec2 describe-images --owners self \
--filters "Name=name,Values=p4_code_review_ubuntu-*"
# Deregister old AMI if no longer needed
aws ec2 deregister-image --image-id ami-xxxxx
Build is slow¶
Normal build time: 10-15 minutes
If taking longer:
- Package downloads can be slow depending on region
- Perforce repository might be experiencing high load
- This is normal - be patient
Need to debug the build?¶
Enable debug mode to step through each provisioner:
packer build -debug p4_code_review_x86.pkr.hcl
This will pause before each provisioner step, allowing you to:
- SSH into the build instance
- Inspect the current state
- Verify installation progress
- Press Enter to continue to the next step
Enable detailed logging:
PACKER_LOG=1 packer build p4_code_review_x86.pkr.hcl
Additional Resources¶
Questions or Issues?¶
If you encounter problems:
- Check the troubleshooting section above
- Review Packer logs with
PACKER_LOG=1 - Use
packer build -debugto step through the build process - Verify AWS credentials and permissions