How to config Meteor on AWS/EBS using METEOR_SETTINGS environment variable

后端 未结 3 1030
夕颜
夕颜 2020-12-14 12:55

Trying to set up a Meteor on an AWS/EBS (Amazon Web Services, Elastic Beanstalk) environment.

A Meteor dev-run can be passed a command line flag: --settings s

3条回答
  •  感动是毒
    2020-12-14 13:17

    After discussing this issue with AWS support I realized that AWS/EBS does not support storing JSON in environment variables. This is because the environment variables are stored as key/value strings in unencoded JSON (apparently, in CloudFormation). The bottom line here a bit disappointing:

    METEOR_SETTINGS cannot be used in the AWS/EBS console

    This is indeed unfortunate, however there are a couple of workarounds.

    1st Workaround

    Move the json configs into an s3 bucket and place the following content in a .ebextensions/app.config file:

    container_commands: 
      01_setvariable: 
        command: "aws s3 cp s3:///nodejs.conf /tmp/deployment/config/#etc#init#nodejs.conf 
    

    This will entirely override /etc/init/nodejs.conf with content retrieved from your s3 bucket. Naturally there's an opportunity to set/override individual settings using fine-tuned/fancy bash scripting.

    I ended up not choosing this method, because it involves another entity (an S3 bucket) and the dev iteration requires a new version deploy, which isn't terribly fast.

    2nd Workaround

    Note: this is a simple code-hack I came up with. It seems to put all this mess behind while not requiring much effort.

    My original need was to propagate AWS/EBS env vars to the client, so I decided to bypass the METEOR_SETTINGS variable and populate Meteor.settings.public directly with env vars from node's process.env space. The whitelisting is managed by a simple list. Add a server/lib/config.js file with:

    Meteor.startup(function () {
        // public settings that need to be exposed to the client can be added here
        var publicEnvs = {
            S3_PATH: 's3path'
        };
        var modified;
        _.each(publicEnvs, (value, key) => {
            let envValue = process.env[key];
            if (envValue) {
                Meteor.settings.public[value] = envValue;
                modified = true;
            }
        });
        if (modified) {
            __meteor_runtime_config__.PUBLIC_SETTINGS = Meteor.settings.public;
        }
    });
    

    Hurray, your client can access the env vars of your choice!

    For example with this change, an S3_PATH environment variable defined in the EBS console can be accessed as Meteor.settings.public.s3path on the client. Quite simple, and without many moving parts :)

提交回复
热议问题