Verwendung von CreatePolicy mit einem AWS-SDK oder CLI - AWS-SDK-Codebeispiele

Weitere AWS-SDK-Beispiele sind im GitHub-Repository Beispiele für AWS Doc SDKs verfügbar.

Verwendung von CreatePolicy mit einem AWS-SDK oder CLI

Die folgenden Code-Beispiele zeigen, wie CreatePolicy verwendet wird.

.NET
SDK für .NET
Anmerkung

Auf GitHub finden Sie noch mehr. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel- einrichten und ausführen.

using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Creates a new AWS Organizations Policy. /// </summary> public class CreatePolicy { /// <summary> /// Initializes the AWS Organizations client object, uses it to /// create a new Organizations Policy, and then displays information /// about the newly created Policy. /// </summary> public static async Task Main() { IAmazonOrganizations client = new AmazonOrganizationsClient(); var policyContent = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\" : [{" + " \"Action\" : [\"s3:*\"]," + " \"Effect\" : \"Allow\"," + " \"Resource\" : \"*\"" + "}]" + "}"; try { var response = await client.CreatePolicyAsync(new CreatePolicyRequest { Content = policyContent, Description = "Enables admins of attached accounts to delegate all Amazon S3 permissions", Name = "AllowAllS3Actions", Type = "SERVICE_CONTROL_POLICY", }); Policy policy = response.Policy; Console.WriteLine($"{policy.PolicySummary.Name} has the following content: {policy.Content}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
  • Weitere API-Informationen finden Sie unter CreatePolicy in der AWS SDK für .NET-API-Referenz.

CLI
AWS CLI

Beispiel 1: So erstellen Sie eine Richtlinie mit einer Textquelldatei für die JSON-Richtlinie

Das folgende Beispiel zeigt, wie Sie eine Service-Kontrollrichtlinie (SCP) namens AllowAllS3Actions erstellen: Der Richtlinieninhalt stammt aus einer Datei auf dem lokalen Computer namens policy.json.

aws organizations create-policy --content file://policy.json --name AllowAllS3Actions, --type SERVICE_CONTROL_POLICY --description "Allows delegation of all S3 actions"

Die Ausgabe umfasst ein Richtlinienobjekt mit Details zur neuen Richtlinie:

{ "Policy": { "Content": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:*\"],\"Resource\":[\"*\"]}]}", "PolicySummary": { "Arn": "arn:aws:organizations::o-exampleorgid:policy/service_control_policy/p-examplepolicyid111", "Description": "Allows delegation of all S3 actions", "Name": "AllowAllS3Actions", "Type":"SERVICE_CONTROL_POLICY" } } }

Beispiel 2: So erstellen Sie eine Richtlinie mit einer JSON-Richtlinie als Parameter

Das folgende Beispiel zeigt, wie Sie dasselbe SCP erstellen, diesmal indem Sie den Richtlinieninhalt als JSON-Zeichenfolge in den Parameter einbetten. Die Zeichenfolge muss mit Backslashes vor den doppelten Anführungszeichen maskiert werden, um sicherzustellen, dass sie im Parameter, der selbst von doppelten Anführungszeichen umgeben ist, als Literale behandelt werden:

aws organizations create-policy --content "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:*\"],\"Resource\":[\"*\"]}]}" --name AllowAllS3Actions --type SERVICE_CONTROL_POLICY --description "Allows delegation of all S3 actions"

Weitere Informationen zum Erstellen und Verwenden von Richtlinien in Ihrer Organisation finden Sie unter „Verwalten von Organisationsrichtlinien“ im Benutzerhandbuch zu AWS Organizations.

  • Weitere API-Informationen finden Sie unter CreatePolicy in der AWS CLI-Befehlsreferenz.

Python
SDK für Python (Boto3)
Anmerkung

Auf GitHub finden Sie noch mehr. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS-Code-Beispiel- einrichten und ausführen.

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
  • Weitere API-Informationen finden Sie unter CreatePolicy in der API-Referenz zum AWS-SDK für Python (Boto3).