AWS 新手。我正在尝试使用 putUserPolicy API 向 IAM 用户添加内联策略,如下所示。运行到 Malformedpolicydocument 作为错误代码和策略中的语法错误作为错误消息
// Attaches the policy document to the IAM user userName in the account
async putUserPolicy(userName: string, roleArn: string) {
let userPolicyData: any = null;
try {
// creates the policy document
const policyDocumentForUser = this.createUserPolicyDocument(roleArn);
const trustPolicyParamsForUser = {
PolicyDocument: JSON.stringify(policyDocumentForUser),
PolicyName: 'userPolciy',
UserName: userName
};
// attaching the policy document to the IAM user
userPolicyData = await this.iam.putUserPolicy(trustPolicyParamsForUser).promise();
this.logger.info(`Successfully created user policy for '${userName}'`);
} catch (error) {
this.logger.error(`Unable to create user policy role`, error);
throw error;
}
}
private createUserPolicyDocument(roleArn: string) {
const policyDocument = {
'statement': [
{
'Action': 'sts:AssumeRole',
'Resource': roleArn,
'Effect': 'Allow'
}
]
};
this.logger.debug('policyDocument:', policyDocument);
return policyDocument;
}
也尝试将版本提供给策略,但观察到相同的错误。我一直在为我的代码库中的所有策略文档使用单引号。
添加参考文档:https ://docs.aws.amazon.com/IAM/latest/APIReference/API_PutUserPolicy.html