Randy Savage
Randy Savage

Reputation: 1

Compiler Error when trying to use the AWS SDK to launch EC2 instances

I am trying to use the AWS SDK in order to automate the ec2 instance launching process, however whenever I run my script I receive a syntax error that I have not been able to get rid of.

const AWS = require('aws-skd')

AWS.config.loadFromPath('./config.json')

var ec2 = new AWS.EC2({apiVersion: '2016-11-15'})

const fs = require('fs')
var params = {
    ImageId: 'ami-0ff8a91507f77f867',
    InstanceType: 't3.nano',
    MinCount: 1,
    MaxCount: 1,
    Tenancy:dedicated
}

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)
});

any help is appreciated and also any pointers if I am calling the runInstance function improperly as I do not have much experience with nodejs yet and this is my first project using it and the AWS API

Edit: error is on line 1 char 1 syntax error code: 00A03EA

Upvotes: 0

Views: 215

Answers (1)

jarmod
jarmod

Reputation: 78613

There are quite a few problems with your current solution:

  • mis-spelling of aws-sdk
  • misuse of console.log
  • incorrect indication of Tenancy in params
  • missing SubnetId (the chosen instance type can only be launched in VPC)
  • mis-spelling of InstanceId in EC2 response

Here is an example of code that works (replace subnet-TODO as appropriate):

const AWS = require('aws-sdk')

AWS.config.loadFromPath('./config.json')

const ec2 = new AWS.EC2({apiVersion: '2016-11-15'})

const params = {
    ImageId: 'ami-0ff8a91507f77f867',
    InstanceType: 't3.nano',
    MinCount: 1,
    MaxCount: 1,
    SubnetId: 'subnet-TODO',
    Placement: {
        Tenancy: 'dedicated',
    }
};

ec2.runInstances(params, function(err,data) {
    if (err) {
        console.log('Could not create instance', err);
        return;
    }
    const instanceId = data.Instances[0].InstanceId;
    console.log('Created instance', instanceId);
});

Assuming this code is in index.js, run this via node index.js. Once that is working, you can move to getting it working with git-bash/VS Code/npm start. Hope this is helpful.

Upvotes: 1

Related Questions