serverless-sequelize-migrations icon indicating copy to clipboard operation
serverless-sequelize-migrations copied to clipboard

database environment parameters in serverless.yml

Open wildthing81 opened this issue 6 years ago • 4 comments

My serverless.yml contains DB parameters like so environment: DB_NAME: ${self:custom.database.name} DB_USERNAME: ${self:custom.database.username} DB_PASSWORD: ${self:custom.database.password} DB_HOST: ${self:custom.database.host} DB_PORT: ${self:custom.database.port}

Will these parameters get resolved if I run 'sls migrations up' . I am currently getting a

Serverless: Setting up connections... Serverless: Looking for pending migrations... Serverless: Error trying to apply migrations: SequelizeConnectionError: connect ENOENT [object Object]

wildthing81 avatar Jul 17 '19 08:07 wildthing81

Hi Ramaswamy.

From what you described, I guess that the DB_DIALECT parameter is missing. You have to set the DB_DIALECT value as one of the options presented below, according to the database you are working with: DB_DIALECT: 'mysql' OR 'mariadb' OR 'postgres' OR 'mssql'

If the issue persists, let me know so that I can help you.

manelferreira avatar Jul 18 '19 22:07 manelferreira

I have added the DB_DIALECT as well. still the same problem. The issue here is how to resolve DB parameters when they are dynamically generated from an RDS resource section within the same serverless yml file & stored as properties in 'custom' section of the yml ?

wildthing81 avatar Jul 23 '19 03:07 wildthing81

Just encountered the same issue.

I have the following:

Resources:
  RDS:
    Type: "AWS::RDS::DBInstance"
    ...

Then, to get the endpoint and the port:

  dbEndpoint:
    "Fn::GetAtt":
      - RDS
      - Endpoint.Address

  dbPort:
    "Fn::GetAtt":
      - RDS
      - Endpoint.Port

On the environment:

  environment:
    STAGE: ${self:provider.stage}
    DB_DIALECT: 'postgres'
    DB_PROTOCOL: ${self:custom.dbProtocol}
    DB_USERNAME: ${self:custom.dbUsername}
    DB_PASSWORD: ${self:custom.dbPassword}
    DB_HOST: ${self:custom.dbEndpoint}
    DB_PORT: ${self:custom.dbPort}
    DB_NAME: ${self:custom.dbName}

Then I get:

RangeError: "port" option should be >= 0 and < 65536: NaN

Alright. Now if I hardcode the port, that error disappears, but this comes up:

SequelizeHostNotFoundError: getaddrinfo ENOTFOUND [object Object] [object Object]:5432

GonzaloSaad avatar Sep 06 '19 02:09 GonzaloSaad

I'm also getting this issue. I have an RDS resource declared:

...
resources:
    Resources:
        RDSInstance:
              Type: AWS::RDS::DBInstance
              Properties:
...

Then I set my environment variables like this:

custom:
    databaseHost: !GetAtt RDSInstance.Endpoint.Address
...
provider:
    environment:
        DB_DIALECT: postgres
        DB_HOST: ${self:custom.databaseHost}
...

But when I run sls migrations up it always tries to connect to 127.0.0.1. But the environment variable is set correctly, I can see in the Lambda console that the DB_HOST has the correct value.

jcoelho93 avatar Mar 02 '22 10:03 jcoelho93