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

Muh*_*aib 5 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]
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*lex 2

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

不过,您可以引用其他 CloudFormation 堆栈的输出。

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

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

Resources:
  Outputs:
    MyUserPoolId:
      Value:
        Ref: CognitoUserPool # Key name of user pool resource
      Export:
        Name: MyUserPoolId
Run Code Online (Sandbox Code Playgroud)

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

custom:
  appSync:
    userPoolConfig:
      userPoolId:
        Fn::ImportValue: MyUserPoolId
Run Code Online (Sandbox Code Playgroud)

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

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