使用 lambda 和 aurora postgresql 时如何处理数据库模式迁移?
How can I handle database schema migration when using lambda and aurora postgresql?
我正在 lambda 上部署应用程序并使用 aurora postgresql 作为数据库。在开发过程中,数据库架构变化非常频繁,我正在寻找一种迁移架构的方法。我知道 flyway 可以完成这项工作,但它适用于部署在 EC2 实例而不是 lambda 上的应用程序。在 lambda 中完成这项工作的最佳方法是什么?
我能想到一个解决方法。我的 lambda 在 typescript
所以它在 nodejs 环境中 运行。
我将 loopback4 与 lambda 结合使用,我创建了一个自定义资源,它将连接到 RDS 和 运行 迁移脚本以更新架构。
我正在使用 loopback4 创建我的模型和数据库架构。我有一个 AWS 自定义资源,它调用处理程序将架构迁移到 RDS。
这是您需要做的:
- 为 RDS 创建一个安全组
1.1.添加入站规则:允许来自 TCP 3306 上的 lambda SG
1.2.添加出站规则:允许所有协议,所有
上的所有端口
- 为 lambda 创建一个安全组
2.1.添加出站规则:允许所有协议,所有
上的所有端口
这是我使用 CDK 的代码:
/** Lambda Security Group */
const lambdaSecurityGroup = new SecurityGroup(this, "LambdaSecurityGroup", {
securityGroupName: "lambda-security-group",
description: "Lambda security group",
vpc: vpc,
allowAllOutbound: true,
});
/** Security Group */
const securityGroup = new SecurityGroup(this, "SecurityGroup", {
securityGroupName: "rds-security-group",
description: "instance security group",
vpc: vpc,
allowAllOutbound: true,
});
/** Security Group Inbound rules - Lambda security group*/
securityGroup.addIngressRule(
SecurityGroup.fromSecurityGroupId(
this,
"LambdaSecurityGroupId",
lambdaSecurityGroup.securityGroupId
),
Port.tcp(config.DatabasePort),
"Allow from Lambda security group on TCP 3306"
);
const customResourceMigrateProvider = new CustomResources.Provider(
this,
"CustomResourceMigrateProvider",
{
onEventHandler: new Function(this, "CustomResourceMigrateLambda", {
runtime: Runtime.NODEJS_12_X,
code: /*this.lambdaCode ||*/ Code.fromAsset("dist"),
handler: "loopback/handlers/custom-resource-migrate.handler",
timeout: Duration.seconds(30),
vpc: vpc,
vpcSubnets: { subnets: [appSubnet1aId, appSubnet1bId] },
securityGroups: [lambdaSecurityGroup],
environment: environmentVariables,
role: customRole,
layers: [layer],
}),
//isCompleteHandler: isComplete,
logRetention: logs.RetentionDays.ONE_DAY,
}
);
我正在 lambda 上部署应用程序并使用 aurora postgresql 作为数据库。在开发过程中,数据库架构变化非常频繁,我正在寻找一种迁移架构的方法。我知道 flyway 可以完成这项工作,但它适用于部署在 EC2 实例而不是 lambda 上的应用程序。在 lambda 中完成这项工作的最佳方法是什么?
我能想到一个解决方法。我的 lambda 在 typescript
所以它在 nodejs 环境中 运行。
我将 loopback4 与 lambda 结合使用,我创建了一个自定义资源,它将连接到 RDS 和 运行 迁移脚本以更新架构。
我正在使用 loopback4 创建我的模型和数据库架构。我有一个 AWS 自定义资源,它调用处理程序将架构迁移到 RDS。 这是您需要做的:
- 为 RDS 创建一个安全组 1.1.添加入站规则:允许来自 TCP 3306 上的 lambda SG 1.2.添加出站规则:允许所有协议,所有 上的所有端口
- 为 lambda 创建一个安全组 2.1.添加出站规则:允许所有协议,所有 上的所有端口
这是我使用 CDK 的代码:
/** Lambda Security Group */
const lambdaSecurityGroup = new SecurityGroup(this, "LambdaSecurityGroup", {
securityGroupName: "lambda-security-group",
description: "Lambda security group",
vpc: vpc,
allowAllOutbound: true,
});
/** Security Group */
const securityGroup = new SecurityGroup(this, "SecurityGroup", {
securityGroupName: "rds-security-group",
description: "instance security group",
vpc: vpc,
allowAllOutbound: true,
});
/** Security Group Inbound rules - Lambda security group*/
securityGroup.addIngressRule(
SecurityGroup.fromSecurityGroupId(
this,
"LambdaSecurityGroupId",
lambdaSecurityGroup.securityGroupId
),
Port.tcp(config.DatabasePort),
"Allow from Lambda security group on TCP 3306"
);
const customResourceMigrateProvider = new CustomResources.Provider(
this,
"CustomResourceMigrateProvider",
{
onEventHandler: new Function(this, "CustomResourceMigrateLambda", {
runtime: Runtime.NODEJS_12_X,
code: /*this.lambdaCode ||*/ Code.fromAsset("dist"),
handler: "loopback/handlers/custom-resource-migrate.handler",
timeout: Duration.seconds(30),
vpc: vpc,
vpcSubnets: { subnets: [appSubnet1aId, appSubnet1bId] },
securityGroups: [lambdaSecurityGroup],
environment: environmentVariables,
role: customRole,
layers: [layer],
}),
//isCompleteHandler: isComplete,
logRetention: logs.RetentionDays.ONE_DAY,
}
);