Repository navigation
Expand file tree
/
Copy pathactivate-pro.py
More file actions
58 lines (51 loc) · 2.68 KB
/
Copy pathactivate-pro.py
File metadata and controls
58 lines (51 loc) · 2.68 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""Approve only the expected CloudFront PRO plan, or verify it with --check.
Requires AWS CLI v2 with pricing-plan-manager support and the SAME credentials
as OpenTofu. Never creates a subscription; CloudFormation owns that resource.
"""
import argparse
import json
import os
import subprocess
import time
def aws(*args):
result = subprocess.run(
["aws", "pricing-plan-manager", *args, "--region", "us-east-1",
"--output", "json", "--no-cli-pager"],
check=True, capture_output=True, text=True, timeout=90)
return json.loads(result.stdout)
def activate(arn, distribution, waf, check_only=False, call=aws, attempts=120, sleep=time.sleep):
for _ in range(attempts):
response = call("get-subscription", "--arn", arn)
sub = response["subscription"]
if sub["arn"] != arn or sub["planFamily"] != "CloudFront" or sub["planTier"] != "PRO":
raise RuntimeError("Refusing unexpected subscription identity or plan (must be CloudFront PRO)")
if sub.get("currentPlanTier", "PRO") != "PRO" or sub.get("usageLevel", "DEFAULT") != "DEFAULT":
raise RuntimeError("Unexpected billed tier or usage level")
resources = set(sub["resourceArns"])
if distribution not in resources or waf not in resources:
raise RuntimeError("Subscription does not cover the expected distribution and WAF")
if sub.get("scheduledChange"):
raise RuntimeError("Subscription has a pending cancellation/downgrade; resolve it explicitly")
status = sub["status"]
if status == "ACTIVE":
print("Verified ACTIVE CloudFront PRO subscription", flush=True)
return
if check_only:
raise RuntimeError(f"Subscription is {status}, not ACTIVE")
if status == "PENDING_APPROVAL":
print("Approving CloudFront PRO ($15/month)", flush=True)
call("approve-paid-subscription", "--arn", arn, "--if-match", response["eTag"])
elif status != "SYNC_IN_PROGRESS":
raise RuntimeError(f"Subscription is {status}: {sub.get('statusReason', '')}")
sleep(5)
raise RuntimeError("Timed out waiting for ACTIVE PRO; publication must not proceed")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true")
args = parser.parse_args()
try:
activate(os.environ["SUBSCRIPTION_ARN"], os.environ["DISTRIBUTION_ARN"],
os.environ["WAF_ARN"], check_only=args.check)
except (RuntimeError, KeyError, ValueError, OSError, subprocess.SubprocessError) as error:
raise SystemExit(f"PRO subscription check failed: {error}")