serverless / template

Compose & provision a collection of Serverless Components
https://serverless.com
Apache License 2.0
10 stars 6 forks source link

Support for nested state reference #13

Open serverlessguru opened 4 years ago

serverlessguru commented 4 years ago

Currently it appears to not be possible to reference a nested state object inside the serverless.yml file.

serverless.yml:

cognitoUserPool:
  component: '@serverless/aws-cognito'
  inputs:
    region: us-east-1
    authTemplate: COGNITO_USER_POOL

myAppSyncApi:
  component: "@serverless/aws-app-sync"
  inputs:
    name: appApi

    # Amazon Cognito Authentication
    authenticationType: AMAZON_COGNITO_USER_POOLS
    userPoolConfig:
      awsRegion: us-east-1
      defaultAction: ALLOW
      userPoolId: ${cognitoUserPool.app.poolId}

state file (Template.cognitoUserPool.json):

{
  "app": {
    "poolId": "us-east-1sxxxxxxx",
    "poolArn": "xxxxxxxxxxxxx",
    "clientId": "xxxxxxxxxxxxxxx"
  },
  "region": "us-east-1"
}

Error:

error:
  Error: invalid reference ${cognitoUserPool.app.poolId}
    at Object.<anonymous> (/usr/local/lib/node_modules/serverless/node_modules/@serverless/template/utils.js:59:17)
    at walker (/usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:190:22)
    at /usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:208:29
    at Array.forEach (<anonymous>)
    at forEach (/usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:298:31)
    at walker (/usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:203:13)
    at /usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:208:29
    at Array.forEach (<anonymous>)
    at forEach (/usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:298:31)
    at walker (/usr/local/lib/node_modules/serverless/node_modules/traverse/index.js:203:13)

  4s › usersLambda › Error: invalid reference ${cognitoUserPool.app.poolId}

Why would you want to do this? When creating Serverless Components, you need to save state (e.g. this.save()) and it's much easier to logically group things together (by a key) than to have it all be flat inside the Template.cognitoUserPool.json object.

medikoo commented 4 years ago

Variable revolvers need to start with service token (as e.g. self:). In you're case I think you need to refer to file:service. See https://serverless.com/framework/docs/providers/aws/guide/variables/ for more info

medikoo commented 4 years ago

Sorry, ignore my comment, I was referring to SLS v1