amazon-web-servicesaws-marketplace

Deploy a free product from AWS Marketplace via an API?


Is it possible to deploy a free product from the AWS Marketplace onto an AWS instance purely via API calls?

I already have a piece of Node.js code that allows me to start/stop an AWS instance but I'd like to further automate this to add a product from the marketplace.


Solution

  • The AWS EC2 API allows you to launch an instance and specify the AMI (Amazon Machine Image) you wish to use. In fact, I believe it requires it, because the OS itself comes from an AMI and without specifying an AMI, it wouldn't know what OS you want. Additionally, there are AMI's (as you've seen in the Marketplace) that include more than just the OS- applications, various environments, etc.

    I'm not a Node developer, but it looks like, in Node.js, you can specify the AMI, by providing the AMI ID# in the parameters to the runInstances method.

    See the below example:
    http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-examples.html

    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.log("Could not create instance", err); return; }
    
      var instanceId = data.Instances[0].InstanceId;
      console.log("Created instance", instanceId);
    
      // Add tags to the instance
      params = {Resources: [instanceId], Tags: [
        {Key: 'Name', Value: 'instanceName'}
      ]};
      ec2.createTags(params, function(err) {
        console.log("Tagging instance", err ? "failure" : "success");
      });
    });
    

    Note the parameters Object created with both the ImageId and InstanceType. I would imagine both are likely required elements. With this code, you would obviously need to hardcode the AMI ID, however you would then be able to automate the launching of new instances.

    You can find more information in the Javascript API, here:

    http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html

    I hope that helps!