User guide
statements.Add(statement);
var policy = new Policy
{
Id = "ListDeploymentConfigsPolicy",
Version = "2012-10-17",
Statements = statements
};
return policy.ToJson();
}
Add a Policy to a Role
The following example creates a new policy and then adds the new policy to an existing role.
The following example doesn't use the AWS Resource APIs for .NET, as the resource APIs currently
don't support adding a policy to a role. However, this example is presented for completeness:
// using Amazon.IdentityManagement;
// using Amazon.IdentityManagement.Model;
var client = new AmazonIdentityManagementServiceClient();
// GenerateRolePolicyDocument() is a custom method.
string policyDoc = GenerateRolePolicyDocument();
var request = new PutRolePolicyRequest
{
RoleName = "DemoEC2",
PolicyName = "DemoEC2Permissions",
PolicyDocument = policyDoc
};
try
{
client.PutRolePolicy(request);
}
catch (NoSuchEntityException)
{
Console.WriteLine
("Role 'DemoEC2' or policy 'DemoEC2Permissions' does not exist.");
}
The preceding example relies on the following example to create the new policy.
The following example doesn't use the AWS Resource APIs for .NET, as the resource APIs currently
don't support creating a policy document. However, this example is presented for completeness:
public static string GenerateRolePolicyDocument()
{
// using Amazon.Auth.AccessControlPolicy;
// Create a policy that looks like this:
/*
{
Version v2.0.0
104
AWS SDK for .NET Developer Guide
IAM Resource API Examples