Amazon Cli Ec2
http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/Welcome.html
Commands
syntaxt of commands http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html
some simple comands start and stop machines
aws ec2 stop-instances --instance-ids i-123abc56
STOPPINGINSTANCES i-123abc56
CURRENTSTATE 64 stopping
PREVIOUSSTATE 16 running
aws ec2 start-instances --instance-ids i-123abc56
STARTINGINSTANCES i-123abc56
CURRENTSTATE 0 pending
PREVIOUSSTATE 80 stopped
extract the name from the tags
aws ec2 describe-instances --query 'Reservations[1].Instances[0].Tags[*]' --output text | grep '^Name'
Name all-ii-251-prod
add a tag
aws ec2 create-tags --resources i-adbc23 --tags Key=CLAVIS_ENVIRONMENT,Value=prod
list tags for an instance
aws ec2 describe-tags --filters Name=resource-id,Values=i-adbc23
extract all the instances with a tag "type" and value "dqonly"
aws ec2 describe-instances --output text --filters Name="tag:type",Values="dqonly"
Backup snapshot system with the tags
#EXTRACT ALL INSTANCE IDS BY TAG
LIST=`aws ec2 describe-instances --output text --query 'Reservations[*].Instances[0].InstanceId' --filters Name="tag:backup",Values="active" `
for myid in `echo $LIST`
do
#EXTRACT THE VOLUMEID FROM THE INSTANCE ID
VOLID=`aws ec2 describe-instances --filters Name="instance-id ",Values="$myid" --output text --query 'Reservations[*].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId'`
#EXTRACT TAG NAME
DESCRIPTION=`aws ec2 describe-instances --filters Name="instance-id ",Values="$myid" --output text --query 'Reservations[*].Instances[0].Tags[*]' | grep Name `
echo "create a snapshot for volid: " $VOLID "with the description: " $DESCRIPTION
aws ec2 create-snapshot --volume-id $VOLID --description "$DESCRIPTION"
sleep 5
done
the delay at the end it is important to avoid to run to much task in the same time and reach the some kind of limits for aws
extract the public dns name of a machine
aws ec2 describe-instances --filters Name="instance-id ",Values="$ID" | grep PublicDnsName | head -1 | cut -f 4 -d '"'
describe ip
aws ec2 describe-addresses --filters Name="instance-id ",Values="i-000111"
{
"Addresses": [
{
"Domain": "vpc",
"InstanceId": "i-000111",
"NetworkInterfaceId": "eni-a3cbbcd4",
"AssociationId": "eipassoc-51a02134",
"NetworkInterfaceOwnerId": "9878204332",
"PublicIp": "8.8.8.8",
"AllocationId": "eipalloc-01fa1164",
"PrivateIpAddress": "10.0.0.12"
}
]
}
describe all the instance
aws ec2 describe-instances --instance-ids i-f1413efd
extract Istanceid from name
aws ec2 describe-tags --filters Name="key",Values="Name" | grep -B1 myname
enable termination on protection from an instance
to have this value "Termination protection" to "True"
if possible use this command
aws ec2 modify-instance-attribute --instance-id i-1122bbaa --disable-api-termination
add a ingress rule to a security group
aws ec2 authorize-security-group-ingress --group-id sg-12345abc --protocol tcp --port 443 --cidr 8.8.8.8/32
list the private ips of machines with a specific tag
by Luca
aws ec2 describe-instances --filters "Name=tag:Name,Values=i-wuseapp01" --output text --query "Reservations[*].Instances[*].PrivateIpAddress"
create a subnet in a vpc
aws ec2 --no-verify-ssl --region eu-west-1 --profile infra create-subnet --vpc-id vpc-xxxxxx --cidr-block '10.1.1.0/24'
versione della pagina: 3, ultima modifica: 11 Apr 2017 09:37