Joey Yi Zhao
Joey Yi Zhao

Reputation: 42490

How can I handle database schema migration when using lambda and aurora postgresql?

I am deploying application on lambda and using aurora postgresql as database. During development process the database schema changes quite frequently and I am looking for a way to migrate the schema. I know that flyway can do the job but it works fine for an application deployed on EC2 instance rather than lambda. What is the best way to do the job in lambda?

I can think of a workaround solution. My lambda is in typescript so it is running inside nodejs environment.

Upvotes: 1

Views: 1080

Answers (2)

Kordestan
Kordestan

Reputation: 100

I am using loopback4 to create my models and database schema. I have an AWS Custom resources that's calls the handler to migrate the schema to RDS. here is what you need to do:

  1. create a security group for RDS 1.1. Add inbound rule: allow from lambda SG on TCP 3306 1.2. Add Outbound rule: allow all protocols, all ports on all
  2. create a security group for the lambda 2.1. Add Outbound rule: allow all protocols, all ports on all

here is my code using 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,
  }
);

Upvotes: 2

Kordestan
Kordestan

Reputation: 100

I am using loopback4 with lambda and I have created a custom resource that will connect to the RDS and run the migrate script to update the schema.

Upvotes: 0

Related Questions