/
__init__.py
181 lines (157 loc) · 6.34 KB
/
__init__.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import logging
from urllib.request import urlopen, Request
from time import sleep
import boto3
import json
from botocore.exceptions import ClientError
class AppRunnerCustomDomainProvider:
"""
temporary implementation of AWS::AppRunner::CustomDomain
"""
def __init__(self, apprunner, sleep_time: float = 10.0):
self.apprunner = apprunner
self.sleep_time = sleep_time
if not self.apprunner:
self.apprunner = boto3.client("apprunner")
@staticmethod
def extract_attributes(response: dict) -> dict:
set_identifier = response["ServiceArn"].split("/")[-1]
return {
"DNSTarget": response["DNSTarget"],
"ValidationResourceRecords": [
{
"Name": r["Name"],
"Type": r["Type"],
"ResourceRecords": [r["Value"]],
"SetIdentifier": set_identifier,
"Weight": "100",
"TTL": "60",
}
for r in response["CustomDomain"]["CertificateValidationRecords"]
],
}
def create(self, request, response):
"""
associates a Custom Domain with an AppRunner service
"""
kwargs = {
"ServiceArn": request.get("ResourceProperties", {}).get("ServiceArn"),
"DomainName": request.get("ResourceProperties", {}).get("DomainName"),
"EnableWWWSubdomain": False,
}
_ = self.apprunner.associate_custom_domain(**kwargs)
response["PhysicalResourceId"] = "{ServiceArn},{DomainName}".format(**kwargs)
self.read(request, response)
def update(self, request, response):
"""
updates an association of a Custom Domain with an AppRunner service
"""
kwargs = {
"ServiceArn": request.get("ResourceProperties", {}).get("ServiceArn"),
"DomainName": request.get("ResourceProperties", {}).get("DomainName"),
}
old_kwargs = {
"ServiceArn": request.get("OldResourceProperties", {}).get("ServiceArn"),
"DomainName": request.get("OldResourceProperties", {}).get("DomainName"),
}
if old_kwargs == kwargs:
response["Reason"] = "Nothing has changed"
response["PhysicalResourceId"] = request["PhysicalResourceId"]
self.read(request, response)
return
self.create(request, response)
def read(self, request, response):
"""
reads the current custom domain and sets the resource attributes.
"""
service_arn = request.get("ResourceProperties", {}).get("ServiceArn")
domain_name = request.get("ResourceProperties", {}).get("DomainName")
attempt = 0
custom_domain = {}
while custom_domain.get("Status", "creating") == "creating" and attempt < 6:
custom_domains = self.apprunner.describe_custom_domains(
ServiceArn=service_arn
)
custom_domain = next(
filter(
lambda r: r["DomainName"] == domain_name
and r["Status"] != "creating",
custom_domains["CustomDomains"],
),
{},
)
if not custom_domain:
attempt += 1
logging.info(
f"sleeping {self.sleep_time}s to await the arrival of the validation records"
)
sleep(self.sleep_time)
if not custom_domain:
response["Status"] = "FAILED"
response["Reason"] = f"No custom domain found for {domain_name}"
return
custom_domains["CustomDomain"] = custom_domain
response["Data"] = self.extract_attributes(custom_domains)
response["Status"] = "SUCCESS"
def delete(self, request, response):
"""
disassociate a Custom Domain from an AppRunner service
"""
if not request["PhysicalResourceId"].startswith("arn:aws:apprunner"):
response["Reason"] = "ignoring failed create"
response["Status"] = "SUCCESS"
return
kwargs = {
"ServiceArn": request.get("ResourceProperties", {}).get("ServiceArn"),
"DomainName": request.get("ResourceProperties", {}).get("DomainName"),
}
try:
_ = self.apprunner.disassociate_custom_domain(**kwargs)
except ClientError as error:
response["Reason"] = "ignoring error to disassociate custom domain"
logging.error("%s, %s", response["Reason"], error)
response["Status"] = "SUCCESS"
def handle(self, request):
"""
handles a cloudformation resource create, update or delete request
"""
response = {
"StackId": request["StackId"],
"RequestId": request["RequestId"],
"LogicalResourceId": request["LogicalResourceId"],
"Status": "SUCCESS",
}
if request.get("PhysicalResourceId", None):
response["PhysicalResourceId"] = request["PhysicalResourceId"]
try:
request_type = request.get("RequestType")
if request_type == "Create":
self.create(request, response)
elif request_type == "Update":
self.update(request, response)
elif request_type == "Delete":
self.delete(request, response)
else:
response["Status"] = "FAILED"
response["Reason"] = f"unknown request type {request_type}"
except Exception as error:
response["Status"] = "FAILED"
response["Reason"] = f"exception while processing {request_type}, {error}"
logging.error("%s, %s", response["Reason"], error)
return response
def post_response_to_cloudformation(url: str, response: dict):
""" "
posts the response message to CloudFormation
"""
data = json.dumps(response).encode("utf-8")
logging.error("%s", data)
put_request = Request(
url=url,
data=data,
headers={"Content-Length": len(data), "Content-Type": ""},
method="PUT",
)
urlopen(put_request)
def handler(request, _):
provider = AppRunnerCustomDomainProvider(boto3.client("apprunner"))
post_response_to_cloudformation(request["ResponseURL"], provider.handle(request))