Search code examples
linuxlaravelamazon-web-servicesamazon-elastic-beanstalkamazon-linux-2

How to auto quote content after character on each line in Linux? (Elastic Beanstalk environment variables related)


Consider the following file:

APP_ENV=production
APP_NAME=Some API <- This line HERE
RDS_DB_PASSWORD=Some_Strong_Password
// etc...

This file is auto-generated by AWS Elastic Beanstalk for my environments i.e. I have no control over the formatting of the contents.

When the application runs, it passes the environment variables internally and this works fine. However, when I try to run Laravel commands like the following, it does not escape the contents of each variable:

export $(sudo cat /opt/elasticbeanstalk/deployment/env) && sudo -E -u webapp php artisan some-command

As a result, the value Some API gets passed as Some instead because it has not been wrapped with quotes.

Is there a way to insert quotes around the values that come after the first = in this file on the fly and then pass them to my web app? Alternatively, am I running my commands incorrectly? Given this is Laravel specific, there are no docs on how to run Laravel commands on Elastic Beanstalk running Amazon Linux 2.


Solution

  • I managed to solve this by exporting the variables to my profile upon application deployment like so:

    commands:
        setvars:
            command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local
    

    See reference here.