AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Organizations K for Python (Boto3)
次のコード例は、AWS SDK for Python (Boto3) with Organizations を使用してアクションを実行する方法を示しています。
「アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。
「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。
それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。
トピック
アクション
次のコード例は、Organizations ポリシーをターゲットにアタッチする方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、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
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいAttachPolicy。
-
次のコード例は、Organizations ポリシーを作成する方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 def create_policy(name, description, content, policy_type, orgs_client): """ Creates a policy. :param name: The name of the policy. :param description: The description of the policy. :param content: The policy content as a dict. This is converted to JSON before it is sent to AWS. The specific format depends on the policy type. :param policy_type: The type of the policy. :param orgs_client: The Boto3 Organizations client. :return: The newly created policy. """ try: response = orgs_client.create_policy( Name=name, Description=description, Content=json.dumps(content), Type=policy_type) policy = response['Policy'] logger.info("Created policy %s.", name) except ClientError: logger.exception("Couldn't create policy %s.", name) raise else: return policy
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいCreatePolicy。
-
次のコード例は、Organizations ポリシーを削除する方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 def delete_policy(policy_id, orgs_client): """ Deletes a policy. :param policy_id: The ID of the policy to delete. :param orgs_client: The Boto3 Organizations client. """ try: orgs_client.delete_policy(PolicyId=policy_id) logger.info("Deleted policy %s.", policy_id) except ClientError: logger.exception( "Couldn't delete policy %s.", policy_id) raise
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいDeletePolicy。
-
次のコード例は、Organizations ポリシーの記述方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 def describe_policy(policy_id, orgs_client): """ Describes a policy. :param policy_id: The ID of the policy to describe. :param orgs_client: The Boto3 Organizations client. :return: The description of the policy. """ try: response = orgs_client.describe_policy(PolicyId=policy_id) policy = response['Policy'] logger.info("Got policy %s.", policy_id) except ClientError: logger.exception("Couldn't get policy %s.", policy_id) raise else: return policy
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいDescribePolicy。
-
次のコード例は、ターゲットから別のバケットにOrganizations コピーする方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいDetachPolicy。
-
次のコード例は、Organizations ポリシーを一覧表示する方法を示しています。
- SDK for Python (Boto3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 def list_policies(policy_filter, orgs_client): """ Lists the policies for the account, limited to the specified filter. :param policy_filter: The kind of policies to return. :param orgs_client: The Boto3 Organizations client. :return: The list of policies found. """ try: response = orgs_client.list_policies(Filter=policy_filter) policies = response['Policies'] logger.info("Found %s %s policies.", len(policies), policy_filter) except ClientError: logger.exception("Couldn't get %s policies.", policy_filter) raise else: return policies
-
API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」のを参照してくださいListPolicies。
-