前面五天我們討論了很多架構和理論,今天開始要動手設定了
今天我們要從最基礎開始:
讓我們一步步實際操作,Kyo-System 部署到 AWS!
重要提醒:
⚠️ 安全提醒:
為了安全性,我們要建立一個專門用於開發的 IAM 使用者。
搜尋並進入 IAM 服務
建立使用者
IAM > 使用者 > 建立使用者
設定使用者詳細資訊
kyo-developer
設定權限
AdministratorAccess
⚠️ 注意:在生產環境中要使用最小權限原則,但開發階段為了方便,我們先用管理員權限。
檢查並建立
進入使用者詳細資料
kyo-developer
使用者建立存取金鑰
安全憑證標籤 > 存取金鑰 > 建立存取金鑰
選擇使用案例
設定描述標籤(可選)
Kyo-System 開發環境
下載憑證
Access Key ID
:AKIA...Secret Access Key
:只會顯示這一次!# 下載 AWS CLI V2 for macOS
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
# 安裝
sudo installer -pkg AWSCLIV2.pkg -target /
# 如果你有 Homebrew
brew install awscli
aws --version
# 應該顯示:aws-cli/2.x.x
aws configure
系統會詢問:
AWS Access Key ID [None]: AKIA... (你的 Access Key ID)
AWS Secret Access Key [None]: ... (你的 Secret Access Key)
Default region name [None]: ap-northeast-1
Default output format [None]: json
區域選擇建議:
ap-east-2
(台北)# 測試 AWS CLI 是否正常工作
aws sts get-caller-identity
應該會顯示類似這樣的結果:
{
"UserId": "AIDA...",
"Account": "123456789012",
"Arn": "arn:aws:iam::123456789012:user/kyo-developer"
}
# 檢查是否已安裝 Node.js
node --version
# 如果沒有安裝,前往 https://nodejs.org/ 下載
# 或使用 Homebrew 安裝
brew install node
要求版本:Node.js 18 或更高,我是使用v22.15.0
# 全域安裝 CDK CLI
npm install -g aws-cdk
# 驗證安裝
cdk --version
# 進入你的專案目錄
cd /path/to/kyong-saas
# 安裝依賴(如果還沒安裝的話)
pnpm install
# 進入 CDK 目錄
cd infrastructure
確認 infra/cdk/lib/kyo-infrastructure-stack.ts
的內容:
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as elasticache from 'aws-cdk-lib/aws-elasticache';
import { Construct } from 'constructs';
export class KyoInfrastructureStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// VPC - 我們的虛擬私有雲
const vpc = new ec2.Vpc(this, 'KyoVpc', {
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
maxAzs: 2,
subnetConfiguration: [
{
cidrMask: 24,
name: 'PublicSubnet',
subnetType: ec2.SubnetType.PUBLIC,
},
{
cidrMask: 24,
name: 'PrivateSubnet',
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
{
cidrMask: 24,
name: 'DatabaseSubnet',
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
}
]
});
// PostgreSQL 資料庫
const database = new rds.DatabaseInstance(this, 'KyoDatabase', {
engine: rds.DatabaseInstanceEngine.postgres({
version: rds.PostgresEngineVersion.VER_15_3
}),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MICRO),
vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_ISOLATED
},
databaseName: 'kyo',
credentials: rds.Credentials.fromGeneratedSecret('kyoadmin'),
allocatedStorage: 20,
deleteAutomatedBackups: false,
deletionProtection: false // 開發環境設為 false,生產環境要設 true
});
// Redis (ElastiCache)
const redisSubnetGroup = new elasticache.CfnSubnetGroup(this, 'KyoRedisSubnetGroup', {
description: 'Subnet group for Kyo Redis',
subnetIds: vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnetIds
});
const redis = new elasticache.CfnCacheCluster(this, 'KyoRedis', {
cacheNodeType: 'cache.t3.micro',
engine: 'redis',
numCacheNodes: 1,
cacheSubnetGroupName: redisSubnetGroup.ref
});
// 輸出重要資訊
new cdk.CfnOutput(this, 'VpcId', {
value: vpc.vpcId,
description: 'VPC ID'
});
new cdk.CfnOutput(this, 'DatabaseEndpoint', {
value: database.instanceEndpoint.hostname,
description: 'RDS PostgreSQL endpoint'
});
new cdk.CfnOutput(this, 'RedisEndpoint', {
value: redis.attrRedisEndpointAddress,
description: 'ElastiCache Redis endpoint'
});
}
}
這是第一次使用 CDK 時必須執行的步驟:
# Bootstrap CDK(只需執行一次)
cdk bootstrap
# 如果指定區域
cdk bootstrap aws://你的帳號ID/ap-northeast-1
Bootstrap 會建立:
# 合成 CloudFormation 模板
cdk synth
這會顯示將要建立的所有 AWS 資源。
# 部署 Kyo Infrastructure
cdk deploy
CDK 會詢問:
Do you wish to deploy these changes (y/n)? y
部署過程大約需要 10-15 分鐘,因為要建立:
部署完成後,你會看到輸出資訊:
Outputs:
KyoInfrastructureStack.VpcId = vpc-abc123...
KyoInfrastructureStack.DatabaseEndpoint = kyo-database.abc123.ap-northeast-1.rds.amazonaws.com
KyoInfrastructureStack.RedisEndpoint = kyo-redis.abc123.cache.amazonaws.com
VPC:
AWS 控制台 > VPC > 你的 VPC
RDS:
AWS 控制台 > RDS > 資料庫 > kyo-database
ElastiCache:
AWS 控制台 > ElastiCache > Redis 叢集
AWS 控制台 > CloudFormation > KyoInfrastructureStack
你可以看到所有建立的資源和它們的關係。
在 AWS 免費方案下:
💰 省錢提醒:如果你不需要 private subnet 連外網,可以移除 NAT Gateway 來節省費用。
User: arn:aws:iam::xxx:user/kyo-developer is not authorized to perform: xxx
解決方案:確認 IAM 使用者有 AdministratorAccess
權限。
The requested configuration is currently not supported in this region
解決方案:改用 us-east-1
或 ap-northeast-1
區域。
You have exceeded the maximum number of VPCs
解決方案:刪除不用的 VPC 或申請配額增加。
CDK version mismatch
解決方案:
npm install -g aws-cdk@latest
當你完成測試後,記得清理資源以避免產生費用:
# 刪除整個 Stack
cdk destroy
# 確認刪除
Do you really want to delete: KyoInfrastructureStack (y/n)? y
明天(Day 7)我們將:
✅ AWS 帳號建立完成
✅ IAM 使用者和存取金鑰設定完成
✅ AWS CLI V2 安裝並設定完成
✅ CDK 安裝完成
✅ 基礎網路架構部署完成
✅ PostgreSQL 和 Redis 運行中
提醒:記得定期檢查 AWS 帳單,確保沒有意外費用產生。在免費方案範圍內使用是最安全的做法。