Get the IAM account password policy using an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Get the IAM account password policy using an AWS SDK

The following code examples show how to get the IAM account password policy.

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); try { var request = new GetAccountPasswordPolicyRequest(); var response = await client.GetAccountPasswordPolicyAsync(request); Console.WriteLine($"{response.PasswordPolicy}"); } catch (NoSuchEntityException ex) { Console.WriteLine($"Error: {ex.Message}"); }
Go
SDK for Go V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// AccountWrapper encapsulates AWS Identity and Access Management (IAM) account actions // used in the examples. // It contains an IAM service client that is used to perform account actions. type AccountWrapper struct { IamClient *iam.Client } // GetAccountPasswordPolicy gets the account password policy for the current account. // If no policy has been set, a NoSuchEntityException is error is returned. func (wrapper AccountWrapper) GetAccountPasswordPolicy() (*types.PasswordPolicy, error) { var pwPolicy *types.PasswordPolicy result, err := wrapper.IamClient.GetAccountPasswordPolicy(context.TODO(), &iam.GetAccountPasswordPolicyInput{}) if err != nil { log.Printf("Couldn't get account password policy. Here's why: %v\n", err) } else { pwPolicy = result.PasswordPolicy } return pwPolicy, err }
JavaScript
SDK for JavaScript V3
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Create the client.

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

Get the account password policy.

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { GetAccountPasswordPolicyCommand } from "@aws-sdk/client-iam"; const run = async () => { try { const data = await iamClient.send(new GetAccountPasswordPolicyCommand({})); console.log("Success", data.PasswordPolicy); } catch (err) { console.log("Error", err); } }; run();
PHP
SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

$uuid = uniqid(); $service = new IamService(); public function getAccountPasswordPolicy() { return $this->iamClient->getAccountPasswordPolicy(); }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

def print_password_policy(): """ Prints the password policy for the account. """ try: pw_policy = iam.AccountPasswordPolicy() print("Current account password policy:") print(f"\tallow_users_to_change_password: {pw_policy.allow_users_to_change_password}") print(f"\texpire_passwords: {pw_policy.expire_passwords}") print(f"\thard_expiry: {pw_policy.hard_expiry}") print(f"\tmax_password_age: {pw_policy.max_password_age}") print(f"\tminimum_password_length: {pw_policy.minimum_password_length}") print(f"\tpassword_reuse_prevention: {pw_policy.password_reuse_prevention}") print(f"\trequire_lowercase_characters: {pw_policy.require_lowercase_characters}") print(f"\trequire_numbers: {pw_policy.require_numbers}") print(f"\trequire_symbols: {pw_policy.require_symbols}") print(f"\trequire_uppercase_characters: {pw_policy.require_uppercase_characters}") printed = True except ClientError as error: if error.response['Error']['Code'] == 'NoSuchEntity': print("The account does not have a password policy set.") else: logger.exception("Couldn't get account password policy.") raise else: return printed
Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

# Prints the password policy for the account. def print_account_password_policy policy = @iam_resource.account_password_policy policy.load puts("The account password policy is:") puts(policy.data.to_h) rescue Aws::Errors::ServiceError => e if e.code == "NoSuchEntity" puts("The account does not have a password policy.") else puts("Couldn't print the account password policy. Here's why:") puts("\t#{e.code}: #{e.message}") raise end end
Rust
SDK for Rust
Note

This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

pub async fn get_account_password_policy( client: &iamClient, ) -> Result<GetAccountPasswordPolicyOutput, SdkError<GetAccountPasswordPolicyError>> { let response = client.get_account_password_policy().send().await?; Ok(response) }