Node.js is a free, open source, cross-platform framework. It is used to execute JavaScript code on the server side. In Node.js, you can use AWS SDK for JavaScript. This SDK will help to remove the complexity of coding by providing JavaScript objects to use the AWS services. A single downloaded package includes the AWS JavaScript library as well documentation.
You can install AWS SDK for Node.js in two ways:
- From GitHub: You can get the source code from https://github.com/aws/aws-sdk-js
- From Node.js Package Manager (npm): You can install AWS SDK from the Node.js package manager
Let's install the AWS SDK package and create a sample application to create and delete a bucket on S3 using the following steps:
- You can download (https://nodejs.org/en/download/) and install Node.js If you haven't already installed it. Once you have installed Node.js, you can open the Node.js command prompt from (Run | Node.js command prompt in Windows.
- You need to create a package.json file to mention the required dependency to install AWS SDK and UUID. We need aws-sdk to install the required Node modules for AWS services and a UUID to create a pseudo random number:
package.json
{
"dependencies": {
"aws-sdk": ">= 2.0.9",
"uuid": ">= 1.4.1"
}
}
- Now open the command prompt and execute the following command:
npm install aws-sdk
- It will create a node_modules folder and install AWS SDK for Node.js. Now you need to set the credentials in the AWS credentials profile file on your local system, located at the following:
- For Linux, macOS, or Unix: ~/.aws/credentials
- For Windows: C:\Users\USERNAME\.aws\credentials
- The file format should be as follows:
[default]
aws_access_key_id = downloaded_access_key_id
aws_secret_access_key = downloaded_secret_access_key
- Now, replace your AWS credentials values with the values downloaded_access_key_id and downloaded_secret_access_key.
- Now let's create a S3Example.js file which will connect to AWS and create and delete the bucket on S3:
var AWS = require('aws-sdk');
var uuid = require('uuid');
- First, you have to create variable such as AWS and UUID to load SDK for JavaScript:
var s3 = new AWS.S3();
var bucketName = 'node-sdk-sample-' + uuid.v4();
var params={Bucket: bucketName}
Here you are creating s3 as an S3 service object, bucketname with node-sdk-sample as the prefix with a random number, and params as the parameters to call the bucket:
s3.createBucket(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Successfully Created Bucket: "+bucketName);
// successful response
});
The preceding method is used to create the bucket with parameters and callback functions:
s3.waitFor('bucketExists', params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
s3.deleteBucket(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Successfully Deleted Bucket:"+bucketName);
// successful response
});
}
});
Here it will check whether the bucket is exists or not. If it exists then it will delete the bucket. If it is not exist than it is trying to delete the bucket which is not created yet. In that case, you will get an error.
- You can execute the file with node S3Example.js and you can see that it will create and delete the bucket:
Previously, we discussed AWS SDKs for different programming languages and we have covered Java and Node.js with setup and examples. Now we will see how you can set up SDKs on IoT devices.