我正在为创建一个使用新AWS-CDK创建的Aurora无服务器DB群集(基于Java或Typescript)创建的示例。此外
我的第一次尝试是用
创建这个software.amazon.awscdk.services.rds.DatabaseCluster
例如
DatabaseCluster databaseCluster = new DatabaseCluster(this, "myDbCluster",
DatabaseClusterProps.builder()
.withEngine(DatabaseClusterEngine.Aurora)
,但根据云形式,您必须将属性engineMode
设置为serverless
。CDK版本0.24.1中不支持属性engineMode
。另请参见Open CDK问题929。
对我的解决方法是使用software.amazon.awscdk.services.rds.CfnDBCluster
创建构造的。请参阅示例代码:
new CfnDBCluster(this, "myDBCluster", CfnDBClusterProps.builder()
.withEngine("aurora")
.withEngineMode("serverless")
.withPort(3306)
.withMasterUsername("masterUserName")
.withMasterUserPassword("***********************")
.withScalingConfiguration(ScalingConfigurationProperty.builder()
.withAutoPause(true)
.withMinCapacity(2)
.withMaxCapacity(16)
.withSecondsUntilAutoPause(300)
.build())
.build());
另请参见:
从CloudFormation创建Aurora无服务器集群?
awscloudformation/laste/userguide/aws-resource-rds-dbcluster.html
https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-rds.html
我知道您要求使用Java或打字稿示例,但是我的母语是Python,所以我只为您提供一个Python示例。Amazon文档确实提供了有关如何将CDK代码从一种语言"转换"到另一种语言的更多详细信息 - 相当简单。请参阅https://docs.aws.amazon.com/cdk/latest/guide/multiple_languages.html
以下示例创建了一个带有PostgreSQL引擎的极光群集。它创建了带有密码的管理用户(" admin"),该密码是从(以前创建的)SSM Secure String参数检索的。它还在称为" mydatabase"的群集中创建一个初始数据库。
它也(首先)创建了一个lambda,假设处理程序的代码已经在lambda-handler.py
中,则将其放在安全组中,并确保Aurora将DB端口公开到Lambda Security Group。
所有这些都是在VPC的上下文中。
代码如下:
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_rds as rds
from aws_cdk import aws_lambda as lambda_
from aws_cdk import core
class DataProcessingRTStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = ec2.Vpc.from_lookup(self, 'VPC', vpc_name="my_vpc")
lambda_sg = kwargs.pop('lambda_sg', None)
with open("lambda-handler.py", encoding="utf8") as fp:
handler_code = fp.read()
lambda_fn = lambda_.Function(
self, "DB_Lambda",
code=lambda_.InlineCode(handler_code),
handler="index.main",
timeout=core.Duration.seconds(300),
runtime=lambda_.Runtime.PYTHON_3_7,
security_group=lambda_sg,
)
aurora_sg = ec2.SecurityGroup(
self, 'AUPG-SG',
vpc=vpc,
description="Allows PosgreSQL connections from Lambda SG",
)
aurora_cluster = rds.DatabaseCluster(
self, "AUPG-CLUSTER-1",
engine=rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
engine_version="10.7",
master_user=rds.Login(
username='admin',
password=core.SecretValue.ssm_secure('AUPG.AdminPass', version='1'),
),
default_database_name='MyDatabase',
instance_props=rds.InstanceProps(
instance_type=ec2.InstanceType.of(
ec2.InstanceClass.MEMORY5, ec2.InstanceSize.XLARGE,
),
vpc=vpc,
security_group=aurora_sg,
),
parameter_group=
rds.ClusterParameterGroup.from_parameter_group_name(
self, "AUPG-ParamGroup-1",
parameter_group_name="default.aurora-postgresql10",
)
)
aurora_cluster.connections.allow_from(
connectors_sg, ec2.Port.tcp(3306),
"Allow MySQL access from Lambda (because Aurora actually exposes PostgreSQL on port 3306)",
)