我需要 powershell 代码来终止实例,而不需要对实例 ID 进行硬编码。
我试过了
aws ec2 terminate-instances --instance-ids 'curl http://169.254.169.254/latest/meta-data/instance-id'
但实例不会终止。有任何想法吗?
最佳答案
正如评论中提到的,我建议使用 AWS Powershell 模块。
以下代码根据 ID 和区域终止实例。
Install-Module AWSPowerShell
Import-Module AWSPowerShell
#Set AWS Credential
Set-AWSCredential -AccessKey "AccessKey" -SecretKey "SecretKey"
#Remove EC2 Insatnace
Remove-EC2Instance -InstanceId "InstanceId" -Region "Region" -Force
如何创建新的AccessKey和SecretKey - Managing Access Keys for Your AWS Account .
AWSPowerShell Module installation.
来自文档:
Terminates a stopped or running Amazon EC2 instance, prompting for confirmation before proceeding.
Note that terminated instances will remain visible after termination (for approximately one hour). The Terminate operation is idempotent; if you terminate an instance more than once, each call will succeed.
关于amazon-web-services - 如何从powershell自行终止EC2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432341/