我們 EC2、LB、ASG 與 CloudFront 都說明了剩下一個架站機乎一定要用到的服務還沒講到那就是 AWS 提供的資料庫 RDS
在 AWS RDS 裡面除了我們常用的 MySQL、MariaDB 與 PostgreSQL 還有 Oracle 與 Microsoft SQL Server 可以選擇,而其實 AWS 還有自己研發一個資料庫叫做 Amazon Aurora
Amazon Aurora 的特色是他可以同時相容 MySQL 和 PostgreSQL,官方文件說在速度上比 MySQL 資料庫快五倍的速度,比標準 PostgreSQL 資料庫快三倍的速度而且價格是 1/10
以網站來說通常比較常使用 MySQL,所以這邊使用 MySQL 做介紹
首先我們先來理解最簡單建立 RDS 的方法,而且為了方便好測試我們先把它放在 Public Subnet
其實密碼管理在 AWS 比較推薦使用 AWS Secrets Manager,不過這邊因為使用明碼直接說明比較簡單,所以就先直接放入密碼,AWS Secrets Manager 就放到後面的章節說明吧!
千萬要記得正式使用請勿把資料庫放在 Public Subnet
import * as rds from "@aws-cdk/aws-rds";
const rdsSecurityGroup = new ec2.SecurityGroup(this, "RdsSecurityGroup", {
vpc,
});
rdsSecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.tcp(3306),
"allow public mysql access"
);
const instance = new rds.DatabaseInstance(this, "Database", {
engine: rds.DatabaseInstanceEngine.mysql({
version: rds.MysqlEngineVersion.VER_8_0_19,
}),
vpc,
deleteAutomatedBackups: true,
instanceType: ec2.InstanceType.of(
ec2.InstanceClass.T3,
ec2.InstanceSize.MICRO
),
allocatedStorage: 10,
credentials: {
username: "admin",
password: cdk.SecretValue.plainText("password"),
},
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
},
securityGroups: [rdsSecurityGroup],
});
new cdk.CfnOutput(this, "RDS", {
value: instance.dbInstanceEndpointAddress,
});
使用以上的方法就可以建立一個 MySQL 版本 8.0.19 的資料庫拉!
CdkRdsStack.RDS = cd1c72041shs1y1.cub7t8ijymvz.us-west-2.rds.amazonaws.com
$ mysql -h cd1c72041shs1y1.cub7t8ijymvz.us-west-2.rds.amazonaws.com -u admin -p 1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.19 Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
已正式來說比較標準的架構我們會把 RDS 放在 private subnet 畢竟資料庫平常可以存取的只有我們內網的機器可以存取
以實際使用來說通常會把 EC2 放在 public subnet 而 RDS 放在 private subnet 來保護它,所以我們這次的測試會使用 public 的 EC2 安裝 mysql client 來測試連線 private subnet 的 RDS
const vpc = new ec2.Vpc(this, "VPC", {
maxAzs: 3,
natGateways: 1,
});
const mySecurityGroup = new ec2.SecurityGroup(this, "SecurityGroup", {
vpc,
allowAllOutbound: true,
});
mySecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.tcp(22),
"allow public ssh access"
);
const ec2Instance = new ec2.Instance(this, "Instance", {
vpc,
instanceType: ec2.InstanceType.of(
ec2.InstanceClass.T3,
ec2.InstanceSize.NANO
),
machineImage: ec2.MachineImage.latestAmazonLinux({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
securityGroup: mySecurityGroup,
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
},
keyName: "KeyPair",
});
new cdk.CfnOutput(this, "EC2PublicDns", {
value: ec2Instance.instancePublicDnsName,
});
new cdk.CfnOutput(this, "EC2PublicIp", {
value: ec2Instance.instancePublicIp,
});
const instance = new rds.DatabaseInstance(this, "Database", {
engine: rds.DatabaseInstanceEngine.mysql({
version: rds.MysqlEngineVersion.VER_8_0_19,
}),
vpc,
deleteAutomatedBackups: true,
instanceType: ec2.InstanceType.of(
ec2.InstanceClass.T3,
ec2.InstanceSize.MICRO
),
allocatedStorage: 10,
credentials: {
username: "admin",
password: cdk.SecretValue.plainText("password"),
},
});
instance.connections.allowFrom(ec2Instance, ec2.Port.tcp(3306));
new cdk.CfnOutput(this, "RDS", {
value: instance.dbInstanceEndpointAddress,
});
$ sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
$ sudo yum install -y mysql-community-client
$ mysql -h cd1ayqd9sllf992.cub7t8ijymvz.us-west-2.rds.amazonaws.com -u admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.19 Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
以上就可以看到一個比較好的範例
這邊因為目前滿少使用的所以就簡單帶過拉 ~
const cluster = new rds.DatabaseCluster(this, "Database", {
engine: rds.DatabaseClusterEngine.auroraMysql({
version: rds.AuroraMysqlEngineVersion.VER_2_09_0,
}),
instances: 1,
instanceProps: {
instanceType: ec2.InstanceType.of(
ec2.InstanceClass.BURSTABLE3,
ec2.InstanceSize.SMALL
),
vpc,
},
credentials: {
username: "admin",
password: cdk.SecretValue.plainText("password"),
},
});
new cdk.CfnOutput(this, "RDS", {
value: cluster.clusterEndpoint.hostname,
});
我們可以到看看這份文件 How do I enable the EPEL repository for my Amazon EC2 instance running CentOS, RHEL, or Amazon Linux? 裡面有提到,如果是 Amazon Linux 2 請安裝 RHEL 7 版本
以目前 8.0 來說可以先參考文件 Installing MySQL on Linux Using the MySQL Yum Repository 就可以知道怎麼安裝了
$ sudo yum install mysql80-community-release-el7-{version-number}.noarch.rpm
$ sudo yum install -y mysql-community-client
今天的說明是 RDS 希望有幫助到大家!
文章內容主要是網路或是程式開發類型的文章
本文同步刊載於 Clarence 部落格:Day 18 - 使用 CDK 控制 Relational Database Service(RDS)
「AWS CDK 完全學習手冊:打造雲端基礎架構程式碼 IaC」
本書改編並延伸自第 12 屆 iT 邦幫忙鐵人賽獲得 DevOps 組冠軍的《用 CDK 定義 AWS 架構》系列文章,以簡單、好讀的行文風格詳述技術細節,並提供完整的程式碼範例與說明,一步一步帶領新手從零開始踏上 AWS CDK 技術達人之路。有興趣的朋友歡迎至天瓏書局選購!