AWS 无服务器 IdentityPoolRoleAttachment
AWS Serverless IdentityPoolRoleAttachment
所以,我正在为我的 Cognito 用户创建一个角色,以便能够调用 API 网关:
IdentityAuthenticatedRole:
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: CognitoIdentityPoolStandardUserIdentityPool
ForAnyValue:StringLike:
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: CognitoGatewayExecute
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "execute-api:Invoke"
Resource: "arn:aws:execute-api:*:*:*"
MaxSessionDuration: 3600
然后我将该角色附加到我的 IdentityPoolRoleAttachment:
CognitoIdentityPoolRoleAttachment:
DependsOn: CognitoIdentityPoolStandardUserIdentityPool
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Fn::Join:
- ''
- - Ref: CognitoIdentityPoolStandardUserIdentityPool
- ''
Roles:
authenticated:
Fn:GetAtt
- IdentityAuthenticatedRole
- Arn
根据文档,它应该可以工作,但当然不能:
CognitoIdentityPoolRoleAttachment - Access to Role 'Fn:GetAtt - IdentityAuthenticatedRole - Arn' is forbidden.
有人可以解释一下吗?
P.S。由于我已经粘贴了这个片段,还有一件事:我正在使用 Fn::Join,否则我会遇到 "Is not of type String" 错误,有没有更好的方法来处理它?
语法错误
您的 Fn::GetAtt
语法有点不对劲。 Fn
和 GetAtt
之间需要两个冒号,然后在该行的末尾需要一个冒号。像这样:
authenticated:
Fn::GetAtt:
这将修复包含 Fn:GetAtt
... 的奇怪错误消息,其中应该是真实的角色名称。
Fn::加入
您可以像这样使用 Ref
来摆脱 Fn::Join
调用:
Properties:
IdentityPoolId:
Ref: CognitoIdentityPoolStandardUserIdentityPool
取决于
DependsOn
行很好,但不需要。 CloudFormation 足够聪明,可以为您找出这种依赖关系。
YAML 注释
最后,虽然这归结为可读性偏好,但我通常将短列表(如您传递给 Fn::GetAtt
的列表)放在方括号中。所以你可以替换这个:
authenticated:
Fn::GetAtt:
- IdentityAuthenticatedRole
- Arn
有了这个:
authenticated:
Fn::GetAtt: [IdentityAuthenticatedRole, Arn]
重写
结果更短,而且可以说更容易阅读。结合这些建议会产生此角色附件资源:
CognitoIdentityPoolRoleAttachment:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Ref: CognitoIdentityPoolStandardUserIdentityPool
Roles:
authenticated:
Fn::GetAtt: [IdentityAuthenticatedRole, Arn]
使用无服务器 1.27.2
测试
所以,我正在为我的 Cognito 用户创建一个角色,以便能够调用 API 网关:
IdentityAuthenticatedRole:
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: CognitoIdentityPoolStandardUserIdentityPool
ForAnyValue:StringLike:
"cognito-identity.amazonaws.com:amr": authenticated
Policies:
- PolicyName: CognitoGatewayExecute
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "execute-api:Invoke"
Resource: "arn:aws:execute-api:*:*:*"
MaxSessionDuration: 3600
然后我将该角色附加到我的 IdentityPoolRoleAttachment:
CognitoIdentityPoolRoleAttachment:
DependsOn: CognitoIdentityPoolStandardUserIdentityPool
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Fn::Join:
- ''
- - Ref: CognitoIdentityPoolStandardUserIdentityPool
- ''
Roles:
authenticated:
Fn:GetAtt
- IdentityAuthenticatedRole
- Arn
根据文档,它应该可以工作,但当然不能:
CognitoIdentityPoolRoleAttachment - Access to Role 'Fn:GetAtt - IdentityAuthenticatedRole - Arn' is forbidden.
有人可以解释一下吗?
P.S。由于我已经粘贴了这个片段,还有一件事:我正在使用 Fn::Join,否则我会遇到 "Is not of type String" 错误,有没有更好的方法来处理它?
语法错误
您的 Fn::GetAtt
语法有点不对劲。 Fn
和 GetAtt
之间需要两个冒号,然后在该行的末尾需要一个冒号。像这样:
authenticated:
Fn::GetAtt:
这将修复包含 Fn:GetAtt
... 的奇怪错误消息,其中应该是真实的角色名称。
Fn::加入
您可以像这样使用 Ref
来摆脱 Fn::Join
调用:
Properties:
IdentityPoolId:
Ref: CognitoIdentityPoolStandardUserIdentityPool
取决于
DependsOn
行很好,但不需要。 CloudFormation 足够聪明,可以为您找出这种依赖关系。
YAML 注释
最后,虽然这归结为可读性偏好,但我通常将短列表(如您传递给 Fn::GetAtt
的列表)放在方括号中。所以你可以替换这个:
authenticated:
Fn::GetAtt:
- IdentityAuthenticatedRole
- Arn
有了这个:
authenticated:
Fn::GetAtt: [IdentityAuthenticatedRole, Arn]
重写
结果更短,而且可以说更容易阅读。结合这些建议会产生此角色附件资源:
CognitoIdentityPoolRoleAttachment:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId:
Ref: CognitoIdentityPoolStandardUserIdentityPool
Roles:
authenticated:
Fn::GetAtt: [IdentityAuthenticatedRole, Arn]
使用无服务器 1.27.2
测试