Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092709
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: 系统运维

2016-07-07 16:55:43

     官方的CLI参考,
     常用的见下边的脚本,写着备忘。建议用CloudFormation吧。
    
#config.sh, 定义vpc等信息。
  1. #!/bin/bash
  2. REGION=cn-north-1

  3. VPC_NAME=web-vpc
  4. VPC_CIDR="172.32.0.0/16"

  5. #DNS
  6. DNS_ZONE_NAME=xxx.aws
  7. DNS_ZONE_CALLER=206-03-10-16:58
  8. DNS_ZONE_COMMENT="xxx.aws"

  9. #name, cidr, AZs
  10. SUBNETS="
  11. xx-1a-web,172.32.16.0/20,cn-north-1
  12. "

  13. # default security group names
  14. SECGROUPS="web"

  15. # ami-7154501b:base_xxx_os (centos 7.2)
  16. BASE_AMI=ami-da666fb4

  17. # instance_name|type|secutiy_group|subnet_cidr|need_public_ip|disk_size(GB)
  18. INSTANCES="
  19. autopush-server-1a-1|m4.xlarge|web|172.32.16.0/20|false|30
  20. autopush-server-1a-2|m4.xlarge|web|172.32.16.0/20|false|30
  21. autopush-server-1a-3|m4.xlarge|web|172.32.16.0/20|false|30
  22. autopush-server-1a-4|m4.xlarge|web|172.32.16.0/20|false|30
  23. autopush-server-1a-5|m4.xlarge|web|172.32.16.0/20|false|30
  24. autopush-server-1a-6|m4.xlarge|web|172.32.16.0/20|false|30
  25. autopush-server-1a-7|m4.xlarge|web|172.32.16.0/20|false|30
  26. "
# create.sh,创建ec2,添加DNS等。

  1. #!/bin/bash
  2. source ./config.sh
  3. echo "imported config..."

  4. function get_subnet_id(){
  5.         subnet_cidr=$1
  6.         echo `aws ec2 describe-subnets \
  7.                 --output text \
  8.                 --filter "Name=cidrBlock,Values=$subnet_cidr" \
  9.                 --query Subnets[].SubnetId \
  10.                 --region $REGION \
  11.                 --profile $REGION`
  12. }

  13. function get_sec_grp_id(){
  14.         sec_grp_name=$1
  15.         echo `aws ec2 describe-security-groups \
  16.                 --query SecurityGroups[].GroupId \
  17.                 --filter "Name=group-name,Values=$sec_grp_name" \
  18.                 --output text \
  19.                 --region $REGION \
  20.                 --profile $REGION`
  21. }

  22. function get_instance_by_name(){
  23.         instance_name=$1
  24.         echo `aws ec2 describe-instances \
  25.                 --filter "Name=tag:Name,Values=$instance_name" \
  26.                 --query Reservations[].Instances[].InstanceId \
  27.                 --output text \
  28.                 --region $REGION \
  29.                 --profile $REGION`
  30. }

  31. function run_instances(){

  32.         HOSTED_ZONE_ID=`aws route53 list-hosted-zones --query "HostedZones[?Name=='$DNS_ZONE_NAME.'].Id" --output text`

  33.         for instance in $INSTANCES
  34.                 do
  35.                         tag_name=`echo $instance|cut -d"|" -f1`
  36.                         type=`echo $instance|cut -d"|" -f2`
  37.                         sec_grp_name=`echo $instance|cut -d"|" -f3`
  38.                         subnet_cidr=`echo $instance|cut -d"|" -f4`
  39.                         need_public_ip=`echo $instance|cut -d"|" -f5`
  40.                         disk_size=`echo $instance|cut -d"|" -f6`
  41.                         # echo $tag_name, $type, $sec_grp_name, $subnet_cidr

  42.                         subnet_id=`get_subnet_id $subnet_cidr`
  43.                         sec_grp_id=`get_sec_grp_id $sec_grp_name`

  44.                         echo "creating $tag_name::($type|$sec_grp_name|$subnet_cidr)"
  45.                         instance_id=`get_instance_by_name $tag_name`

  46.                         device_mapping=`sed s/{{size}}/$disk_size/g ./device-mapping.json`

  47.                         if [ "$instance_id" == "" ]; then
  48.                                 echo "$tag_name does not exist, creating $tag_name instance...."
  49.                                 instance_id=$(aws ec2 run-instances --image-id $BASE_AMI \
  50.                                         --count 1 \
  51.                                         --instance-type $type \
  52.                                         --key-name stage \
  53.                                         --security-group-ids $sec_grp_id \
  54.                                         --subnet-id $subnet_id \
  55.                                         --output table \
  56.                                         --disable-api-termination \
  57.                                         --associate-public-ip-address \
  58.                                         --instance-initiated-shutdown-behavior stop \
  59.                                         --block-device-mappings "$device_mapping" \
  60.                                          ####  --user-data "$userdata" \
  61.                                         --output text \
  62.                                         --query Instances[].InstanceId \
  63.                                         --region $REGION \
  64.                                         --profile $REGION)
  65.                                 echo "instance created: $tag_name ($instance_id)"
  66.                         else
  67.                                 echo "$tag_name ($instance_id) existe, skipping...."
  68.                         fi

  69.                         aws ec2 create-tags --resources $instance_id --tags Key=Name,Value=$tag_name --output table --region $REGION --profile $REGION

  70.                         echo "waiting for 5 seconds..."
  71.                         sleep 5
  72.                         private_ip=`aws ec2 describe-instances --instance-ids $instance_id --query Reservations[].Instances[].PrivateIpAddress --output text --region $REGION --profile $REGION`

  73.                         dns_file_name="$tag_name"_dns.json
  74.                         cp ./dns.json $dns_file_name

  75.                         sed -i "s/{{hostname}}/$tag_name/g" $dns_file_name
  76.                         sed -i "s/{{domain}}/$DNS_ZONE_NAME/g" $dns_file_name
  77.                         sed -i "s/{{ipaddress}}/$private_ip/g" $dns_file_name

  78.                         # cat $dns_file_name
  79.                         aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch file://$dns_file_name --output table
  80.                         rm -rf $dns_file_name

  81.                         volume_id=`aws ec2 describe-volumes \
  82.                                 --filter "Name=attachment.instance-id,Values=$instance_id" \
  83.                                 --query Volumes[].VolumeId \
  84.                                 --output text \
  85.                                 --region=$REGION \
  86.                                 --profile=$REGION`

  87.                         if [ "$volume_id" != "" ]; then
  88.                                 aws ec2 create-tags --resources $volume_id --tags Key=Name,Value=$tag_name --output table --region $REGION --profile $REGION
  89.                         fi

  90.                         if [ "$need_public_ip" == 'true' ];then
  91.                                 ip_own_id=`aws ec2 describe-instances \
  92.                                         --instance-ids $instance_id \
  93.                                         --query Reservations[].Instances[].NetworkInterfaces[].Association.IpOwnerId \
  94.                                         --output text \
  95.                                         --region $REGION \
  96.                                     --profile $REGION`

  97.                                 public_ip=`aws ec2 describe-instances \
  98.                                         --instance-ids $instance_id \
  99.                                         --query Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp \
  100.                                         --output text \
  101.                                         --region $REGION \
  102.                                         --profile $REGION`

  103.                                 if [ "$ip_own_id" == "amazon" ]; then
  104.                                         aws ec2 wait instance-running --instance-ids $instance_id
  105.                                         eip_id=`aws ec2 allocate-address --domain vpc --query AllocationId --output text --region $REGION --profile $REGION`
  106.                                         aws ec2 associate-address --instance-id $instance_id --allocation-id $eip_id --output table --region $REGION --profile $REGION
  107.                                         public_ip=`aws ec2 describe-addresses \
  108.                                                 --filter "Name=allocation-id,Values=$eip_id" \
  109.                                                 --query Addresses[].PublicIp \
  110.                                                 --output text \
  111.                                                 --region $REGION \
  112.                                                 --profile $REGION`
  113.                                 fi

  114.                                 echo "$tag_name have associate-address:$public_ip"
  115.                         fi
  116.                 done
  117. }

  118. run_instances
  119. echo "done"
