$npx -y skills add chaterm/terminal-skills --skill aws-cliAWS CLI 操作
| 1 | # AWS CLI 操作 |
| 2 | |
| 3 | ## 概述 |
| 4 | EC2、S3、IAM、Lambda 等 AWS 服务的命令行操作技能。 |
| 5 | |
| 6 | ## 配置与认证 |
| 7 | |
| 8 | ```bash |
| 9 | # 配置凭证 |
| 10 | aws configure |
| 11 | aws configure --profile myprofile |
| 12 | |
| 13 | # 查看配置 |
| 14 | aws configure list |
| 15 | aws configure list --profile myprofile |
| 16 | |
| 17 | # 配置文件位置 |
| 18 | ~/.aws/credentials |
| 19 | ~/.aws/config |
| 20 | |
| 21 | # 使用环境变量 |
| 22 | export AWS_ACCESS_KEY_ID=xxx |
| 23 | export AWS_SECRET_ACCESS_KEY=xxx |
| 24 | export AWS_DEFAULT_REGION=us-east-1 |
| 25 | |
| 26 | # 使用 profile |
| 27 | export AWS_PROFILE=myprofile |
| 28 | aws s3 ls --profile myprofile |
| 29 | |
| 30 | # 获取当前身份 |
| 31 | aws sts get-caller-identity |
| 32 | ``` |
| 33 | |
| 34 | ## EC2 实例 |
| 35 | |
| 36 | ### 实例管理 |
| 37 | ```bash |
| 38 | # 列出实例 |
| 39 | aws ec2 describe-instances |
| 40 | aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" |
| 41 | aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]' --output table |
| 42 | |
| 43 | # 启动/停止实例 |
| 44 | aws ec2 start-instances --instance-ids i-1234567890abcdef0 |
| 45 | aws ec2 stop-instances --instance-ids i-1234567890abcdef0 |
| 46 | aws ec2 reboot-instances --instance-ids i-1234567890abcdef0 |
| 47 | |
| 48 | # 终止实例 |
| 49 | aws ec2 terminate-instances --instance-ids i-1234567890abcdef0 |
| 50 | |
| 51 | # 创建实例 |
| 52 | aws ec2 run-instances \ |
| 53 | --image-id ami-12345678 \ |
| 54 | --instance-type t3.micro \ |
| 55 | --key-name my-key \ |
| 56 | --security-group-ids sg-12345678 \ |
| 57 | --subnet-id subnet-12345678 \ |
| 58 | --count 1 |
| 59 | ``` |
| 60 | |
| 61 | ### 安全组 |
| 62 | ```bash |
| 63 | # 列出安全组 |
| 64 | aws ec2 describe-security-groups |
| 65 | aws ec2 describe-security-groups --group-ids sg-12345678 |
| 66 | |
| 67 | # 创建安全组 |
| 68 | aws ec2 create-security-group \ |
| 69 | --group-name my-sg \ |
| 70 | --description "My security group" \ |
| 71 | --vpc-id vpc-12345678 |
| 72 | |
| 73 | # 添加入站规则 |
| 74 | aws ec2 authorize-security-group-ingress \ |
| 75 | --group-id sg-12345678 \ |
| 76 | --protocol tcp \ |
| 77 | --port 22 \ |
| 78 | --cidr 0.0.0.0/0 |
| 79 | |
| 80 | # 删除规则 |
| 81 | aws ec2 revoke-security-group-ingress \ |
| 82 | --group-id sg-12345678 \ |
| 83 | --protocol tcp \ |
| 84 | --port 22 \ |
| 85 | --cidr 0.0.0.0/0 |
| 86 | ``` |
| 87 | |
| 88 | ## S3 存储 |
| 89 | |
| 90 | ### 基础操作 |
| 91 | ```bash |
| 92 | # 列出桶 |
| 93 | aws s3 ls |
| 94 | |
| 95 | # 列出对象 |
| 96 | aws s3 ls s3://bucket-name/ |
| 97 | aws s3 ls s3://bucket-name/prefix/ --recursive |
| 98 | |
| 99 | # 创建桶 |
| 100 | aws s3 mb s3://bucket-name |
| 101 | aws s3 mb s3://bucket-name --region us-west-2 |
| 102 | |
| 103 | # 删除桶 |
| 104 | aws s3 rb s3://bucket-name |
| 105 | aws s3 rb s3://bucket-name --force # 包括内容 |
| 106 | ``` |
| 107 | |
| 108 | ### 文件操作 |
| 109 | ```bash |
| 110 | # 上传文件 |
| 111 | aws s3 cp file.txt s3://bucket-name/ |
| 112 | aws s3 cp file.txt s3://bucket-name/path/file.txt |
| 113 | |
| 114 | # 下载文件 |
| 115 | aws s3 cp s3://bucket-name/file.txt ./ |
| 116 | aws s3 cp s3://bucket-name/path/ ./ --recursive |
| 117 | |
| 118 | # 同步目录 |
| 119 | aws s3 sync ./local-dir s3://bucket-name/prefix/ |
| 120 | aws s3 sync s3://bucket-name/prefix/ ./local-dir |
| 121 | aws s3 sync ./local-dir s3://bucket-name/ --delete # 删除目标多余文件 |
| 122 | |
| 123 | # 删除对象 |
| 124 | aws s3 rm s3://bucket-name/file.txt |
| 125 | aws s3 rm s3://bucket-name/prefix/ --recursive |
| 126 | |
| 127 | # 移动/重命名 |
| 128 | aws s3 mv s3://bucket-name/old.txt s3://bucket-name/new.txt |
| 129 | ``` |
| 130 | |
| 131 | ### 高级操作 |
| 132 | ```bash |
| 133 | # 预签名 URL |
| 134 | aws s3 presign s3://bucket-name/file.txt --expires-in 3600 |
| 135 | |
| 136 | # 设置公开访问 |
| 137 | aws s3api put-object-acl --bucket bucket-name --key file.txt --acl public-read |
| 138 | |
| 139 | # 查看桶策略 |
| 140 | aws s3api get-bucket-policy --bucket bucket-name |
| 141 | |
| 142 | # 设置生命周期 |
| 143 | aws s3api put-bucket-lifecycle-configuration \ |
| 144 | --bucket bucket-name \ |
| 145 | --lifecycle-configuration file://lifecycle.json |
| 146 | ``` |
| 147 | |
| 148 | ## IAM 管理 |
| 149 | |
| 150 | ```bash |
| 151 | # 列出用户 |
| 152 | aws iam list-users |
| 153 | |
| 154 | # 创建用户 |
| 155 | aws iam create-user --user-name myuser |
| 156 | |
| 157 | # 创建访问密钥 |
| 158 | aws iam create-access-key --user-name myuser |
| 159 | |
| 160 | # 附加策略 |
| 161 | aws iam attach-user-policy \ |
| 162 | --user-name myuser \ |
| 163 | --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess |
| 164 | |
| 165 | # 列出角色 |
| 166 | aws iam list-roles |
| 167 | |
| 168 | # 获取角色 |
| 169 | aws iam get-role --role-name myrole |
| 170 | ``` |
| 171 | |
| 172 | ## Lambda 函数 |
| 173 | |
| 174 | ```bash |
| 175 | # 列出函数 |
| 176 | aws lambda list-functions |
| 177 | |
| 178 | # 调用函数 |
| 179 | aws lambda invoke \ |
| 180 | --function-name my-function \ |
| 181 | --payload '{"key": "value"}' \ |
| 182 | output.json |
| 183 | |
| 184 | # 查看函数配置 |
| 185 | aws lambda get-function --function-name my-function |
| 186 | |
| 187 | # 更新函数代码 |
| 188 | aws lambda update-function-code \ |
| 189 | --function-name my-function \ |
| 190 | --zip-file fileb://function.zip |
| 191 | |
| 192 | # 查看日志 |
| 193 | aws logs filter-log-events \ |
| 194 | --log-group-name /aws/lambda/my-function \ |
| 195 | --start-time $(date -d '1 hour ago' +%s)000 |
| 196 | ``` |
| 197 | |
| 198 | ## EKS 集群 |
| 199 | |
| 200 | ```bash |
| 201 | # 列出集群 |
| 202 | aws eks list-clusters |
| 203 | |
| 204 | # 获取集群信息 |
| 205 | aws eks describe-cluster --name my-cluster |
| 206 | |
| 207 | # 更新 kubeconfig |
| 208 | aws eks update-kubeconfig --name my-cluster --region us-east-1 |
| 209 | |
| 210 | # 列出节点组 |
| 211 | aws eks list-nodegroups --cluster-name my-cluster |
| 212 | ``` |
| 213 | |
| 214 | ## 常见场景 |
| 215 | |
| 216 | ### 场景 1:批量操作实例 |
| 217 | ```bash |
| 218 | # 获取所有运行中实例 ID |
| 219 | aws ec2 describe-instances \ |
| 220 | --filters "Name=instance-state-name,Values=running" \ |
| 221 | --query 'Reservations[*].Instances[*].InstanceId' \ |
| 222 | --output text |
| 223 | |
| 224 | # 批量停止 |
| 225 | aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances \ |
| 226 | --filters "Name=tag:Environment,Values=dev" \ |
| 227 | --query 'Reservations[*].Instances[*].InstanceId' \ |
| 228 | --output text) |
| 229 | ``` |
| 230 | |
| 231 | ### 场景 2:S3 大文件传输 |
| 232 | ```bash |
| 233 | # 多部分上传(自动) |
| 234 | aws s3 cp large-file.zip s3://bucket-name/ \ |
| 235 | --storage-class STANDARD_IA |
| 236 | |
| 237 | # 配置传输加速 |
| 238 | aws s3api put-bucket-accelerate-configuration \ |
| 239 | --bucket bucket-name \ |
| 240 | --accelerate-configuration Status=Enabled |
| 241 | ``` |
| 242 | |
| 243 | ### 场景 3:查询 CloudWatch 日志 |
| 244 | ```bash |
| 245 | # 查询日志 |
| 246 | aws logs filter-log-events \ |
| 247 | --log-group-name /aws/lambda/my-function \ |
| 248 | --filter-pattern "ERROR" \ |
| 249 | --start-time $(date -d '1 hour ago' +%s)000 |
| 250 | |
| 251 | # 实时跟踪 |
| 252 | aws |