Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The aws ec2 interface has a button called "launch more like this" that launches a second instance like the one selected. There is no similar functionality in the aws command line interface as far as I can tell.

like image 461
Hamzeh Alsalhi Avatar asked Nov 04 '15 17:11

Hamzeh Alsalhi


People also ask

How do I replicate an EC2 instance?

To use your current instance as a templateOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance you want to use, and then choose Actions, Images and templates, Launch more like this. The launch instance wizard opens.

Which CLI command is used to launch new EC2 instances?

To launch an Amazon EC2 instance using the AMI you selected, use the aws ec2 run-instances command. You can launch the instance into a virtual private cloud (VPC).

Is EC2 automatically replicated?

Replication Servers are lightweight Amazon EC2 instances that are used to replicate data between your source servers and AWS. Replication Servers are automatically launched and terminated as needed.


2 Answers

Here's a working (though hacky) way to build 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
like image 193
Ken J Avatar answered Sep 28 '22 07:09

Ken J


Launch more like this is a wizard and there is no CLI equivalent for it. You need to get the first instance attributes and use them to lauch the seconds instance with the same attributes (except for the ones that are unique to an instance). The link adds --clone option to awscli. Check it out.

like image 42
helloV Avatar answered Sep 28 '22 07:09

helloV