#device-mapping.json and dns.json

  1. [
  2. {
  3.     "DeviceName": "/dev/sda1",
  4.         "Ebs": {
  5.             "DeleteOnTermination": false,
  6.             "VolumeSize": {{size}},
  7.             "VolumeType": "gp2"
  8.         }
  9. }
  10. ]
  11. {
  12.     "Comment": "",
  13.         "Changes": [
  14.         {
  15.             "Action": "CREATE",
  16.             "ResourceRecordSet": {
  17.                 "Name": "{{hostname}}.{{domain}}",
  18.                 "Type": "A",
  19.                 "TTL": 600,
  20.                 "ResourceRecords": [
  21.                 {
  22.                     "Value": "{{ipaddress}}"
  23.                 }
  24.                 ]
  25.             }
  26.         }
  27.     ]
  28. }
# start/stop

  1. #!/bin/bash
  2. REGION=cn-north-1
  3. servers="
  4. autopush-server-1a-1
  5. autopush-server-1a-2
  6. "

  7. function get_instance_by_name(){
  8.     instance_name=$1
  9.     echo `aws ec2 describe-instances \
  10.         --filter "Name=tag:Name,Values=$instance_name" \
  11.         --query Reservations[].Instances[].InstanceId \
  12.         --output text \
  13.         --region $REGION \
  14.         --profile $REGION`
  15. }

  16. for server in $servers
  17. do
  18.     echo $server
  19.     server_id=`get_instance_by_name $server`
  20.     echo $server_id
  21.     aws ec2 start-instances --instance-ids $server_id --output table
  22.     # aws ec2 stop-instances --instance-ids $server_id --output table
  23.     echo "$server started!"
  24. done
总的来说,没啥别的,就是参考文档写,比较繁琐。




阅读(1470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~