因此,我正在为我的Cognito用户创建一个角色,以便能够调用API Gateway:
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.
有人能解释一下这件事吗?
附注:由于我已经粘贴了这段代码,还有一件事:我正在使用Fn::Join,因为否则我会收到"Is not of type String“错误,有没有更好的方法来处理它?
发布于 2018-06-10 02:44:34
语法错误
您的Fn::GetAtt
语法有点不对劲。在Fn
和GetAtt
之间需要两个冒号,然后在该行的末尾需要一个冒号。如下所示:
authenticated:
Fn::GetAtt:
这将修复包含Fn:GetAtt
的奇怪错误消息...真实的角色名称应该在哪里。
Fn::Join
只需使用Ref
即可消除Fn::Join
调用,如下所示:
Properties:
IdentityPoolId:
Ref: CognitoIdentityPoolStandardUserIdentityPool
DependsOn
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]
使用Serverless 1.27.2测试
https://stackoverflow.com/questions/50746902
复制相似问题