官方的CLI参考,
常用的见下边的脚本,写着备忘。建议用CloudFormation吧。
#config.sh, 定义vpc等信息。
-
#!/bin/bash
-
REGION=cn-north-1
-
-
VPC_NAME=web-vpc
-
VPC_CIDR="172.32.0.0/16"
-
-
#DNS
-
DNS_ZONE_NAME=xxx.aws
-
DNS_ZONE_CALLER=206-03-10-16:58
-
DNS_ZONE_COMMENT="xxx.aws"
-
-
#name, cidr, AZs
-
SUBNETS="
-
xx-1a-web,172.32.16.0/20,cn-north-1
-
"
-
-
# default security group names
-
SECGROUPS="web"
-
-
# ami-7154501b:base_xxx_os (centos 7.2)
-
BASE_AMI=ami-da666fb4
-
-
# instance_name|type|secutiy_group|subnet_cidr|need_public_ip|disk_size(GB)
-
INSTANCES="
-
autopush-server-1a-1|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-2|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-3|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-4|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-5|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-6|m4.xlarge|web|172.32.16.0/20|false|30
-
autopush-server-1a-7|m4.xlarge|web|172.32.16.0/20|false|30
-
"
# create.sh,创建ec2,添加DNS等。
-
#!/bin/bash
-
source ./config.sh
-
echo "imported config..."
-
-
function get_subnet_id(){
-
subnet_cidr=$1
-
echo `aws ec2 describe-subnets \
-
--output text \
-
--filter "Name=cidrBlock,Values=$subnet_cidr" \
-
--query Subnets[].SubnetId \
-
--region $REGION \
-
--profile $REGION`
-
}
-
-
function get_sec_grp_id(){
-
sec_grp_name=$1
-
echo `aws ec2 describe-security-groups \
-
--query SecurityGroups[].GroupId \
-
--filter "Name=group-name,Values=$sec_grp_name" \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
}
-
-
function get_instance_by_name(){
-
instance_name=$1
-
echo `aws ec2 describe-instances \
-
--filter "Name=tag:Name,Values=$instance_name" \
-
--query Reservations[].Instances[].InstanceId \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
}
-
-
function run_instances(){
-
-
HOSTED_ZONE_ID=`aws route53 list-hosted-zones --query "HostedZones[?Name=='$DNS_ZONE_NAME.'].Id" --output text`
-
-
for instance in $INSTANCES
-
do
-
tag_name=`echo $instance|cut -d"|" -f1`
-
type=`echo $instance|cut -d"|" -f2`
-
sec_grp_name=`echo $instance|cut -d"|" -f3`
-
subnet_cidr=`echo $instance|cut -d"|" -f4`
-
need_public_ip=`echo $instance|cut -d"|" -f5`
-
disk_size=`echo $instance|cut -d"|" -f6`
-
# echo $tag_name, $type, $sec_grp_name, $subnet_cidr
-
-
subnet_id=`get_subnet_id $subnet_cidr`
-
sec_grp_id=`get_sec_grp_id $sec_grp_name`
-
-
echo "creating $tag_name::($type|$sec_grp_name|$subnet_cidr)"
-
instance_id=`get_instance_by_name $tag_name`
-
-
device_mapping=`sed s/{{size}}/$disk_size/g ./device-mapping.json`
-
-
if [ "$instance_id" == "" ]; then
-
echo "$tag_name does not exist, creating $tag_name instance...."
-
instance_id=$(aws ec2 run-instances --image-id $BASE_AMI \
-
--count 1 \
-
--instance-type $type \
-
--key-name stage \
-
--security-group-ids $sec_grp_id \
-
--subnet-id $subnet_id \
-
--output table \
-
--disable-api-termination \
-
--associate-public-ip-address \
-
--instance-initiated-shutdown-behavior stop \
-
--block-device-mappings "$device_mapping" \
-
#### --user-data "$userdata" \
-
--output text \
-
--query Instances[].InstanceId \
-
--region $REGION \
-
--profile $REGION)
-
echo "instance created: $tag_name ($instance_id)"
-
else
-
echo "$tag_name ($instance_id) existe, skipping...."
-
fi
-
-
aws ec2 create-tags --resources $instance_id --tags Key=Name,Value=$tag_name --output table --region $REGION --profile $REGION
-
-
echo "waiting for 5 seconds..."
-
sleep 5
-
private_ip=`aws ec2 describe-instances --instance-ids $instance_id --query Reservations[].Instances[].PrivateIpAddress --output text --region $REGION --profile $REGION`
-
-
dns_file_name="$tag_name"_dns.json
-
cp ./dns.json $dns_file_name
-
-
sed -i "s/{{hostname}}/$tag_name/g" $dns_file_name
-
sed -i "s/{{domain}}/$DNS_ZONE_NAME/g" $dns_file_name
-
sed -i "s/{{ipaddress}}/$private_ip/g" $dns_file_name
-
-
# cat $dns_file_name
-
aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch file://$dns_file_name --output table
-
rm -rf $dns_file_name
-
-
volume_id=`aws ec2 describe-volumes \
-
--filter "Name=attachment.instance-id,Values=$instance_id" \
-
--query Volumes[].VolumeId \
-
--output text \
-
--region=$REGION \
-
--profile=$REGION`
-
-
if [ "$volume_id" != "" ]; then
-
aws ec2 create-tags --resources $volume_id --tags Key=Name,Value=$tag_name --output table --region $REGION --profile $REGION
-
fi
-
-
if [ "$need_public_ip" == 'true' ];then
-
ip_own_id=`aws ec2 describe-instances \
-
--instance-ids $instance_id \
-
--query Reservations[].Instances[].NetworkInterfaces[].Association.IpOwnerId \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
-
public_ip=`aws ec2 describe-instances \
-
--instance-ids $instance_id \
-
--query Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
-
if [ "$ip_own_id" == "amazon" ]; then
-
aws ec2 wait instance-running --instance-ids $instance_id
-
eip_id=`aws ec2 allocate-address --domain vpc --query AllocationId --output text --region $REGION --profile $REGION`
-
aws ec2 associate-address --instance-id $instance_id --allocation-id $eip_id --output table --region $REGION --profile $REGION
-
public_ip=`aws ec2 describe-addresses \
-
--filter "Name=allocation-id,Values=$eip_id" \
-
--query Addresses[].PublicIp \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
fi
-
-
echo "$tag_name have associate-address:$public_ip"
-
fi
-
done
-
}
-
-
run_instances
-
echo "done"
#device-mapping.json and dns.json
-
[
-
{
-
"DeviceName": "/dev/sda1",
-
"Ebs": {
-
"DeleteOnTermination": false,
-
"VolumeSize": {{size}},
-
"VolumeType": "gp2"
-
}
-
}
-
]
-
{
-
"Comment": "",
-
"Changes": [
-
{
-
"Action": "CREATE",
-
"ResourceRecordSet": {
-
"Name": "{{hostname}}.{{domain}}",
-
"Type": "A",
-
"TTL": 600,
-
"ResourceRecords": [
-
{
-
"Value": "{{ipaddress}}"
-
}
-
]
-
}
-
}
-
]
-
}
# start/stop
-
#!/bin/bash
-
REGION=cn-north-1
-
servers="
-
autopush-server-1a-1
-
autopush-server-1a-2
-
"
-
-
function get_instance_by_name(){
-
instance_name=$1
-
echo `aws ec2 describe-instances \
-
--filter "Name=tag:Name,Values=$instance_name" \
-
--query Reservations[].Instances[].InstanceId \
-
--output text \
-
--region $REGION \
-
--profile $REGION`
-
}
-
-
for server in $servers
-
do
-
echo $server
-
server_id=`get_instance_by_name $server`
-
echo $server_id
-
aws ec2 start-instances --instance-ids $server_id --output table
-
# aws ec2 stop-instances --instance-ids $server_id --output table
-
echo "$server started!"
-
done
总的来说,没啥别的,就是参考文档写,比较繁琐。
阅读(1470) | 评论(0) | 转发(0) |