无服务器 - 如何动态添加从 javascript 文件生成的资源并将它们与其他资源合并?
serverless - How to dynamically add resources generated from a javascript file and merge them with other resources?
我想创建一个将 Cognito 资源部署到 AWS 的无服务器文件。我有一个 config.yml
文件,其中包含应在 Cognito 资源服务器中创建的所有范围。
config.yml
- name: scope1
description: Description of scope1
- name: scope2
description: Description of scope2
我想要完成的是为我们注册的每个范围动态生成一个 Cognito 应用程序客户端,并将这些范围添加到 Cognito 资源服务器(在我的例子中,已经创建了 Cognito 用户池和域名).
为此,我尝试创建一个 javascript 文件来加载 config.yml
文件并生成两个变量:
userPoolClientList
包含 Cognito App Client 资源列表。
scopeList
表示应在 Cognito 资源服务器中注册的范围列表。
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);
},
};
现在这看起来 returns 正是我想要的(我已经测试过了,效果很好)。
我的问题在于 serverless.yml
文件的实现以及如何组合固定的 Resources
和动态生成的文件。
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}
这会引发错误,因为语法不正确。但是,当我尝试单独部署资源时(一次只是 cognitoResourceServer
资源,另一次是 javascript 文件生成的变量),一切正常。
真正的问题在于我应该如何组合或合并这两个资源。
我一直在尝试很多不同的组合来让它工作,但它总是给我一个无效的模板。
所以我想知道我尝试完成的事情是否可以在无服务器中实现,如果可以,我该如何更改我的最终 serverless.yml
文件以使其工作。
非常感谢。
是resource blocks can be merged。
我认为应该这样做:
resources:
- Resources: ${file(sls-template.js):userPoolClientList}
- Resources:
cognitoResourceServer:
Type: AWS::Cognito::UserPoolResourceServer
Properties:
Identifier: server
Name: Server
Scopes: ${file(sls-template.js):scopeList}
UserPoolId: ${self:custom.cognitoUserPoolId}
不能 100% 确定动态块是否需要生成 Resources
顶级密钥,或者您是否可以像我在示例中显示的那样对其进行硬编码。
我想创建一个将 Cognito 资源部署到 AWS 的无服务器文件。我有一个 config.yml
文件,其中包含应在 Cognito 资源服务器中创建的所有范围。
config.yml
- name: scope1
description: Description of scope1
- name: scope2
description: Description of scope2
我想要完成的是为我们注册的每个范围动态生成一个 Cognito 应用程序客户端,并将这些范围添加到 Cognito 资源服务器(在我的例子中,已经创建了 Cognito 用户池和域名).
为此,我尝试创建一个 javascript 文件来加载 config.yml
文件并生成两个变量:
userPoolClientList
包含 Cognito App Client 资源列表。scopeList
表示应在 Cognito 资源服务器中注册的范围列表。
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);
},
};
现在这看起来 returns 正是我想要的(我已经测试过了,效果很好)。
我的问题在于 serverless.yml
文件的实现以及如何组合固定的 Resources
和动态生成的文件。
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}
这会引发错误,因为语法不正确。但是,当我尝试单独部署资源时(一次只是 cognitoResourceServer
资源,另一次是 javascript 文件生成的变量),一切正常。
真正的问题在于我应该如何组合或合并这两个资源。
我一直在尝试很多不同的组合来让它工作,但它总是给我一个无效的模板。
所以我想知道我尝试完成的事情是否可以在无服务器中实现,如果可以,我该如何更改我的最终 serverless.yml
文件以使其工作。
非常感谢。
是resource blocks can be merged。
我认为应该这样做:
resources:
- Resources: ${file(sls-template.js):userPoolClientList}
- Resources:
cognitoResourceServer:
Type: AWS::Cognito::UserPoolResourceServer
Properties:
Identifier: server
Name: Server
Scopes: ${file(sls-template.js):scopeList}
UserPoolId: ${self:custom.cognitoUserPoolId}
不能 100% 确定动态块是否需要生成 Resources
顶级密钥,或者您是否可以像我在示例中显示的那样对其进行硬编码。