有没有办法从 cloudformation 模板中检索 cognito 用户池应用程序客户端的登录页面 url?
我目前有两个项目:一个用于 spa webapp 端,一个用于 aws stack 托管和服务。
就自动化而言,在资产编译之前,让 spa 项目管道通过 cli(例如:或类似方式)从 aws stack 检索任何必要的 url 是完美的sam describe-stack
,不是吗?
答案1
您可以使用 CloudFormation输出和进口将它们放入需要它们的模板中。
这CloudFormation Cognito 用户池文档具有返回值“ProviderURL”。您可以在创建用户池的 CloudFormation 模板底部执行类似操作。
Resources:
UserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: Example
(etc)
Outputs:
UserPoolURL:
Value: !GetAtt 'UserPool.ProviderURL'
Export:
Name: "UserPoolURL"
然后,您将在需要引用 URL 的 CloudFormation 模板中执行类似的事情。
Resources:
ResourceName:
Type: AWS::Whatever
Properties:
URL: !ImportValue UserPoolURL
由于我尚未测试过,因此代码可能不太正确,但多年来我已经做过很多次了,即使不是完美的,也应该是正确的。
答案2
注册和登录 URL 是根据 UserPoolClient 构建的。以下是一个最小的 SAM 模板,它创建 UserPool 和 UserPoolClient,并生成注册和登录 URL 作为输出。
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Parameters:
userPoolName:
Type: String
Description: The domain name for the Cognito User Pool
Default: my-unique-pool-name
callbackDomain:
Type: String
Description: URL that will be used for cognito callbacks
Default: my-app.example.com
Resources:
MyUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: !Ref userPoolName
Schema:
- Name: email
AttributeDataType: String
Mutable: true
Required: true
StringAttributeConstraints:
MinLength: '0'
MaxLength: '2048'
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
RequireUppercase: true
AutoVerifiedAttributes:
- email
MyUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: !Ref userPoolName
UserPoolId: !Ref MyUserPool
GenerateSecret: false
CallbackURLs:
- !Sub 'https://${callbackDomain}/'
LogoutURLs:
- !Sub 'https://${callbackDomain}/logout'
AllowedOAuthFlows:
- code
- implicit
AllowedOAuthScopes:
- email
- openid
- profile
AllowedOAuthFlowsUserPoolClient: true
SupportedIdentityProviders:
- COGNITO
MyUserPoolDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Ref userPoolDomain
UserPoolId: !Ref MyUserPool
Outputs:
SignUpUrl:
Description: Cognito Sign Up URL
Value: !Sub 'https://${userPoolDomain}.auth.${AWS::Region}.amazoncognito.com/signup?response_type=code&client_id=${MyUserPoolClient}&redirect_uri=https://${callbackDomain}/'
LoginUrl:
Description: Cognito Login URL
Value: !Sub 'https://${userPoolDomain}.auth.${AWS::Region}.amazoncognito.com/login?response_type=code&client_id=${MyUserPoolClient}&redirect_uri=https://${callbackDomain}/'