简体   繁体   English

使用 Node.js SDK 构建 Elastic Beanstalk

[英]Building Elastic Beanstalk with Node.js SDK

Has anyone created an elastic beanstalk application with the AWS javascript sdk?有没有人用 AWS javascript sdk 创建过弹性 beanstalk 应用程序? I've been able to update existing applications using grunt, that works really well.我已经能够使用 grunt 更新现有的应用程序,效果非常好。 But as part of a continuous integration/continuous deployment project, we want to also create the app when it's not there.但是作为持续集成/持续部署项目的一部分,我们还希望在应用程序不存在时创建它。 I find the documentation confusing, and in AWS's usual fashion, lacking in any kind of cohesive examples, that say, "do this, then this."我发现文档令人困惑,并且以 AWS 的惯常方式,缺乏任何类型的有凝聚力的示例,比如“做这个,然后这个”。 If anyone has done this and can point me in the right direction, that would be a great help.如果有人这样做并且可以为我指明正确的方向,那将是一个很大的帮助。 At this point in time, I'm not sure whether it's a single step or multi step process.目前,我不确定这是单步还是多步过程。

So, here's a basic node package to build an app.所以,这里有一个基本的节点包来构建一个应用程序。 I have uploaded a basic API app as a zip file, it doesn't much of anything.我已经上传了一个基本的 API 应用程序作为一个 zip 文件,它没有什么。 The idea is that once it's created, I can then update it using a grunt script - there are a couple of very good grunt modules that will do that, once it's created.这个想法是,一旦它被创建,我就可以使用 grunt 脚本更新它——一旦它被创建,有几个非常好的 grunt 模块可以做到这一点。 But the initial creation was missing.但最初的创作却不见了。 Easy enough to add on more parameters to this now, too.现在也很容易添加更多参数。

 var applicationName = process.argv[2];
 var environmentName = process.argv[3];
 var regionName = process.argv[4];

 var AWS = require('aws-sdk');
 AWS.config.update({region: regionName});

 var applicationParams = {
   ApplicationName: applicationName
 };

 var environmentParams =
 {
     ApplicationName: applicationName, /* required */
     EnvironmentName: environmentName, /* required */
     VersionLabel: 'initial',
     SolutionStackName: "64bit Amazon Linux 2015.03 v1.4.4 running     Node.js",
     CNAMEPrefix: applicationName,
     Tier:
     {
         Version: " ",
         Type: "Standard",
         Name: "WebServer"
     },
     OptionSettings:
     [
         {
             Namespace: 'aws:elasticbeanstalk:environment',
             OptionName: 'EnvironmentType',
             Value: 'SingleInstance'
         },
         {
             Namespace: 'aws:autoscaling:launchconfiguration',
             OptionName: 'EC2KeyName',
             Value: 'MyPemFile'
         },
         {
             Namespace: 'aws:autoscaling:launchconfiguration',
             OptionName: 'IamInstanceProfile',
             Value: 'aws-elasticbeanstalk-ec2-role'
         },
         {
             Namespace: 'aws:autoscaling:launchconfiguration',
             OptionName: 'InstanceType',
             Value: 't1.micro'
         }
     ],
  };

 var versionParams =
 {
     ApplicationName: applicationName, /* required */
     VersionLabel: 'initial', /* required */
     AutoCreateApplication: true,
     SourceBundle:
     {
         S3Bucket: 'beanstalk-test-ff',
         S3Key: 'test-app.zip'
     }
 };

 var elasticbeanstalk = new AWS.ElasticBeanstalk();

 elasticbeanstalk.createApplication(applicationParams, function(err, data)
 {
     console.log('Creating application');
     console.log(data);
     if (err)
     {
         if (err.message.indexOf("already exists") > -1)
         {
             console.log('Application already exists, continuing on');
         }
         else
         {
             console.log(err,err.stack); // an error occurred
         }
     }
     else
     {
         elasticbeanstalk.createApplicationVersion(versionParams, function(err, data)
         {
             console.log('Creating application version....');
             console.log(data);

             if (err) console.log(err, err.stack); // an error occurred

             else
             {
                 elasticbeanstalk.createEnvironment(environmentParams, function(err, data)
                 {
                     console.log('Creating application environment....');
                     console.log(data);
                     if (err) console.log(err, err.stack); // an error occurred

                 });
             }
         });
     }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM