Administrator
发布于 2025-01-06 / 11 阅读
0
0

aws-cli教程

参考:https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/cli-chap-welcome.html

安装aws-cli

# 参考:https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
# 如果是Amazon Linux,卸载旧版本
sudo yum remove awscli

# 安装 aws-cli
sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
sudo aws --version

# 配置aws
[ec2-user@ip-172-31-12-180 ~]$ sudo aws configure
AWS Access Key ID [None]: AKIAV********OORENCW
AWS Secret Access Key [None]: tUPldS*******************3e9KH1f
Default region name [None]: ap-east-1
Default output format [None]: json

# 格式
aws <command> <subcommand> [options and parameters]

使用示例

#公有子网中启动ec2实例测试
#启动ec2
aws ec2 run-instances \
--region $region \
--count 1 \
--instance-name $instance_name \
--instance-type t3.micro \
--subnet-id ${subnet_id} \
--security-group-ids $sg_id \
--key-name $ssh_secret \
--image-id ami-0afd8196c98fcbd00  \
 |tee /tmp/ec2.log
 
ec2_id=$(jq -r ".Instances[0].InstanceId" /tmp/ec2.log)
 
#查看ec2启动状态
aws ec2 describe-instances --instance-id ${ec2_id} \
--query "Reservations[*].Instances[*].{State:State.Name,Address:PublicIpAddress}"
 
#等待实例状态为running,若没公网IP,可以申请EIP后绑定
aws ec2 allocate-address --region $region |tee /tmp/eip3.log
eip3=$(jq -r ".PublicIp" /tmp/eip3.log)
eip3_id=$(jq -r ".AllocationId" /tmp/eip3.log)
aws ec2 associate-address --instance-id ${ec2_id} --allocation-id ${eip3_id}

aws ec2 associate-address --instance-id i-0a0e921cfb0c23052 --allocation-id eipalloc-05e8d695b2400e62e
 
#ssh登录
#ssh -i ${ssh_secret}.pem ec2@${eip3}


##若为测试, 则删除创建的实例
##ec2
#aws ec2 terminate-instances --instance-ids ${ec2_id}


评论