您可以使用AWS CLI或SDK进行操作。
AWS CLI
一个非常简单的方法就是在第二个实例上安装 aws cli 并运行
aws ec2 start-instances --instance-ids i-1234567890abcdef0
或
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
SDK
如果您想坚持使用节点,那么您可以使用 SDK 以这种方式启动/停止实例:
var params = {
InstanceIds: [
"i-1234567890abcdef0"
]
};
/* Start */
ec2.startInstances(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
/* Stop */
ec2.stopInstances(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
重要提示
请注意,实例的 IAM 角色需要包含以下策略(或等效策略)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "*"
}
]
}
希望对你有帮助!