forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_format_function.py
More file actions
34 lines (25 loc) · 1.17 KB
/
custom_format_function.py
File metadata and controls
34 lines (25 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import re
import boto3
import custom_format_schema as schemas
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.validation import SchemaValidationError, validate
# awsaccountid must have 12 digits
custom_format = {"awsaccountid": lambda value: re.match(r"^(\d{12})$", value)}
def lambda_handler(event, context: LambdaContext) -> dict:
try:
# validate input using custom json format
validate(event=event, schema=schemas.INPUT, formats=custom_format)
client_organization = boto3.client("organizations", region_name=event.get("region"))
account_data = client_organization.describe_account(AccountId=event.get("accountid"))
return {
"account": json.dumps(account_data.get("Account"), default=str),
"message": "Success",
"statusCode": 200,
}
except SchemaValidationError as exception:
return return_error_message(str(exception))
except Exception as exception:
return return_error_message(str(exception))
def return_error_message(message: str) -> dict:
return {"account": None, "message": message, "statusCode": 400}