Files
saladeaula.digital/users-events/app/events/set_custom_domain.py

50 lines
1.4 KiB
Python

from aws_lambda_powertools.utilities.data_classes import (
EventBridgeEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from cloudflare import Cloudflare
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from boto3clients import dynamodb_client
from config import CLOUDFLARE_ZONE_ID, USER_TABLE
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
cf_client = Cloudflare()
@event_source(data_class=EventBridgeEvent)
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
try:
custom_hostname = cf_client.custom_hostnames.create(
zone_id=CLOUDFLARE_ZONE_ID,
hostname=new_image['hostname'],
ssl={
'type': 'dv',
'method': 'http',
'settings': {
'tls_1_3': 'on',
'min_tls_version': '1.2',
},
},
)
dyn.update_item(
key=KeyPair(
pk=new_image['id'],
sk=new_image['sk'],
),
update_expr='SET hostname_id = :hostname_id, updated_at = :now',
expr_attr_values={
':hostname_id': custom_hostname.id, # type: ignore
':now': now(),
},
)
except Exception:
pass
return True