【问题标题】:serverless - How to dynamically add resources generated from a javascript file and merge them with other resources?serverless - 如何动态添加从 javascript 文件生成的资源并将它们与其他资源合并?
【发布时间】:2020-08-28 01:14:25
【问题描述】:

我想创建一个将 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 应用客户端资源列表。
  • 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);
  },
};

现在看起来它返回的正是我想要的(我已经对其进行了测试并且效果很好)。

我的问题在于serverless.yml 文件的实现以及如何将固定的Resources 与动态生成的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 文件以使其正常工作。

非常感谢。

【问题讨论】:

    标签: javascript amazon-web-services yaml amazon-cloudformation serverless-framework


    【解决方案1】:

    是的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 顶级密钥,或者您是否可以像我在示例中显示的那样对其进行硬编码。

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2014-06-21
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多