Usar DetachPolicy com o AWS SDK ou a CLI - Exemplos de código do AWS SDK

Há mais exemplos do AWS SDK disponíveis no repositório do GitHub Documento de Exemplos do AWS SDK.

Usar DetachPolicy com o AWS SDK ou a CLI

Os exemplos de código a seguir mostram como usar o DetachPolicy.

.NET
SDK para .NET
nota

Há mais no GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Shows how to detach a policy from an AWS Organizations organization, /// organizational unit, or account. /// </summary> public class DetachPolicy { /// <summary> /// Initializes the Organizations client object and uses it to call /// DetachPolicyAsync to detach the policy. /// </summary> public static async Task Main() { // Create the client object using the default account. IAmazonOrganizations client = new AmazonOrganizationsClient(); var policyId = "p-00000000"; var targetId = "r-0000"; var request = new DetachPolicyRequest { PolicyId = policyId, TargetId = targetId, }; var response = await client.DetachPolicyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully detached policy with Policy Id: {policyId}."); } else { Console.WriteLine("Could not detach the policy."); } } }
  • Consulte detalhes da API em DetachPolicy na Referência da API AWS SDK para .NET.

CLI
AWS CLI

Como separar uma política de uma raiz, UO ou conta

O seguinte exemplo mostra como separar uma política de uma UO:

aws organizations detach-policy --target-id ou-examplerootid111-exampleouid111 --policy-id p-examplepolicyid111
  • Para obter detalhes da API, consulte.DetachPolicy na Referência de comandos da AWS CLI.

Python
SDK para Python (Boto3).
nota

Há mais no GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

def detach_policy(policy_id, target_id, orgs_client): """ Detaches a policy from a target. :param policy_id: The ID of the policy to detach. :param target_id: The ID of the resource where the policy is currently attached. :param orgs_client: The Boto3 Organizations client. """ try: orgs_client.detach_policy(PolicyId=policy_id, TargetId=target_id) logger.info("Detached policy %s from target %s.", policy_id, target_id) except ClientError: logger.exception( "Couldn't detach policy %s from target %s.", policy_id, target_id ) raise
  • Consulte detalhes da API em DetachPolicy na Referência da API AWS SDK para Python (Boto3).