This snippet provides a comprehensive solution for managing contact details across an entire AWS Organization. It combines two automation workflows to ensure both Primary and Alternate contact records are standardized and up-to-date across all member accounts.
⚙️ Capabilities
🛠️ Configuration Notes
Supporting Blog Post: https://blog.sentri.cloud/automating-contact-updates-across-accounts-in-your-organization
import boto3
# Assuming org_client is already initialized
org_client = boto3.client('organizations')
# Collect all accounts using paginator
accounts = []
paginator = org_client.get_paginator('list_accounts')
for page in paginator.paginate():
accounts.extend(page['Accounts'])
# Update the alternate contact details for each account in the organization
for account in accounts:
account_id = account['Id']
# Initialize a session using the AWS account
account_session = boto3.Session()
account_client = account_session.client('account')
# Now you can use account_client to update alternate contact details or perform other actions
try:
# Update alternate contact details for the account
account_client.put_alternate_contact(
AccountId=account_id,
AlternateContactType='SECURITY',
Title='',
EmailAddress='',
Name='',
PhoneNumber=''
)
account_client.put_alternate_contact(
AccountId=account_id,
AlternateContactType='OPERATIONS',
Title='',
EmailAddress='',
Name='',
PhoneNumber=''
)
account_client.put_alternate_contact(
AccountId=account_id,
AlternateContactType='BILLING',
Title='',
EmailAddress='',
Name='',
PhoneNumber=''
)
print(f"Updated alternate contact details for account: {account_id}")
except Exception as e:
print(f"Failed to update alternate contact details for account: {account_id}. Error: {e}")
import boto3
def put_contact_information(account_id):
account_client = boto3.client('account')
response = account_client.put_contact_information(
AccountId=account_id,
ContactInformation={
'FullName': '',
'AddressLine1': '',
'City': '',
'CountryCode': '',
'CompanyName': '',
'FullName': '',
'PhoneNumber': '',
'PostalCode': '',
'StateOrRegion': '',
'WebsiteUrl': '',
}
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
print('Successfully updated contact information for account ID: {}'.format(account_id))
else:
print('Failed to update contact information for account ID: {}'.format(account_id))
def get_account_name(account_id):
"""
Retrieve the name of the AWS account.
:param account_id: ID of the AWS account
:return: Name of the AWS account
"""
client = boto3.client('organizations')
response = client.describe_account(AccountId=account_id)
return response['Account']['Name']
def main():
"""Loops through all accounts in an AWS organization and updates primary account information."""
organizations_client = boto3.client('organizations')
response = organizations_client.list_accounts()
for account in response['Accounts']:
account_id = account['Id']
# Skip/Exclude the master account(s)
if account_id != '222222222222' and account_id != '111111111111':
put_contact_information(account_id)
if __name__ == '__main__':
main()