【问题标题】:Serverless.yml custom stack with cloudformation output variable带有 cloudformation 输出变量的 Serverless.yml 自定义堆栈
【发布时间】:2018-11-11 04:00:54
【问题描述】:

我是无服务器的新手,如果这是非常基本的,请原谅我。我在创建 AMAZON COGNITO POOL 时遇到问题,我想将此 userPoolId 用于我的自定义堆栈块以将其与 appsync 连接。下面是我的serverless.yml

 custom:
  accountId: 123xxxxxxxx
  appSync:
    apiId: 123xyzxxxxxxx # only required for update-appsync
    authenticationType: AMAZON_COGNITO_USER_POOLS
    userPoolConfig:
      awsRegion: ap-southeast-1
      defaultAction: ALLOW
      userPoolId: (here it only takes string but i want to reference)
  resources:
    Resources:
    # Cognito - User pool
    CognitoUserPool:
      Type: AWS::Cognito::UserPool
      Properties:
         UserPoolName: abc_xyz_pool
    # Cognito - Client
    CognitoUserPoolClient:
      Type: AWS::Cognito::UserPoolClient
      Properties:
        ClientName: abc_xyz_pool
        GenerateSecret: false
        UserPoolId:
          Ref: CognitoUserPool
    # Cognito - Identity
    CognitoIdentityPool:
      Type: AWS::Cognito::IdentityPool
      Properties:
        IdentityPoolName: sc_identity_pool
        AllowUnauthenticatedIdentities: false
        CognitoIdentityProviders:
          - ClientId:
              Ref: CognitoUserPoolClient
            ProviderName:
              Fn::GetAtt: [CognitoUserPool, ProviderName]

我可以在 Resources 块内引用,但我不能在自定义块内引用它

【问题讨论】:

    标签: amazon-web-services serverless


    【解决方案1】:

    serverless.yml 中的自定义块在创建资源之前进行评估,因此无法引用这些输出。即使在 CFN 中,引用它们的位置和方式也存在限制。

    但是,您可以引用其他 CloudFormation 堆栈的输出。

    您应该将无服务器项目分成两个项目,第一个建立用户池,第二个使用该基础架构。

    在您的第一个项目中,您拥有用户池资源,并导出 ID 以供将来在其他堆栈中使用,如下所示:

    Resources:
      Outputs:
        MyUserPoolId:
          Value:
            Ref: CognitoUserPool # Key name of user pool resource
          Export:
            Name: MyUserPoolId
    

    在需要池 ID 的第二个项目中,您将导入它:

    custom:
      appSync:
        userPoolConfig:
          userPoolId:
            Fn::ImportValue: MyUserPoolId
    

    需要为第二个项目部署您的第一个项目才能导入导出的值。

    您也可以使用 ENV 变量,但这仍然需要您先建立用户池。

    【讨论】:

    • 谢谢,我正在使用 ENV 变量。
    • 这是一个很棒的方法,我最不想拥有的是更多的环境变量......有趣的是,甚至不需要指定导入的变量来自哪个堆栈跨度>
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2018-07-31
    • 2020-09-17
    • 2020-10-31
    • 2017-05-16
    • 2021-02-07
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多