Previous Lecture Complete and continue  

  Node AWS SDK: Get Regions

This is the basic idea. We have our JavaScript code. It codes the library, AWS SDK, and the library, which uses objects, it will under the hood use the HTTP calls to the platform, AWS EC2 endpoints. Once you've created your config file or environmental variables, now you can import the library from Node.js. So Node.js, describe.js, let's say that's the name of our file, we create a special object EC2 = new AWS.EC2

It's important to provide the version number, otherwise if there's a new version, and you try to run this code without the API version, it's just not gonna work. I recommend also providing the region just in case. You can do it with environmental variables as well, but it's just more robust and more predictable if you also put it here.

Then, you can create parameters, such as different filters. For example region name, endpoint, etcetera. These parameters, they go to describe regions. To describe regions singularly to describe regions in AWS CLI. That will give you a list of regions. It's omnipotent, meaning it doesn't matter how many times you run it, it will not modify your environment. It's a safe procedure. That's why I picked it for our first example.

If you're not familiar with Node.js in Node.js we use call backs a lot because it's asynchronous environment. So describe regions uses a callback. The first argument, it's a parameter, but the second it's a function, which is a call back, and all code which you want to execute after that will go inside of the callback. For example, describe images, it's the function that I want to execute after the describe regions. That's why I put describe images after I put it inside of this callback.

And similarly I have the console log-in screen inside of describe images, and that's in its own callback. So how do you run your code? So you saved it in describe.js. You have the package.json, you have the library installed, which, by the way, would be in the folder named node_modules. Very easy, you just go into your terminal.

You go to the folder where you have that file describe.js and also you need to have that node_modules with aws-sdk inside of that node_modules folder. So you go into your terminal and just type "node describe.js" and that will run your Node script. There is no compilation, you don't need to compile anything. That will output you the regions. You can also take the code that I provide, the code for this course, which is in a folder named code, code/sdk. That's where you would find the describe.js.

0 comments