Search code examples
amazon-web-servicessymfonyamazon-elastic-beanstalksystemd

Create systemd service in AWS Elastic Beanstalk on new Amazon Linux 2


I'm currently trying to create a worker on AWS Elastic Beanstalk which is pulling messages from a specific SQS queue (with the help of the Symfony messenger). I don't want to use dedicated worker instances for this task. After some research, I found out that systemd can help here which is enabled by default on the new Amazon Linux 2 instances.

However, I'm not able to create a running systemd service. Here is my .ebextensions/03_workers.config file:

files:
    /etc/systemd/system/my_worker.service:
        mode: "000755"
        owner: root
        group: root
        content: |
            [Unit]
            Description=My worker

            [Service]
            User=nginx
            Group=nginx
            Restart=always
            ExecStart=/usr/bin/nohup /usr/bin/php /var/app/current/bin/console messenger:consume integration_incoming --time-limit=60

            [Install]
            WantedBy=multi-user.target

services:
    systemd:
        my_worker:
            enabled: "true"
            ensureRunning: "true"

I can't see my service running if I'm running this command:

systemctl | grep my_worker

What am I doing wrong? :)


Solution

  • systemd is not supported in Services. The only correct is sysvinit:

    services:
      sysvinit:
        my_worker:
          enabled: "true"
          ensureRunning: "true"
    

    But I don't think it will even work, as this is for Amazon Linux 1, not for Amazon Linux 2.

    In Amazon Linux 2 you shouldn't be even using much of .ebextensions. AWS docs specifically write:

    On Amazon Linux 2 platforms, instead of providing files and commands in .ebextensions configuration files, we highly recommend that you use Buildfile. Procfile, and platform hooks whenever possible to configure and run custom code on your environment instances during instance provisioning.

    Thus, you should consider using Procfile which does basically what you want to achieve:

    Use a Procfile for long-running application processes that shouldn't exit. Elastic Beanstalk expects processes run from the Procfile to run continuously. Elastic Beanstalk monitors these processes and restarts any process that terminates. For short-running processes, use a Buildfile.

    Alternative

    Since you already have created a unit file /etc/systemd/system/my_worker.service for systemd, you can enable and start it yourself.

    For this container_commands in .ebextensions can be used. For example:

    container_commands:
       10_enable_worker:
         command: systemctl enable worker.service
       20_start_worker:
         command: systemctl start worker.service