带有cloudformation输出变量的Serverless.yml自定义堆栈

时间:2018-06-01 04:36:49

标签: amazon-web-services serverless

我是无服务器的新手,请原谅我,如果这是非常基本的。我有一个问题,我正在创建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块中引用,但我不能在自定义块

中引用它

1 个答案:

答案 0 :(得分: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变量,但仍需要先建立用户池。