typescript icon indicating copy to clipboard operation
typescript copied to clipboard

Support referencing resources from yaml files

Open coyoteecd opened this issue 4 years ago • 4 comments

Use case

Our existing serverless.yml files reference (partial) CloudFormation templates defined separately, as follows:

service: example
provider:
  name: aws
[...]
resources:
  - Description: CloudFormation stack description
  - ${file(./cf-resources/resource1.yml)}
  - ${file(./cf-resources/resource2.yml)}

Each resource yml file is a "valid" chunk of CloudFormation template, as follows:

Resources:
  Resource1:
    Type: AWS:xxx:yyy
    Properties:
      [etc. etc]

The advantages of the above setup (for us) is we can then configure cfn-lint tool to run against the entire /resources folder and validate the template chunks. With the help of VSCode plugins, we also get Intellisense that is context dependent on the raw resource type + links to relevant AWS docs, both which help a lot.

Problem

I tried to convert the serverless.yml template to TypeScript and I ran into issues with the file references to the raw resource templates. I do not want to convert the resource definitions to TypeScript, because I would lose the cfn-lint support.

The following works (but does not compile with the current @serverless/typescript definitions):

import type { AWS } from '@serverless/typescript';

const serverlessConfig: AWS = {
  service: 'example',
  provider: {
    name: 'aws'
  },
  resources: [
    {
      Description: CloudFormation stack description
    },
   '${file(./cf-resources/resource1.yml)}',
   '${file(./cf-resources/resource2.yml)}'
  ]
}

module.exports = serverlessConfig;

So to summarize, the type of resources should be changed to a union type that acccepts T | T[], where T = (the current type of resources entry + string type).

Obviously, if there is a better way that I did not see, I'm open to suggestions.

coyoteecd avatar Jul 06 '21 16:07 coyoteecd

You can wrtie ./cf-resources/resource1.yml on TS and then import. In that case just use TS

pdecarcer avatar Jul 20 '21 22:07 pdecarcer

@pdecarcer can you elaborate a bit?

coyoteecd avatar Jul 21 '21 08:07 coyoteecd

@fredericbarthelet what's your opinion, would you accept a PR that changes the resource type as suggested above?

coyoteecd avatar Oct 14 '21 06:10 coyoteecd

Thanks for raising this issue @coyoteecd. The problem you're pointing out actually extend to all definition file properties where serverless variables can be used. This issue was raised in https://github.com/serverless/typescript/issues/11 Please bear in mind that types exported from this repository are auto-generated, you cannot modify them manually. The only persistent way to introduce this kind of modification is using a custom compiler pass in the generation library. PR are more than welcome on https://github.com/serverless/typescript/issues/11 and https://github.com/serverless/typescript/issues/1 which is the first blocker.

fredericbarthelet avatar Oct 14 '21 07:10 fredericbarthelet