amazon-web-services - 使用 serverless.yml 编辑 AWS Cognito 身份池
问题描述
我正在使用无服务器框架配置 AWS Cognito 身份池,并且正在编辑 yml 配置中的文件以添加未经身份验证的角色,以便用户将图像上传到 s3 存储桶。
之前部署的代码没有指定未经身份验证的角色,并且部署顺利且稳定。在我寻找一种方法来控制访问 S3 存储桶的权限后,我发现在 S3 存储桶上授予写入而不是读取权限的唯一方法是在用户策略中指定它,所以我必须添加身份池的未经身份验证的角色。但是,当我部署代码时,我收到一条错误消息:
Serverless Error ---------------------------------------
An error occurred: CognitoIdentityPoolRoles - Resource cannot be updated.
我已经设法在开发环境中解决了这个问题,但它需要完全删除堆栈并从头开始重建它。
我也不想在 AWS 控制台中手动调整资源,因为资源应该在 cloudformation 或控制台中进行管理,但是两种方式都会导致混乱。
因此,目前,我看到的选项是删除整个堆栈并使用新角色重建它,或者找到通过 cloudformation 进行更新的方法。
有没有人有办法避免第一个选项并允许我更新堆栈而不在控制台中附加角色?
serverless.yml 的相关部分如下...
Resources:
# The federated identity for our user pool to auth with
CognitoIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
# Generate a name based on the stage
IdentityPoolName: ${self:custom.stage}MyIdentityPool
# Allow unathenticated users
AllowUnauthenticatedIdentities: true
# Link to our User Pool
CognitoIdentityProviders:
- ClientId:
Ref: CognitoUserPoolClient
ProviderName:
Fn::GetAtt: [ "CognitoUserPool", "ProviderName" ]
# IAM roles
CognitoIdentityPoolRoles:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Ref: CognitoIdentityPool
Roles:
authenticated:
Fn::GetAtt: [CognitoAuthRole, Arn]
# Next two lines are the 2 lines of code which break everything
unauthenticated:
Fn::GetAtt: [CognitoUnAuthRole, Arn]
# IAM role for UN-authenticated users
CognitoUnAuthRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Federated: 'cognito-identity.amazonaws.com'
Action:
- 'sts:AssumeRoleWithWebIdentity'
Condition:
StringEquals:
'cognito-identity.amazonaws.com:aud':
Ref: CognitoIdentityPool
'ForAnyValue:StringLike':
'cognito-identity.amazonaws.com:amr': unauthenticated
Policies:
- PolicyName: 'CognitoUnAuthorizedPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'mobileanalytics:PutEvents'
- 'cognito-sync:*'
- 'cognito-identity:*'
Resource: '*'
# Allow users to upload attachments to their
# folder inside our S3 bucket
- Effect: 'Allow'
Action:
- 's3:PutObject'
Resource:
- Fn::Join:
- ''
-
- Fn::GetAtt: [MediafilesBucket, Arn]
- '/submissions/'
解决方案
固定的。
我注释掉了 serverless.yml 中与已部署(已销毁)的身份池相关的部分,然后取消注释该部分,重新部署并从备份中恢复。
这似乎有点骇人听闻,但它奏效了。
我也觉得应该有一种方法可以通过 cloudformation 来编辑身份池角色......
推荐阅读
- python - 将 if/else 逻辑应用于函数中的数据框:ValueError:系列的真值不明确
- c++ - 使用 clang 或 g++/gcc 打印预处理代码而不包括系统路径中的文件
- javascript - 反应:出现 Material-UI 对话框时模糊背景
- java - 具有大负载的 gRPC 延迟和吞吐量
- android - Android Emulator 连接到 WSL2 中的节点服务器
- javascript - JS 日期 YYYY-MM-DD 返回不正确的值
- oracle - Oracle apex - 交互式网格 setSelectedRecords 不起作用
- r - 使用辅助轴绘图
- javascript - React-toastify 显示多个吐司
- java - 在溢出的情况下我们如何扩展数据包?