首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参考serverless.yml中由sst (无服务器堆栈)创建的用户池

参考serverless.yml中由sst (无服务器堆栈)创建的用户池
EN

Stack Overflow用户
提问于 2021-07-07 01:32:32
回答 1查看 110关注 0票数 0

我有一个用serverless构建的相当大的应用程序,现在我们正在试用serverless-stack。我正尝试在serverless.yml函数中引用由sst创建的用户池。有可能吗?下面是我尝试过的步骤:

我已经创建了一个用户池

代码语言:javascript
复制
import * as cdk from '@aws-cdk/core'
import * as cognito from '@aws-cdk/aws-cognito'
import * as sst from '@serverless-stack/resources'

export default class UserServiceStack extends sst.Stack {
  constructor(scope: cdk.Construct, id: string, props: sst.StackProps = {}) {
    super(scope, id, props)

    const userPool = new cognito.UserPool(this, 'userPool', {
      signInAliases: {
        email: true,
        phone: true,
      },
      autoVerify: {
        email: true,
        phone: true,
      },
      passwordPolicy: {
        minLength: 8,
        requireDigits: false,
        requireLowercase: false,
        requireSymbols: false,
        requireUppercase: false,
      },
      signInCaseSensitive: false,
      selfSignUpEnabled: true,
    })

    new cdk.CfnOutput(this, 'UserPoolId', {
      value: userPool.userPoolId,
    })

    const userPoolClient = new cognito.UserPoolClient(this, 'userPoolClient', {
      userPool,
      authFlows: {
        adminUserPassword: true,
        userPassword: true,
      },
    })
    
    new cdk.CfnOutput(this, 'UserPoolClientId', {
      value: userPoolClient.userPoolClientId,
    })
  }
}

并且想要更新在serverless.yml中定义的post确认触发器

代码语言:javascript
复制
  ...
  createUser:
    handler: createUser.default
    events:
      - cognitoUserPool:
          pool: !ImportValue '${self:custom.sstApp}...' # what to put here?
          trigger: PostConfirmation
          existing: true
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-07 03:13:40

我想通了。首先,如何在serverless.yml中使用cdk输出变量。

将它们导出到文件中

代码语言:javascript
复制
AWS_PROFILE=<profile-name> npx sst deploy --outputs-file ./exports.json

serverless.yml中,你可以像这样引用它

代码语言:javascript
复制
  ...
  createUser:
    handler: createUser.default
    events:
      - cognitoUserPool:
          pool: ${file(../../infrastructure/exports.json):${self:custom.sstApp}-UserServiceStack.userPoolName}
          trigger: PostConfirmation
          existing: true

第二。serverless是这样设置的,你必须传递userPoolName,而不是userPoolId。所以我必须生成用户池名称并将其输出

代码语言:javascript
复制
import * as uuid from 'uuid'

...

    const userPoolName = uuid.v4()
    const userPool = new cognito.UserPool(this, 'userPool', {
      userPoolName,
      ...
    })
    ...
    // eslint-disable-next-line no-new
    new cdk.CfnOutput(this, 'userPoolName', {
      value: userPoolName,
    })

第三,为了避免在调用lambda作为触发器时出现AccessDeniedException,您需要在资源中添加以下内容

代码语言:javascript
复制
  - Resources:
      OnCognitoSignupPermission:
        Type: 'AWS::Lambda::Permission'
        Properties:
          Action: "lambda:InvokeFunction"
          FunctionName:
            Fn::GetAtt: [ "CreateUserLambdaFunction", "Arn"] # the name must be uppercased name of your lambda + LambdaFunction at the end
          Principal: "cognito-idp.amazonaws.com"
          SourceArn: ${file(../../infrastructure/exports.json):${self:custom.sstApp}-UserServiceStack.userPoolArn}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68275083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档