如何使用Elastic Beanstalk为AWS CDK设置环境变量?

时间:2019-10-08 08:07:41

标签: node.js amazon-cloudformation amazon-elastic-beanstalk aws-cdk

我想设置一个环境变量,该变量在使用AWS CDK部署到Elastic Beanstalk的Nodejs应用程序中可用。我曾尝试将其添加到Environment选项中,但是直到我手动取消堆栈更新之前,部署都停留了几分钟。

根据docs from AWS,这是我尝试添加环境变量的方式:

      {
        namespace: 'aws:elasticbeanstalk:application:environment',
        optionName: 'CORS_ALLOW_ORIGIN',
        value:
          'http://my-frontend.s3-website.eu-central-1.amazonaws.com'
      }

我也尝试使用'http...'和'“ http ...”'添加值,但没有成功。

这是完整的脚本:

#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import ebs = require('@aws-cdk/aws-elasticbeanstalk');

export class MyPrototype extends cdk.Stack {
  public constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const appName = 'MyPrototypeApp';
    const platform = this.node.tryGetContext('platform');

    const app = new ebs.CfnApplication(this, 'Application', {
      applicationName: appName
    });

    const options: ebs.CfnEnvironment.OptionSettingProperty[] = [
      {
        namespace: 'aws:autoscaling:launchconfiguration',
        optionName: 'IamInstanceProfile',
        value: 'aws-elasticbeanstalk-ec2-role'
      },
      {
        namespace: 'aws:elasticbeanstalk:container:nodejs',
        optionName: 'NodeVersion',
        value: '10.15.0'
      },
      {
        namespace: 'aws:elasticbeanstalk:application:environment',
        optionName: 'CORS_ALLOW_ORIGIN',
        value:
          'http://my-frontend.s3-website.eu-central-1.amazonaws.com'
      }
    ];

    new ebs.CfnEnvironment(this, 'Environment', {
      environmentName: 'MyPrototypeEnvironment',
      applicationName: app.applicationName || appName,
      platformArn: platform,
      optionSettings: options
    });
  }
}

const app = new cdk.App();

new MyPrototype(app, 'ElasticBeanstalk');

app.synth();

1 个答案:

答案 0 :(得分:0)

不确定为什么会失败,因为这是在 optionSettings 中设置环境变量的方式,使用“aws:elasticbeanstalk:application:environment”作为命名空间(您也可以添加多个具有相同名称的命名空间)