Discovering my RDS connection details

Hey there,

I’m fairly new to serverless. I have python functions deployed, and that’s all working well. Now I’m trying to setup and use an RDS resource. Here’s where I’m getting stuck. I’ve successfully created an RDS instance, but I’m at a loss for how I’m supposed to auto-discover the DATABASE_URL to connect to it. I presume there’s a special ${…} syntax I can use in the environment section .

Any help much appreciated!

My configuration looks like this:

resources:
  Resources:
    MarketDB: 
      Type: AWS::RDS::DBInstance
      Properties: 
        DBName: "MarketDB"
        AllocatedStorage: "5"
        DBInstanceClass: "db.t2.small"
        Engine: "MySQL"
        EngineVersion: "5.7.22"
        MasterUsername: "xxx"
        MasterUserPassword: "xxx"
      DeletionPolicy: "Snapshot"

Cheers,
Hitechbunny

1 Like

Value that you’re looking for is exposed in resource output as Endpoint.Address (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html section Return Values).

Then, you can pass it to your Lambda function using Fn:GetAtt like this:

functions:
  helloFunction:
    environment:
      DATABASE_URL: 
        Fn::GetAtt:
          - MarketDB
          - Endpoint.Address
2 Likes

Awesome! Thank you so much. :slight_smile: