AWS SDK EC2 Deploy via Method


#1

I’m trying to create an EC2 instance from a server method. I’m using https://atmospherejs.com/eluck/aws-sdk.

I have a server method that has this as its contents [Example AWS NodeJS SDK EC2 Deploy]:

AWS.config.region = 'eu-west-1';

var ec2 = new AWS.EC2();

var params = {
  ImageId: 'ami-1624987f', // Amazon Linux AMI x86_64 EBS
  InstanceType: 't1.micro',
  MinCount: 1, MaxCount: 1
};

// Create the instance
ec2.runInstances(params, function(err, data) {
  if (err) { console.warn("Could not create instance", err); return; }

  var instanceId = data.Instances[0].InstanceId;
  console.warn("Created instance", instanceId);

  // Add tags to the instance
  params = {Resources: [instanceId], Tags: [
    {Key: 'Name', Value: instanceName}
  ]};
  ec2.createTags(params, function(err) {
    console.warn("Tagging instance", err ? "failure" : "success");
  });
});

I also have environment settings:

if (Meteor.settings.AWS) {
  AWS.config.update({
    AWS_ACCESS_KEY_ID: Meteor.settings.AWS.accessKeyId,
    AWS_SECRET_ACCESS_KEY: Meteor.settings.AWS.secretAccessKey
  });
  console.warn("AWS working");
} else {
  console.warn("AWS settings missing");
}

Nothing actually deploys, does anyone have any experience or idea why that this might happening?


#2

Good question. Interested in hearing the answers to this.

I assume instanceName is being passed as one of the parameters of the method call.


#3

instanceName wasn’t being passed, thanks for that catch, I’m changed it to doc.name, using a form to enter the variables and parsing the data via doc.


#4

Still doesn’t work though :slight_smile:


#5

No, that’s in the callback, so wouldn’t have any effect on whether the instance is created or not. But it would have produced an error if the callback ran (I’m guessing the callback function wasn’t being called while testing this).


#6

Solved the issue! Make sure to check the ImageId, the one in the Amazon example doesn’t exist. I’ve changed it too: ami-7b3db00c. When using the Amazon Linux AMI 2014.09.2 (HVM) - ami-9d23aeea as an alternative it didn’t like the fact it was HVM. ami-7b3db00c is an PV image.