Authenticating SDK

Welcome to Module two, AWS SDKs. Here's my question to you, how to access and work with AWS platform from within your application. We've covered AWS CLI, but there is a better way. Welcome to SDKs. Why it's better: well, here's some of the advantages of using SDKs. You can automate everything and anything. You can build your own clients or interfaces for AWS similarly to AWS web console. And you can integrate them directly into your applications. There is no need to create HTTP requests and worry about payloads, headers, formats, et cetera et cetera. You only work with objects, and you work in your favorite environment, such as Java, Python, NodeJS, as well as many, many others.

Let's take a look at the supported services. Pretty much all of them, S3, EC2, DynamoDB, many, many more, and the selection of services depends on what languages are available and different languages, different SDK libraries might support different services. So we have support for pretty much all the major languages. Obviously, at Node University, our favorite platform is NodeJS, but you can also use PHP, .NET, Java, iOS, Android, Python, Ruby, and even Go.

How to get started with Node SDK. Well, first of all, let's create a folder, then you navigate into that folder and create a package with the handy command "npm init", space dash y, y for yes. So basically that will answer yes to all the npm init questions and create a standard, boilerplate, package.json.

Out of that, you can install aws-sdk, that's the module name, aws-sdk, it's an npm module, and we would use npm to install it. So npm space i space dash SE, capital SE, space aws dash sdk. Why dash SE, S stands for save and the E for exact. In npm version five, everything is saved by default, but in older versions, all the npm, such as npm two, three and four, you would need to use S because we want to have the version and the package name saved into the package.json.

After that, we would need to provide the credentials. Remember, if we did "aws configure", to provide credentials to the AWS CLI tool, the same concept here, we need those two things, the access key and the secret. We have options. Just pick one: you can put them in your home directory, you can put them in your environmental variables, which might be better if you're working with multiple accounts on the same host machine. Or you can put them directly in your code, which is probably not a great idea for a real application, but it's totally fine for this lab.

How to add credentials in the home folder. Different locations for different platforms, so far Mac OS, and many Linux distributions, that is gonna be in your home, which is ~/.aws/credentials. Don't forget the dot in front of AWS, that's a hidden folder. For Windows users, it's gonna be in users, your name, dot AWS slash credentials. What will go inside of this file? Well, there are two lines, and the name of the variables the name of the configurations, is lower case with underscores. The region goes into Node file, so when you create essential objects, that's where you would put the region.

Let's take a look how you can use environmental variables. To authenticate with the node SDK. To do that, you would put these three environmental variables, you can put them in a shell script or you can just put them in front of whatever command you are launching. The lowercase is gone, now it's a capital case, and like in home folder, and also we can provide the region right here in the environmental variable.

Lastly, if you want to put your configurations and credentials in the configuration file, such as JSON, you can also do that. That name would be different yet again, there's no dashes, there's camelCase. So you pick what you want to use. I recommend, just put in a JSON file, the easiest way. Environmental variables is a better way for production, but for these examples, the NodeJS or config file will work too.