Interface UserDataBuilder

All Known Implementing Classes:
UserDataBuilderImpl

public interface UserDataBuilder
Builder interface for constructing EC2 UserData scripts.

This interface provides infrastructure-level helpers for common EC2 setup tasks (storage mounting, CloudWatch configuration) while allowing applications to inject their specific installation and configuration commands.

The UserDataBuilder abstracts away the complexity of bash scripting for:

  • System updates (dnf/yum compatibility)
  • EFS mounting with proper error handling
  • EBS device detection (handles both /dev/xvdh and /dev/nvme1n1)
  • CloudWatch Agent installation and configuration
  • File ownership and permissions

Applications can focus on their specific installation logic while the infrastructure handles platform concerns.

See Also:
  • Method Details

    • addSystemUpdate

      void addSystemUpdate()
      Add system update commands (handles both dnf and yum). Automatically detects whether to use dnf (Amazon Linux 2023) or yum (Amazon Linux 2).
    • installCloudWatchAgent

      void installCloudWatchAgent(String logGroupName, List<String> logFilePaths)
      Install and configure CloudWatch Agent for log streaming.

      This method:

      • Downloads and installs the CloudWatch Agent
      • Configures log file collection for the specified paths
      • Starts the agent service
      Parameters:
      logGroupName - The CloudWatch Logs group name (e.g., "/aws/jenkins/mystack/ec2/dev")
      logFilePaths - List of absolute paths to log files to monitor
    • mountEfs

      void mountEfs(String efsId, String accessPointId, String mountPath, String uid, String gid)
      Mount EFS filesystem with IAM authentication.

      This method:

      • Installs amazon-efs-utils
      • Creates the mount directory
      • Adds fstab entry with TLS and IAM auth
      • Mounts the filesystem
      • Sets ownership to the specified uid:gid
      Parameters:
      efsId - The EFS filesystem ID (e.g., "fs-12345678")
      accessPointId - The EFS access point ID (e.g., "fsap-12345678")
      mountPath - The local mount path (e.g., "/var/lib/jenkins")
      uid - The user ID for ownership (e.g., "1000")
      gid - The group ID for ownership (e.g., "1000")
    • mountEbs

      void mountEbs(String deviceName, String mountPath, String uid, String gid)
      Format and mount EBS volume.

      This method:

      • Detects the EBS device (handles both /dev/xvdh and /dev/nvme1n1)
      • Formats the device with XFS filesystem
      • Creates the mount directory
      • Adds fstab entry
      • Mounts the filesystem
      • Sets ownership to the specified uid:gid
      Parameters:
      deviceName - The EBS device name (e.g., "/dev/xvdh")
      mountPath - The local mount path (e.g., "/var/lib/jenkins")
      uid - The user ID for ownership (e.g., "1000")
      gid - The group ID for ownership (e.g., "1000")
    • addCommands

      void addCommands(String... commands)
      Add custom commands to the UserData script. Commands are executed in the order they are added.
      Parameters:
      commands - One or more shell commands to execute
    • addCommand

      void addCommand(String command)
      Add a single custom command to the UserData script.
      Parameters:
      command - Shell command to execute