AWS 模块 0:安全账户设置(Zero to Architect)

发布: (2025年12月12日 GMT+8 09:33)
6 min read
原文: Dev.to

Source: Dev.to

介绍

本教程一步步指导你从零安全地设置 AWS 账户,重点包括:

  • 安全性: 防止未经授权的访问。
  • 成本控制: 避免账单意外。
  • 最佳实践: 从第 1 天起就使用专业配置。

重要提示: 本教程假设你使用的是公共仓库。所有配置均旨在 绝不泄露密钥

基本概念

元素推荐用法备注
Root User仅用于首次初始化具备全部权限;若被泄露风险极高。
IAM User日常工作按最小权限原则(Least Privilege)分配权限。
MFARoot 必须使用,强烈建议 IAM 也使用增加额外安全层(密码 + 临时验证码)。

步骤 1 – 保护 Root 用户

  1. 进入控制台并使用 Root 用户登录。
  2. 在右上角点击你的名称 → Security credentials
  3. Multi-factor authentication (MFA) 部分,选择 Assign MFA device 并挑选:
    • Authenticator app(推荐入门)

    • Security key(YubiKey 或其他)

    • 用 Google Authenticator、Authy 或 Microsoft Authenticator 扫描二维码。

    • 输入两个连续的 MFA 代码(每次间隔 30 秒)。

  4. 将恢复代码 保存在安全的实体位置。

创建账户别名

  1. 在同一页面,找到 Account Alias
  2. 创建一个易记的别名(例如 mi-startup-prodpersonal-aws)。
  3. 别名会生成更友好的登录 URL:https://<alias>.signin.aws.amazon.com

从现在起,绝不要使用 Root 用户进行日常工作。

步骤 2 – 创建 IAM 用户

  1. 在控制台搜索 IAMUsersCreate user
  2. 配置:
    • User name: juan-admin(或你喜欢的名称)。
    • Access type: 勾选 Provide user access to the AWS Management Console
    • Console password: 选择 Custom password(强密码)或 Auto‑generated
    • 取消勾选 “Users must create a new password at next sign‑in”(为简化教程)。
  3. 分配权限:
    • Attach policies directly 中搜索并勾选 AdministratorAccess(用于学习)。
    • 生产环境请使用更细粒度的权限(例如 AmazonS3FullAccessAWSLambdaFullAccess 等)。
  4. 点击 NextCreate user
  5. 下载 包含凭证的 CSV 文件,并将其保存到密码管理器(1Password、Bitwarden 等)。
  6. 为新 IAM 用户设置 MFA,步骤同 Root 用户(“Multi‑factor authentication (MFA)” 部分)。
  7. 注销 Root 用户,使用账户别名和新建的 IAM 用户重新登录。

步骤 3 – 配置计费警报

  1. 使用 Root 会话,进入 AccountBilling preferencesEdit
  2. 启用:
    • Receive Free Tier Usage Alerts
    • Receive Billing Alerts
  3. 输入你的电子邮件并保存更改。
  4. 使用 IAM 用户,打开 AWS Budgets(或 Billing → Budgets) → Create budget
    • 模板: Zero spend budget(推荐)。
    • 预算名称: Alerta-Costo-Minimo
    • 周期: Monthly
    • 预算金额: 0.01 USD
    • 范围: All AWS services
  5. 配置警报:
    • 警报 1 – 实际费用: 阈值 Actual costs – 100%(当达到 $0.01 时)。
    • 警报 2 – 预测费用(可选): 阈值 Forecasted costs – 80%
  6. 将你的邮箱添加到每个警报。AWS 会在接近或超出 Free Tier 限额时发送通知。

步骤 4 – 安装 AWS CLI

macOS

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

Linux

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Windows

  1. 下载 MSI 安装程序。
  2. 运行安装程序。

验证安装

aws --version
# Expected output: aws-cli/2.x.x Python/3.x.x ...

注意: 只为你的 IAM 用户安装 CLI,绝不要为 Root 安装。

步骤 5 – 保护你的代码仓库

5.1 为 CLI 创建 Access Key

  1. 在 AWS 控制台(IAM 用户) → Users → 你的用户 → Security credentials
  2. Access keys 中点击 Create access key
  3. 勾选 “I understand…” → Next,并(可选)添加描述(CLI-Local-DevMachine)。
  4. 下载 CSV(仅显示一次)。

5.2 配置 CLI 配置文件

aws configure --profile mi-proyecto

按 CSV 中的内容输入:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name(例如 us-east-1
  • Default output formatjson 或留空)

文件会保存至:

  • macOS/Linux: ~/.aws/credentials~/.aws/config
  • Windows: C:\Users\<user>\.aws\credentials

5.3 为配置文件设置环境变量

Bash/Zsh

echo 'export AWS_PROFILE=mi-proyecto' >> ~/.zshrc
source ~/.zshrc

Fish

echo 'set -gx AWS_PROFILE mi-proyecto' >> ~/.config/fish/config.fish
source ~/.config/fish/config.fish

PowerShell

[System.Environment]::SetEnvironmentVariable('AWS_PROFILE', 'mi-proyecto', 'User')

验证身份

aws sts get-caller-identity

预期输出:

{
    "UserId": "AIDAXXXXXXXXXX",
    "Account": "123456789012",
    "Arn": "arn:aws:iam::123456789012:user/juan-admin"
}
  • Arn 包含 :user/ → 正常。
  • Arn 包含 :root → 正在使用 Root(错误)。

5.4 .gitignore(关键)

在项目根目录创建 .gitignore,内容如下最小示例:

# === AWS 与 TERRAFORM 机密 ===
*.tfvars
*.tfstate
*.tfstate.*
.terraform/
.terraform.lock.hcl

# === 凭证文件 ===
.env
.env.*
*.pem
*.key
aws_credentials.txt
credentials.json
config.json

# === GOLANG ===
*.exe
*.dll
*.so
*.dylib
*.test
*.out
vendor/
go.work

# === 系统文件 ===
.DS_Store
Thumbs.db

# === 编辑器 ===
.vscode/
.idea/
*.swp
*.swo
*~

运行 git status,确认 没有 .tfstate.env.pem.key.terraform/ 等文件被列出。

5.5 Pre‑commit Hook(可选但推荐)

安装 pre-commit

# macOS
brew install pre-commit

# Linux / Windows
pip install pre-commit

创建 .pre-commit-config.yaml

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: check-added-large-files
      - id: detect-aws-credentials
      - id: detect-private-key

  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.83.5
    hooks:
      - id: terraform_fmt
      - id: terraform_validate

激活 Hook:

pre-commit install

最终检查

  • Root 用户已启用 MFA。
  • Root 用户 用于日常工作。
  • 已创建 IAM 用户并赋予 AdministratorAccess(用于学习)。
  • IAM 用户已启用 MFA。
  • 已配置并测试计费警报。
  • AWS CLI 已使用安全配置文件完成配置。
  • .gitignore 已屏蔽所有敏感文件。
  • (可选)Pre‑commit Hook 已启用。

其他资源

  • AWS IAM 官方文档
  • AWS MFA 使用指南
  • AWS Budgets
  • AWS CLI 参考手册
  • Terraform 的 Pre‑commit Hook
Back to Blog

相关文章

阅读更多 »