manelferreira / serverless-sequelize-migrations

A Serverless plugin to manage sequelize migrations
MIT License
21 stars 17 forks source link

database environment parameters in serverless.yml #3

Open wildthing81 opened 5 years ago

wildthing81 commented 5 years ago

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]

manelferreira commented 5 years ago

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.

wildthing81 commented 5 years ago

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 ?

GonzaloSaad commented 5 years ago

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

jcoelho93 commented 2 years ago

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.