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

标签 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 块内引用,但不能在自定义块内引用它

最佳答案

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 变量,但这仍然需要您首先建立您的用户池。

关于amazon-web-services - 带有 cloudformation 输出变量的 Serverless.yml 自定义堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636166/

相关文章:

amazon-web-services - 无服务器框架和 CloudFormation 之间的区别?

node.js - 在 GraphQL Playground 中设置我想要使用的测试 header 会导致 "Server cannot be reached"

api - 是否有用于 Amazon Web Services 定价的 API?

amazon-web-services - 如何在 AWS Lambda 中创建身份验证中间件

java - 从 Amazon EBS 读取文件

sql-server - 读取 ADLS 中存储的 CSV 时,Synapse 无服务器 SQL 池出现性能问题

c# - 无法在 Azure Functions 中加载文件或程序集 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

amazon-web-services - 如何在多个Lambda函数之间共享代码?

java - AWS SDK 在检查不存在的 S3 key 时抛出 403,但对现有 key 返回 true

java - 使用 distcp 或 s3distcp 将文件从 S3 复制到 HDFS