Úselo AttachPolicy con un AWS SDK o CLI - AWS Ejemplos de código de SDK

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Úselo AttachPolicy con un AWS SDK o CLI

Los siguientes ejemplos de código muestran cómo utilizar AttachPolicy.

.NET
SDK para .NET
nota

Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Shows how to attach an AWS Organizations policy to an organization, /// an organizational unit, or an account. /// </summary> public class AttachPolicy { /// <summary> /// Initializes the Organizations client object and then calls the /// AttachPolicyAsync method to attach the policy to the root /// organization. /// </summary> public static async Task Main() { IAmazonOrganizations client = new AmazonOrganizationsClient(); var policyId = "p-00000000"; var targetId = "r-0000"; var request = new AttachPolicyRequest { PolicyId = policyId, TargetId = targetId, }; var response = await client.AttachPolicyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully attached Policy ID {policyId} to Target ID: {targetId}."); } else { Console.WriteLine("Was not successful in attaching the policy."); } } }
  • Para obtener más información sobre la API, consulta AttachPolicyla Referencia AWS SDK para .NET de la API.

CLI
AWS CLI

Asociación de una política a un nodo raíz, unidad organizativa o cuenta

Ejemplo 1

El siguiente ejemplo de código muestra cómo adjuntar una política de control de servicio (SCP) a una unidad organizativa.

aws organizations attach-policy --policy-id p-examplepolicyid111 --target-id ou-examplerootid111-exampleouid111

Ejemplo 2

El siguiente ejemplo de código muestra cómo adjuntar una política de control de servicio directamente a una cuenta:

aws organizations attach-policy --policy-id p-examplepolicyid111 --target-id 333333333333
  • Para obtener más información sobre la API, consulta AttachPolicyla Referencia de AWS CLI comandos.

Python
SDK para Python (Boto3)
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

def attach_policy(policy_id, target_id, orgs_client): """ Attaches a policy to a target. The target is an organization root, account, or organizational unit. :param policy_id: The ID of the policy to attach. :param target_id: The ID of the resources to attach the policy to. :param orgs_client: The Boto3 Organizations client. """ try: orgs_client.attach_policy(PolicyId=policy_id, TargetId=target_id) logger.info("Attached policy %s to target %s.", policy_id, target_id) except ClientError: logger.exception( "Couldn't attach policy %s to target %s.", policy_id, target_id ) raise
  • Para obtener más información sobre la API, consulta AttachPolicyla AWS Referencia de API de SDK for Python (Boto3).

SAP ABAP
SDK para SAP ABAP
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

TRY. lo_org->attachpolicy( iv_policyid = iv_policy_id iv_targetid = iv_target_id ). MESSAGE 'Policy attached to target.' TYPE 'I'. CATCH /aws1/cx_orgaccessdeniedex. MESSAGE 'You do not have permission to attach the policy.' TYPE 'E'. CATCH /aws1/cx_orgpolicynotfoundex. MESSAGE 'The specified policy does not exist.' TYPE 'E'. CATCH /aws1/cx_orgtargetnotfoundex. MESSAGE 'The specified target does not exist.' TYPE 'E'. CATCH /aws1/cx_orgduplicateplyatta00. MESSAGE 'The policy is already attached to the target.' TYPE 'E'. ENDTRY.
  • Para obtener más información sobre la API, consulte AttachPolicyla referencia sobre la API ABAP del AWS SDK para SAP.