fauna-labs / serverless-fauna

MIT No Attribution
18 stars 3 forks source link

Plugin does not create resources by dependency graph #7

Closed rts-rob closed 3 years ago

rts-rob commented 3 years ago

The plugin throws an error when an "earlier" resource depends on a "later" resource. Ordering is:

This means a user cannot create a UDF and a custom role in a single deployment.

Steps to recreate

  1. Create a new serverless project with the following serverless.yml. Assume all setup is complete (_FAUNASECRET, etc.)
    
    service: dependency-issue
    frameworkVersion: '2'
    useDotenv: true

provider: name: aws runtime: go1.x lambdaHashingVersion: 20201221

plugins:

fauna: client: secret: ${env:FAUNA_SECRET}

functions: always-true: name: always_true body: Lambda([], true) role: ${self:fauna.roles.true-function-role.name}

roles: true-function-role: name: true_function privileges: []

2. Run `sls fauna deploy`

### Expected result:
Plugin creates one role _true_function_ and one UDF _always_true_ with the new role assigned.

### Observed result:
Plugin fails to create the resources, giving the following error:

Serverless: Running "serverless" installed locally (in service node_modules) Fauna: Schema updating in process... Fauna: Error: upsert.function.always_true => let,2,res,else,role: Cannot read reference.



## Workaround
1. Comment out the function, run `sls fauna deploy`, uncomment the function, run `sls fauna deploy` again.

This is insufficient for real-world applications. Expecting developers to unwind their dependency graph and deploy resources in order defeats the purpose of an IaC tool.

## Other variations tried
Also tried cutting and pasting _roles_ to be before _functions_, but I receive the same error. This may indicate that it's not a strict ordering based on order of appearance in the template. 

## Possible root cause
The plugin appears to create resources in a specific order by type ([collections -> indexes -> functions -> roles](https://github.com/fauna-labs/serverless-fauna/blob/978196aeacf6463f97f611232c9367395c0d76f6/fauna/DeployQueries.js#L15)) rather than via a DAG.

Simply reordering the `prepareQueries` statement won't address the problem. Some _functions_ depend on _roles_; some _roles_ depend on _functions_. It's reasonable to expect both cases to be present in the same template, although we'll still need to watch out for circular dependencies.
github-actions[bot] commented 3 years ago

Internal ticket number is LABS-19

fireridlle commented 3 years ago

@rts-rob this is because functions created before roles https://github.com/fauna-labs/serverless-fauna/blob/main/fauna/DeployQueries.js#L18. Another case is that role might have privileges to call function. and in this case, a function must be created first.

rts-rob commented 3 years ago

Right - that is the cause and I linked it above. However, that isn't a great experience for the developer.

Expected behavior would be to define a valid configuration and deploy it in a single sls fauna deploy command. One way to do this is to build a DAG of dependencies and create each resource one by one, rather than by category.