I want to create a serverless file that deploys Cognito resources to AWS. I have a config.yml file that holds all the scopes that should be created in the Cognito Resource Server.
config.yml
- name: scope1
description: Description of scope1
- name: scope2
description: Description of scope2
What I want to accomplish is to dynamically generate one Cognito App Client for each scope we register, as well as adding these scopes to a Cognito Resource Server (in my case, the Cognito User Pool and Domain Name are already created).
To do that, I tried to make a javascript file that will load the config.yml file and generate two variables:
userPoolClientListwhich holds a list of Cognito App Client resources.scopeListwhich represents a list of scopes that should be registered in the Cognito Resource Server.
sls-template.js
const fs = require("fs");
const yaml = require("js-yaml");
const scopeList = yaml.safeLoad(fs.readFileSync("config.yml"));
module.exports = {
scopeList: function () {
return scopeList.map(({ name, description }) => ({
ScopeName: name,
ScopeDescription: description,
}));
},
userPoolClientList: function (serverless) {
const { cognitoUserPoolId } = serverless.service.custom;
const scopeResourceList = scopeList.map(({ name, description }) => ({
[`cognitoUserPoolClient-${name}`]: {
Type: "AWS::Cognito::UserPoolClient",
Properties: {
AllowedOAuthScopes: [`server/${name}`],
UserPoolId: cognitoUserPoolId,
},
DependsOn: "cognitoResourceServer",
},
}));
return Object.assign({}, ...scopeResourceList);
},
};
Now this looks like it returns exactly what I wanted (I've tested it and it works great).
My problem is rather in the implementation on the serverless.yml file and how to combine a fixed Resources and a dynamically generated one.
serverless.yml
resources:
Resources:
${file(sls-template.js):userPoolClientList}
cognitoResourceServer:
Type: AWS::Cognito::UserPoolResourceServer
Properties:
Identifier: server
Name: Server
Scopes: ${file(sls-template.js):scopeList}
UserPoolId: ${self:custom.cognitoUserPoolId}
This throws an error as the syntax is not correct. However, when I try to deploy the resources individually (one time just the cognitoResourceServer resource, the other time the generated variable from the javascript file), everything works fine.
The problem really is on how I should combine or merge these two resources.
I've been trying a lot of different combinations to try to make it work, but it always give me an invalid template.
So I was wondering if what I try to accomplish is even possible in serverless and if so, how can I change my final serverless.yml file to make it work.
Thanks a lot.