如何复制ec2"更多这样的推出"从命令行?

时间:2015-11-04 17:56:39

标签: amazon-web-services amazon-ec2

aws ec2界面有一个名为&#34的按钮;启动更像这样的"启动第二个实例,如选择的那个。据我所知,aws命令行界面中没有类似的功能。

4 个答案:

答案 0 :(得分:4)

这是一种将实例构建为另一个实例的副本的工作方式(虽然是hacky):

function cloneinstance {
  awsinstanceid=$1
  region=$2
  export AWS_DEFAULT_REGION=$region
  ami=$(aws ec2 describe-instances --instance-ids $awsinstanceid | grep INSTANCES | awk '{print $7}')
  privatekey=$(aws ec2 describe-instances --instance-ids $awsinstanceid | grep INSTANCES | awk '{print $10}')
  securitygroup=$(aws ec2 describe-instances --instance-ids $awsinstanceid | grep SECURITYGROUPS | awk '{print $2}')
  instancetype=$(aws ec2 describe-instances --instance-ids $awsinstanceid | grep INSTANCES | awk '{print $9}')
  subnet=$(aws ec2 describe-instances --instance-ids $awsinstanceid | grep NETWORKINTERFACES | awk '{print $9}')

  awsinstancedata=$(aws ec2 run-instances --image-id $ami --key-name $privatekey --security-group-ids $securitygroup --instance-type $instancetype --subnet-id $subnet)
  awsinstanceid=$(echo $awsinstancedata | awk '{print $9}')

  # AWS CLI sucks and doesn't return error codes so have to look for a valid id
  if [[ "$awsinstanceid" == i-* ]]; then echo -e "\t\tSuccessfully created. Instance ID: $awsinstanceid"; else echo -e "\t\tSomething went wrong. Check your configuration."; exit 1; fi 
  echo -e "\t\tWaiting for it to come up..."
  aws ec2 wait instance-running --instance-ids $awsinstanceid
  echo -e "\t\tServer is up and ready"
}

cloneinstance i-12345678 us-west-1

答案 1 :(得分:1)

更多像这样启动是一个向导,没有与之等效的CLI。您需要获取第一个实例属性并使用它们来创建具有相同属性的秒实例(除了实例唯一的属性)。 link为awscli添加了--clone选项。看看吧。

答案 2 :(得分:0)

这可能不是您正在寻找的答案,但如果您可以使用Cloud Formation描述初始实例,那么您可以创建更多类似的内容:

aws cloudformation create-stack --region your-region --template-body file://path-to-your-instance-description.json stack-name-must-be-unique

答案 3 :(得分:0)

第一条评论效果很好。但是为了上瘾,我添加了一个使用 powershell 核心的替代方案。

function LaunchMoreLikeThis ([String]$SourceInstanceName, [String]$CloneInstanceName, [String]$AMI_ID, [String]$InstanceType, [Int64]$Count = 1) {
 $SourceInstance = aws ec2 describe-instances --filters "Name=tag:Name,Values=$SourceInstanceName"
 $SourceInstance = $SourceInstance -join '' | ConvertFrom-Json -Depth 99

 $SourceInstanceAttr = $SourceInstance.Reservations.Instances[0];
 $InstanceId = $SourceInstanceAttr.InstanceId
 $SGs = $SourceInstanceAttr.SecurityGroups.GroupId | ForEach-Object { "`"$_`"" } | Join-String -Separator ' '  ;
 $PrivateKey = $SourceInstanceAttr.KeyName
 $SubnetId = $SourceInstanceAttr.SubnetId
 $UserData = pwsh -nologo -command "aws ec2 describe-instance-attribute --instance-id $InstanceId --attribute userData"
 $UserData = $UserData -join '' | ConvertFrom-Json -Depth 99
 $UserDataDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($UserData.UserData.Value))
 $UserDataFile = New-item -ItemType File -Path $([IO.Path]::GetTempPath()) -Name "user_data" -Value $UserDataDecoded -Force #You can change this to use a different user_data
 $Tags = "ResourceType=instance,Tags=[{Key=Name,Value=$CloneInstanceName}]"
 pwsh -nologo -command "aws ec2 run-instances --image-id $AMI_ID --instance-type $InstanceType --security-group-ids $SGs --count 1 --subnet-id $SubnetId --key-name $PrivateKey --user-data file://$($UserDataFile.FullName) --tag-specifications `'$Tags`'"

}

LaunchMoreLikeThis -SourceInstanceName "<source_instance_name>" -CloneInstanceName "<clone_instance_name>" -AMI_ID "<ami_id>" -InstanceType "t3.medium"

就我而言,我需要提供不同的 AMI,但您可以从实例中提取它。在我的示例中,我也在提取 user_data。