Table of Contents
What is AWS SDK?
Prerequisites
Create EC2 instance with AWS SDK
References
What is AWS SDK
The AWS SDK (Amazon Web Services Software Development Kit) is a collection of libraries and tools that developers use to interact with and access various Amazon Web Services (AWS) cloud services.
The AWS SDK abstracts the complexities of handling AWS APIs and authentication, making it easier for developers to interact with AWS services programmatically and build scalable, flexible, and robust applications on top of AWS infrastructure.
Prerequisites
Installed AWS CLI;
Configured AWS CLI with
aws configure
command;
Create an EC2 instance with AWS-SDK
Initialize a new npm project with the command:
npm init -y
Install an aws-sdk package locally with the command:
npm install aws-sdk
Create a
createInstance.js
file inside the project:touch createInstance.js
First, import the AWS SDK (Software Development Kit) library into your code in the file
createInstance.js
:const AWS = require("aws-sdk");
We can then use
AWS
variable to access various AWS services, create instances, make API calls, and manage AWS resources using the methods provided by the SDK.Then, update the AWS SDK configuration with the following credentials and the region:
AWS.config.update({ "accessKeyId": "xxx", "secretAccessKey": "xxx", "region": "us-east-1" })
The SDK will use this information when making requests to AWS services. This ensures that your requests are properly authenticated and directed to the correct region, allowing you to work with AWS services securely and efficiently.
Next, create an instance of the AWS EC2 service object with a specified API version:
const ec2 = new AWS.EC2({apiVersion: "2016-11-15"});
Using a specific API version ensures that your code remains consistent and doesn't break unexpectedly when AWS introduces changes to its service APIs. It also allows you to use new features and improvements available in a specific API version. Now, we can use ec2 object's methods to perform various operations related to Amazon EC2.
Create a JS object named
instanceParams
that contain parameters to be used for creating a new Amazon EC2 instance using the AWS SDK.const instanceParams = { ImageId: "ami-053b0d53c279acc90", InstanceType: "t2.micro", KeyName: "demo-key", MinCount: 1, MaxCount: 1 }
Using promises in JavaScript, create an EC2 instance with the AWS SDK:
const instancePromise = ec2.runInstances(instanceParams).promise() // Handle promise's fulfilled and rejected states instancePromise.then(function(data){ console.log(data); const instanceId = data.Instances[0].InstanceId; console.log("Created instance: ", instanceId); }).catch(function(err){ console.log(err, err.stack); })
The code above sets up a promise chain to create an EC2 instance using the
runInstances()
method, handle the response data to extract the instance ID and log the instance ID if the creation is successful. If any errors occur during the process, the code logs the error message and stack trace to the console.Finally, run the code with the command:
node createInstance.js
The output is:
Check whether the instance is running via AWS Console: