One of the things I like about DynamoDB is you can connect to it like so.
const params = {
TableName: process.env.TWILIO_TABLE,
Item: {
callId: obj.CallSid,
to: obj.To,
from: obj.From,
callerName: obj.CallerName,
callerCity: obj.CallerCity,
callStatus: obj.CallStatus,
direction: obj.Direction,
createdAt: new Date().getTime()
}
};
Note that all you need is the TableName and from your AWS credentials it knows what DynamoDB table you are referring.
I want to connect to RDS in much the same way.
I'm using the serverless framework to create the resource like below.
resources:
Resources:
uploadBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.uploadBucket}
RDSDatabase:
Type: AWS::RDS::DBInstance
Properties:
Engine : mysql
DBInstanceIdentifier: ${self:custom.databaseName}
MasterUsername: Test123
MasterUserPassword: TestPass123
DBInstanceClass : db.m1.small
AllocatedStorage: '5'
PubliclyAccessible: true
DeletionPolicy: Snapshot
Which in turn uses cloud formation to create the database instance for me. In particular I am dynamically creating the database name and its identifier DBInstanceIdentifier: ${self:custom.databaseName}
That name changes based on the stage parameters (dev, beta, alpha, prod, etc...) that I use when deploying via serverless.
The idea is that a developer can automatically have a database created for him for testing purposes based on the stage if needed. Say they created a custom stage like 'joe' for themselves to test with real resources before pushing to 'staging'.
The advantage of this is real testing and is easy to remove resources with sls remove --stage joe
for example which removes all associated resources with that stage.
The problem is to connect to RDS you need to have the username, password and host. These are located in the console after the resource is created but I need access to these somehow during my serverless script so I can set them to environment variables to easily connect to the correct databases for all my scripts.
So in short, the way DynamoDB works is perfect since I just reference the tableName and I don't have to worry about the hostname,username,password etc and it knows which tables are what. I need to do the same thing for RDS if possible or a workaround type solution.
Any ideas?