设为首页 收藏本站
查看: 516|回复: 0

[经验分享] 常用AWS CLI Shell脚本

[复制链接]

尚未签到

发表于 2019-2-22 10:22:06 | 显示全部楼层 |阅读模式
  您可能会用到的AWS CLI Shell脚本。主要功能有根据名字/类别管理EC2、RDS资源,创建资源时自动添加标签,创建EC2实例时可附加卷、定义用户数据文件。可用-h或--help查看支持的命令和参数,GitHub源码。

EC2
  common.sh

#!/bin/bash
INSTANCE_ID_REGEX="i-\w{8,17}"
IMAGE_ID_REGEX="ami-\w{8,17}"
VOLUME_ID_REGEX="vol-\w{8,17}"
SNAPSHOT_ID_REGEX="snap-\w{8,17}"
ROUTETABLE_ID_REGEX="rtb-\w{8,17}"
query_instance_id_by_name() {
instance_id=$(aws ec2 describe-instances --filter Name=tag:Name,Values="$1" Name=instance-state-name,Values=pending,running,stopped \
--query 'Reservations[0].[Instances[0].InstanceId]' | grep -o -E "$INSTANCE_ID_REGEX")
echo ${instance_id}
}
query_instance_ids_by_category() {
instance_ids=$(aws ec2 describe-instances --filter Name=tag:Category,Values="$1" Name=instance-state-name,Values=pending,running,stopped \
--query 'Reservations
  • .[Instances
  • .InstanceId]' | grep -o -E "$INSTANCE_ID_REGEX")
    echo ${instance_ids}
    }
    wait_instance_ok() {
    instance_id=$(aws ec2 describe-instances --filter Name=tag:Name,Values="$1" Name=instance-state-name,Values=pending,running \
    --query 'Reservations[0].[Instances[0].InstanceId]' | grep -o -E "$INSTANCE_ID_REGEX")
    check_instance_status ${instance_id}
    }
    check_instance_status() {
    while true
    do
    ok_count=$(aws ec2 describe-instance-status --instance-id $1 | grep -c ok)
    if [[ "$ok_count" -eq 2 ]]; then
    break
    else
    echo "Waiting ..."
    sleep 5
    fi
    done
    }
    describe_running_instances() {
    instances=$(aws ec2 describe-instances --filter Name=instance-state-name,Values=running \
    --query 'Reservations
  • .Instances
  • .{State:State.Name,Ip:PrivateIpAddress,InstanceId:InstanceId,Name:Tags[0].Value}')
    echo ${instances}
    }
    query_image_id_by_name() {
    image_id=$(aws ec2 describe-images --filter Name=name,Values="$1" --query Images[0].[ImageId] | grep -o -E "$IMAGE_ID_REGEX")
    echo ${image_id}
    }
    query_volume_id_by_name() {
    id=$(aws ec2 describe-volumes --filter Name=tag:Name,Values="$1" --query Volumes[0].[VolumeId] | grep -o -E "$VOLUME_ID_REGEX")
    echo ${id}
    }
    query_volume_ids_by_name() {
    id=$(aws ec2 describe-volumes --filter Name=tag:Name,Values="$1" --query Volumes
  • .[VolumeId] | grep -o -E "$VOLUME_ID_REGEX")
    echo ${id}
    }
    query_volume_id_by_instance_id_and_device() {
    id=$(aws ec2 describe-volumes --filter Name=attachment.instance-id,Values="$1" Name=attachment.device,Values=$2 \
    --query Volumes[0].[VolumeId] | grep -o -E "$VOLUME_ID_REGEX")
    echo ${id}
    }
    query_snapshot_id_by_name() {
    snapshot_id=$(aws ec2 describe-snapshots --filter Name=tag:Name,Values="$1" --query Snapshots[0].[SnapshotId] | grep -o -E "$SNAPSHOT_ID_REGEX")
    echo ${snapshot_id}
    }
    query_snapshot_ids_by_image_id() {
    snapshot_ids=$(aws ec2 describe-snapshots --query Snapshots
  • .[SnapshotId]
  • --filter Name=description,Values=*"$1"* | grep -o -E "$SNAPSHOT_ID_REGEX")
    echo ${snapshot_ids}
    }
    query_route_table_id_by_name() {
    id=$(aws ec2 describe-route-tables --filter Name=tag:Name,Values="$1" --query RouteTables[0].RouteTableId | grep -o -E "$ROUTETABLE_ID_REGEX")
    echo ${id}
    }
    query_elb_instance_ids() {
    ids=$(aws elb describe-load-balancers --load-balancer-name "$1" --query LoadBalancerDescriptions[0].[Instances
  • .[InstanceId]] | grep -o -E "$INSTANCE_ID_REGEX")
    echo ${ids}
    }
    create_tags_with_name() {
    resource_id=$1
    name=$2
    tags=$3;
    if [[ -z ${resource_id} ]]; then
    return 1
    fi
    if [[ ${tags} ]]; then
    echo "Add tags: ${tags}"
    fi
    aws ec2 create-tags --resources ${resource_id} --tags Key=Name,Value="${name}" ${tags}
    echo
    }
      ec2.sh

    #!/bin/bash
    . $(dirname $0)/common.sh
    # 根据Instance Name创建image并添加标签
    create_image() {
    instance_name=$1
    image_name=$2
    tags=$3
    echo "Create image for instance ${instance_name}"
    instance_id=$(query_instance_id_by_name "${instance_name}")
    image_id=$(aws ec2 create-image --instance-id "${instance_id}" --name "${image_name}" --description "${image_name}" --no-reboot --query ImageId)
    image_id=${image_id//\"/}
    echo "ImageId: $image_id"
    create_tags_with_name "${image_id}" "${image_name}" "${tags}"
    }
    # 删除AMI
    delete_image() {
    image_name=$1
    echo "Delete image ${image_name}"
    image_id=$(query_image_id_by_name "${image_name}")
    echo "Image id: ${image_id}"
    echo "Deregister image $image_id"
    aws ec2 deregister-image --image-id "${image_id}"
    snapshot_ids=$(query_snapshot_ids_by_image_id "${image_id}")
    for snapshot_id in ${snapshot_ids}
    do
    echo "Delete snapshot ${snapshot_id}"
    aws ec2 delete-snapshot --snapshot-id "${snapshot_id}"
    done
    echo
    }
    # 根据Name启动EC2 Instance
    start_instance() {
    id=$(query_instance_id_by_name "$1")
    aws ec2 start-instances --instance-ids ${id}
    }
    # 根据类别启动EC2 Instance
    start_instances() {
    ids=$(query_instance_ids_by_category "$1")
    aws ec2 start-instances --instance-ids ${ids}
    }
    # 根据Name停止EC2 Instance
    stop_instance() {
    id=$(query_instance_id_by_name "$1")
    aws ec2 stop-instances --instance-ids ${id}
    }
    # 根据类别停止EC2 Instance
    stop_instances() {
    ids=$(query_instance_ids_by_category "$1")
    aws ec2 stop-instances --instance-ids ${ids}
    }
    # 根据Name重启EC2 Instance
    reboot_instance() {
    id=$(query_instance_id_by_name "$1")
    aws ec2 reboot-instances --instance-ids ${id}
    }
    # 根据类别重启EC2 Instance
    reboot_instances() {
    ids=$(query_instance_ids_by_category "$1")
    aws ec2 reboot-instances --instance-ids ${ids}
    }
    # 根据Name终止EC2 Instance
    terminate_instance() {
    id=$(query_instance_id_by_name "$1")
    echo "terminate instance, instance name: $1 instance id: ${id}"
    aws ec2 modify-instance-attribute --instance-id "${id}" --no-disable-api-termination
    aws ec2 terminate-instances --instance-ids ${id}
    echo
    }
    # 根据类别终止EC2 Instance
    terminate_instances() {
    ids=$(query_instance_ids_by_category "$1")
    echo "terminate instances, category: $1 instance-ids: ${ids}"
    for id in ${ids}
    do
    aws ec2 modify-instance-attribute --instance-id "${id}" --no-disable-api-termination
    done
    aws ec2 terminate-instances --instance-ids ${ids}
    echo
    }
    # 从Image创建EC2 Instance,EC2配置从JSON文件读取,可以附加一个Volume,可以使用用户数据文件,可以添加标签
    run_instance() {
    instance_name=$1
    image_name=$2
    device_snapshot_name=$3
    init_file=$4
    tags=$5
    block_device_mappings=" "
    if [[ "${device_snapshot_name}" ]]; then
    snapshot_id=$(query_snapshot_id_by_name "${device_snapshot_name}")
    if [[ "${snapshot_id}" ]]; then
    block_device_mappings="--block-device-mappings DeviceName=/dev/sdf,Ebs={SnapshotId=${snapshot_id},DeleteOnTermination=true,VolumeType=gp2}"
    else
    echo "Please provide a valid volume snapshot name"
    exit 1
    fi
    fi
    echo "Create EC2 instance ${instance_name} from image ${image_name}"
    image_id=$(query_image_id_by_name "${image_name}")
    if [[ "$init_file" ]]; then
    instance_id=$(aws ec2 run-instances --image-id "${image_id}" ${block_device_mappings} --cli-input-json file://json/"${instance_name}".json \
    --user-data file://"${init_file}" --query 'Instances[0].[InstanceId]' | grep -o -E "${INSTANCE_ID_REGEX}")
    else
    instance_id=$(aws ec2 run-instances --image-id "${image_id}" ${block_device_mappings} --cli-input-json file://json/"${instance_name}".json \
    --query 'Instances[0].[InstanceId]' | grep -o -E "${INSTANCE_ID_REGEX}")
    fi
    create_tags_with_name "${instance_id}" "${instance_name}" "${tags}"
    }
    # 为EC2 Instance的指定卷创建快照并删除以前同名快照
    create_snapshot() {
    instance_name=$1
    device=$2
    snapshot_name=$3
    tags=$4
    instance_id=$(query_instance_id_by_name "${instance_name}")
    delete_snapshot "${snapshot_name}"
    volume_id=$(query_volume_id_by_instance_id_and_device ${instance_id} ${device})
    if [[ "${volume_id}" ]]; then
    echo "create snapshot for volume: ${device} of instance ${instance_name}"
    snapshot_id=$(aws ec2 create-snapshot --volume-id ${volume_id} | grep -o -E "${SNAPSHOT_ID_REGEX}")
    create_tags_with_name "${snapshot_id}" "${snapshot_name}" "${tags}"
    fi
    }
    # 根据名称删除快照
    delete_snapshot() {
    snapshot_id=$(query_snapshot_id_by_name "$1")
    if [[ "${snapshot_id}" ]]; then
    echo "delete snapshot: $1"
    aws ec2 delete-snapshot --snapshot-id ${snapshot_id}
    fi
    }
    # 从快照创建卷并删除旧的重名卷,然后将卷连接到Instance的指定device
    attach_volume() {
    snapshot_name=$1
    instance_name=$2
    device=$3
    tags=$4
    availability_zone="cn-north-1a"
    volume_name="$1-1a"
    snapshot_id=$(query_snapshot_id_by_name ${snapshot_name})
    instance_id=$(query_instance_id_by_name ${instance_name})
    if [[ -z "${snapshot_id}" ]]; then
    echo "Please provide valid snapshot name"
    exit 1
    fi
    if [[ -z "${instance_id}" ]]; then
    echo "Please provide valid instance name"
    exit 1
    fi
    old_volume_ids=$(query_volume_ids_by_name "${volume_name}")
    for id in ${old_volume_ids}
    do
    echo "delete old volume: $id"
    aws ec2 delete-volume --volume-id ${id}
    done
    echo "create volume ${volume_name} from snapshot ${snapshot_name}(${snapshot_id})"
    volume_id=$(aws ec2 create-volume --snapshot-id ${snapshot_id} --availability-zone ${availability_zone} --volume-type gp2 --query 'VolumeId' \
    | grep -o -E "${VOLUME_ID_REGEX}")
    count=0
    while [[ "${count}" -le 0 ]]
    do
    echo "Creating volume ${volume_name} ..."
    count=$(aws ec2 describe-volumes --volume-ids ${volume_id} --query Volumes[0].State | grep -c available)
    sleep 3
    done
    echo "attach volume: ${volume_name} to instance ${instance_name}"
    aws ec2 attach-volume --volume-id ${volume_id} --instance-id ${instance_id} --device ${device}
    create_tags_with_name "${volume_id}" "${volume_name}" "${tags}"
    }
    # 关联弹性IP到Instance
    associate_address() {
    instance_id=$(query_instance_id_by_name "$1")
    aws ec2 associate-address --instance-id ${instance_id} --public-ip $2
    }
    # 更新NAT Instance路由
    replace_route() {
    echo "update route table: $1"
    route_table_id=$(query_route_table_id_by_name "$1")
    nat_instance_id=$(query_instance_id_by_name "$2")
    cidr=$3
    aws ec2 replace-route --route-table-id ${route_table_id} --destination-cidr-block ${cidr} --instance-id ${nat_instance_id}
    }
    echo_usage() {
    echo "Usage: $0 [command] [args ...]"
    echo "Commands:"
    echo "  create-image [instance_name] [image_name] [tags] Create an AMI from an EC2 instance"
    echo "  delete-image [image_name] Delete image by name"
    echo "  start-instance [instance_name] Start an EC2 instance"
    echo "  start-instances [category_name] Start EC2 instances by category"
    echo "  stop-instance [instance_name] Stop an EC2 instance"
    echo "  stop-instances [category_name] Stop EC2 instances by category"
    echo "  reboot-instance [instance_name] Reboot an EC2 instance"
    echo "  reboot-instances [category_name] Reboot EC2 instances by category"
    echo "  terminate-instance [instance_name] Terminate an EC2 instance"
    echo "  terminate-instances [category_name] Terminate EC2 instances by category"
    echo "  run-instance [instance_name] [image_name] [options] Launch an instance using an AMI"
    echo "    Options:"
    echo "      --device-snapshot-name One block device snapshot name"
    echo "      --init-file an user data file"
    echo "      --tags One  or more tags"
    echo "  create-snapshot [instance_name] [device] [snapshot_name] [tags] Creates a snapshot of the specified volume for an instance"
    echo "  delete-snapshot [snapshot_name] Deletes the specified snapshot"
    echo "  attach-volume [snapshot_name] [instance_name] [device] [tags] Create a volume from a snapshot, and then attach the volume to an instance"
    echo "  associate-address [instance_name] [public_ip]  Associates an Elastic IP address with an instance"
    echo "  replace-route [route_table_name] [nat_instance_name] [cidr] Replaces an existing route within a route table in a VPC"
    }
    if test @$1 = @--help -o @$1 = @-h; then
    echo_usage
    exit 0
    fi
    if [[ $# -lt 2 ]]; then
    echo_usage
    exit 1
    fi
    case "$1" in
    create-image)
    create_image "$2" "$3" "$4"
    ;;
    delete-image)
    delete_image "$2"
    ;;
    start-instance)
    start_instance "$2"
    ;;
    start-instances)
    start_instances "$2"
    ;;
    stop-instance)
    stop_instance "$2"
    ;;
    stop-instances)
    stop_instances "$2"
    ;;
    reboot-instance)
    reboot_instance "$2"
    ;;
    reboot-instances)
    reboot_instances "$2"
    ;;
    terminate-instance)
    terminate_instance "$2"
    ;;
    terminate-instances)
    terminate_instances "$2"
    ;;
    run-instance)
    args=`getopt -l init-file:,device-snapshot-name:,tags: -- "$@"`
    if [[ $? != 0 ]] ; then
    exit 1
    fi
    device_snapshot_name=""
    init_file=""
    tags=""
    eval set -- "${args}"
    while true
    do
    case "$1" in
    --device-snapshot-name)
    device_snapshot_name="$2"
    shift 2
    ;;
    --init-file)
    init_file="$2"
    shift 2
    ;;
    --tags)
    tags="$2"
    shift 2
    ;;
    --)
    shift
    break
    ;;
    esac
    done
    run_instance "$2" "$3" "${device_snapshot_name}" "${init_file}" "${tags}"
    ;;
    create-snapshot)
    create_snapshot "$2" "$3" "$4" "$5"
    ;;
    delete-snapshot)
    delete_snapshot "$2"
    ;;
    attach-volume)
    attach_volume "$2" "$3" "$4" "$5"
    ;;
    associate-address)
    associate_address "$2" "$3"
    ;;
    replace-route)
    replace_route "$2" "$3" "$4"
    ;;
    *)
    exit 1
    ;;
    esac
      示例ec2.json

    {
    "DryRun": false,
    "KeyName": "AWS Prod Key Pair",
    "SecurityGroupIds": [
    "sg-1361c577"
    ],
    "InstanceType": "m4.large",
    "Placement": {
    "AvailabilityZone": "cn-north-1a",
    "Tenancy": "default"
    },
    "Monitoring": {
    "Enabled": false
    },
    "DisableApiTermination": true,
    "InstanceInitiatedShutdownBehavior": "stop",
    "NetworkInterfaces": [
    {
    "DeviceIndex": 0,
    "SubnetId": "subnet-d45208b1",
    "PrivateIpAddress": "10.184.12.246",
    "DeleteOnTermination": true,
    "AssociatePublicIpAddress": false
    }
    ],
    "EbsOptimized": false
    }
    RDS
      rds.sh

    #!/bin/bash
    # 根据db-instance-identifier删除数据库,删除前先创建快照,并将db-snapshot-identifier保存在文件中
    delete() {
    echo "deleting database $1 ..."
    snapshot_id="$1-$(date +%Y%m%d)"
    aws rds delete-db-instance --db-instance-identifier "$1" --no-skip-final-snapshot --final-db-snapshot-identifier "${snapshot_id}"
    echo "${snapshot_id}" > "$(dirname $0)/$1.snapshot.id"
    }
    # 从最近的快照恢复数据库,数据库配置从json文件读取,恢复成功后指定security group,并输出恢复日志到文件中
    restore() {
    log_file="$(dirname $0)/restore.log"
    id_file="$(dirname $0)/$1.snapshot.id"
    snapshot_id=$(cat ${id_file})
    echo "Restore database $1 from snapshot ${snapshot_id}" | tee ${log_file}
    aws rds restore-db-instance-from-db-snapshot --db-snapshot-identifier "${snapshot_id}" --cli-input-json file://json/"$1".json | tee ${log_file}
    count=0
    while [[ "${count}" -le 0 ]]
    do
    echo "Creating database $1 ..."
    count=$(aws rds describe-db-instances --db-instance-identifier "$1" --query 'DBInstances[0].[DBInstanceStatus]' | grep -c available)
    sleep 5
    done
    echo "Modify database $1" | tee ${log_file}
    aws rds modify-db-instance --db-instance-identifier "$1" --vpc-security-group-ids $2 | tee ${log_file}
    }
    echo_usage() {
    echo "Usage: $0 [delete|restore] [args ...]"
    echo "Commands:"
    echo "  delete [db-instance-identifier] Delete a DB instance"
    echo "  restore [db-instance-identifier] [vpc-security-group-ids] Create a new DB instance from a DB snapshot"
    }
    if test @$1 = @--help -o @$1 = @-h; then
    echo_usage
    exit 0
    fi
    if [[ $# -lt 2 ]]; then
    echo_usage
    exit 1
    fi
    case "$1" in
    delete)
    delete "$2"
    ;;
    restore)
    restore "$2" "$3"
    ;;
    *)
    exit 1
    ;;
    esac
      示例dn.json

    {
    "DBInstanceIdentifier": "test",
    "DBInstanceClass": "db.r3.large",
    "Port": 1521,
    "AvailabilityZone": "cn-north-1a",
    "DBSubnetGroupName": "test-db",
    "MultiAZ": false,
    "PubliclyAccessible": false,
    "AutoMinorVersionUpgrade": true,
    "LicenseModel": "license-included",
    "DBName": "test",
    "Engine": "oracle-se1",
    "OptionGroupName": "default:oracle-se1-11-2",
    "StorageType": "standard",
    "CopyTagsToSnapshot": true
    }
    CloudWatch
      cloudwatch.sh

    #!/bin/bash
    . $(dirname $0)/common.sh
    # 当EC2 Instance CPU利用率超过90%时发送邮件告警
    cpu() {
    instance_name=$1
    sns_topic_arn=$2
    instance_id=$(query_instance_id_by_name "${instance_name}")
    aws cloudwatch put-metric-alarm --alarm-name "cpu-${instance_name}" --alarm-description "Alarm when CPU exceeds 90 percent" --metric-name CPUUtilization \
    --namespace AWS/EC2 --statistic Average --period 300 --threshold 90 --comparison-operator GreaterThanThreshold \
    --dimensions "Name=InstanceId,Value=${instance_id}" --evaluation-periods 2 --unit Percent --alarm-actions $2
    }
    echo_usage() {
    echo "Usage: $0 [cpu] [instance_name] [sns_topic_arn]"
    }
    if test @$1 = @--help -o @$1 = @-h; then
    echo_usage
    exit 0
    fi
    if [[ $# -lt 2 ]]; then
    echo_usage
    exit 1
    fi
    case "$1" in
    "cpu")
    cpu "$2" "$3"
    ;;
    *)
    exit 1
    ;;
    esac



  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-675672-1-1.html 上篇帖子: AWS Centos磁盘和内存监控脚本 下篇帖子: AWS Athena 分析日志
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表