-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-tag-amplify-apps.py
70 lines (53 loc) · 2.16 KB
/
aws-tag-amplify-apps.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import logging
import os
import sys
from botocore.exceptions import ClientError
sys.path.append(os.path.dirname(os.getcwd()))
# pylint: disable=wrong-import-position
from aws_service_name import AwsServiceName as aws
from common_funcs import CommonFuncs
logger = logging.getLogger()
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
base_steps_client_names: list[str] = [aws.amplify_str]
cf = CommonFuncs(
logger,
filename=str(os.path.basename(__file__)),
json_paths=base_steps_client_names,
)
amplify_list_apps_file: str = "amplify-list-apps.json"
# NB. To generate this file, run the following AWS CLI command (in the cwd):
# `aws amplify list-apps --query 'apps[].{appArn:appArn,name:name,tags:tags}' > amplify-list-apps.json`
# NB. To get AWS Amplify apps starting with `<str>`, use query:
# `... 'apps[?starts_with(name, `<str>`) == `true`].{appArn:appArn,name:name,tags:tags}' ...`
def main(region: str):
cf.info_log_starting()
clients, res = cf.get_clients_and_res_objs(region, base_steps_client_names)
with open(amplify_list_apps_file, "r", encoding="utf-8") as f:
amplify_list_apps: dict = json.load(f)
res[aws.amplify_str]["tag_resource"] = {}
for i in amplify_list_apps:
try:
res[aws.amplify_str]["tag_resource"][i["name"]] = clients[aws.amplify_str].tag_resource(
resourceArn=i["appArn"], tags=i["tags"]
)
logger.info("## Amplify Tag Resource successful response")
except ClientError as ex:
logger.error(f"## Amplify Tag Resource ERROR: '{ex}'")
cf.write_to_json_paths(res, base_steps_client_names)
sys.exit(1)
cf.write_to_json_paths(res, base_steps_client_names)
cf.info_log_finished()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description=f"Tag all AWS Amplify apps, specified in local config file: '{amplify_list_apps_file}'"
)
parser.add_argument(
"--region",
required=True,
help="Specify the AWS region code, eg. '--region eu-west-2'.",
type=str,
)
args = parser.parse_args()
main(region=args.region)