Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门。使用 Amazon 开发工具包创建用户的内联 IAM policy
以下代码示例演示如何为用户创建内联 IAM policy。
- Ruby
-
- SDK for Ruby
-
# Creates an inline policy for a user that lets the user assume a role.
#
# @param policy_name [String] The name to give the policy.
# @param user [Aws::IAM::User] The user that owns the policy.
# @param role [Aws::IAM::Role] The role that can be assumed.
# @return [Aws::IAM::UserPolicy] The newly created policy.
def create_user_policy(policy_name, user, role)
policy = user.create_policy(
policy_name: policy_name,
policy_document: {
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Action: "sts:AssumeRole",
Resource: role.arn
}]
}.to_json)
puts("Created an inline policy for #{user.name} that lets the user assume role #{role.name}.")
rescue Aws::Errors::ServiceError => e
puts("Couldn't create an inline policy for user #{user.name}. Here's why: ")
puts("\t#{e.code}: #{e.message}")
raise
else
policy
end
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。