What is @aws-sdk/client-ec2?
@aws-sdk/client-ec2 is a part of the AWS SDK for JavaScript, which allows developers to interact with Amazon EC2 (Elastic Compute Cloud) services. This package provides a comprehensive set of functionalities to manage EC2 instances, security groups, key pairs, and other related resources programmatically.
What are @aws-sdk/client-ec2's main functionalities?
Launch an EC2 Instance
This code sample demonstrates how to launch a new EC2 instance using the @aws-sdk/client-ec2 package. It specifies the AMI ID, instance type, and the number of instances to launch.
const { EC2Client, RunInstancesCommand } = require('@aws-sdk/client-ec2');
const client = new EC2Client({ region: 'us-west-2' });
const run = async () => {
const command = new RunInstancesCommand({
ImageId: 'ami-0abcdef1234567890',
InstanceType: 't2.micro',
MinCount: 1,
MaxCount: 1
});
const response = await client.send(command);
console.log(response);
};
run();
Describe EC2 Instances
This code sample shows how to describe EC2 instances using the @aws-sdk/client-ec2 package. It retrieves information about all EC2 instances in the specified region.
const { EC2Client, DescribeInstancesCommand } = require('@aws-sdk/client-ec2');
const client = new EC2Client({ region: 'us-west-2' });
const describe = async () => {
const command = new DescribeInstancesCommand({});
const response = await client.send(command);
console.log(response);
};
describe();
Terminate an EC2 Instance
This code sample demonstrates how to terminate an EC2 instance using the @aws-sdk/client-ec2 package. It specifies the instance ID of the EC2 instance to be terminated.
const { EC2Client, TerminateInstancesCommand } = require('@aws-sdk/client-ec2');
const client = new EC2Client({ region: 'us-west-2' });
const terminate = async () => {
const command = new TerminateInstancesCommand({
InstanceIds: ['i-0abcdef1234567890']
});
const response = await client.send(command);
console.log(response);
};
terminate();
Create a Security Group
This code sample shows how to create a new security group using the @aws-sdk/client-ec2 package. It specifies the group name, description, and VPC ID.
const { EC2Client, CreateSecurityGroupCommand } = require('@aws-sdk/client-ec2');
const client = new EC2Client({ region: 'us-west-2' });
const createSecurityGroup = async () => {
const command = new CreateSecurityGroupCommand({
GroupName: 'my-security-group',
Description: 'My security group',
VpcId: 'vpc-0abcdef1234567890'
});
const response = await client.send(command);
console.log(response);
};
createSecurityGroup();
Other packages similar to @aws-sdk/client-ec2
aws-sdk
The 'aws-sdk' package is the older version of the AWS SDK for JavaScript. It provides a wide range of AWS services, including EC2. However, it is not modular like the v3 SDK, which means you have to install the entire SDK even if you only need EC2 functionalities.
aws-cdk
The 'aws-cdk' (AWS Cloud Development Kit) allows developers to define cloud infrastructure using code. It supports multiple programming languages and provides higher-level abstractions for AWS services, including EC2. It is more suitable for infrastructure as code (IaC) use cases compared to the @aws-sdk/client-ec2 package.
serverless
The 'serverless' framework is used to build and deploy serverless applications on AWS and other cloud providers. It supports AWS Lambda, API Gateway, and other services, including EC2. It provides a higher-level abstraction and is more focused on serverless architectures compared to the @aws-sdk/client-ec2 package.