Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy EB through AWS SDK

My current deployment process:

  1. Create zip file from code
  2. Upload code to EB instance with EB CLI and .elasticbeanstalk/config.yml I specify application name, environment name and artifact (zip file) in config.yml.

Can i deploy code to EB instance using only AWS SDK (nodeJS)?

like image 755
Vitaly Senko Avatar asked Dec 03 '25 12:12

Vitaly Senko


1 Answers

Found myself. You need to upload ZIP file to S3. Then you need to create application version for EB Application (With unique label and S3Key where ZIP file is). Then you need to update EB environment with new Versionlabel (and don't forget to specify Node start script if needed).

Maybe this code will be helpful for someone:

var aws = require('aws-sdk');
var s3 = new aws.S3();
var eb = new aws.ElasticBeanstalk();

var zipFileName = 'myCodeZipArchive.zip';
var appName = 'app-name';
var envName = 'env-name';
var s3bucket = 'my-app-source-bucket';

var label = `${appName}_${envName}_${new Date().toISOString()}`;

s3.upload({
  Bucket: s3bucket,
  Key: label,
  Body: fs.createReadStream(zipFileName)
}).promise().then(() => eb.createApplicationVersion({
  ApplicationName: appName,
  VersionLabel: label,
  SourceBundle: {
    S3Bucket: s3bucket,
    S3Key: label
  }
}).promise()).then(() => eb.updateEnvironment({
  ApplicationName: appName,
  EnvironmentName: envName,
  OptionSettings: [{
    Namespace: 'aws:elasticbeanstalk:container:nodejs',
    OptionName: 'NodeCommand',
    Value: 'npm start'
  }],
  VersionLabel: label
}).promise());
like image 121
Vitaly Senko Avatar answered Dec 05 '25 02:12

Vitaly Senko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!