How to replicate ec2 "run more like this" from the command line?

The aws ec2 interface has a button called “launch in a more similar way” that launches a second instance similar to the one selected. As far as I know, there are no similar functions in the aws command line interface.

+4
source share
3 answers

This works (albeit hacked) way to create an instance as a copy of another instance:

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
+4
source

, - , CLI. ( , ). --clone awscli. .

+1

This is probably not the answer you are looking for, but if you can describe your initial instance of Cloud Formation , then you can create even more with:

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

Source: https://habr.com/ru/post/1614494/


All Articles