Using aws-sdk-js
This example shows how to use aws-sdk-js module.
aws-sdk-js
module is imported as aws
Create EC2 instance
const credentials = new aws.CognitoIdentityCredentials({
IdentityPoolId:'<identity pool ID>',
accessKeyId: '<acces key ID>',
secretAccessKey: '<secret access key>'
});
aws.config.update({
credentials,
region: 'us-west-2',
});
// Create EC2 service object
const ec2 = new aws.EC2({ apiVersion: '2016-11-15' });
// AMI is amzn-ami-2011.09.1.x86_64-ebs
var instanceParams = {
ImageId: 'AMI_ID',
InstanceType: 't2.micro',
KeyName: 'KEY_PAIR_NAME',
MinCount: 1,
MaxCount: 1
};
const instance = await new AWS.EC2({apiVersion: '2016-11-15'}).runInstances(instanceParams).promise();
const instanceId = data.Instances[0].InstanceId;
// Add tags to the instance
const tagParams = {
Resources: [instanceId],
Tags: [
{
Key: 'Name',
Value: 'SDK Sample'
}
],
};
// Create a promise on an EC2 service object
await = new AWS.EC2({apiVersion: '2016-11-15'}).createTags(tagParams).promise();
See SDK for JavaScript Code Examples for full examples.