# Buggy code
def _update_domain_name(self, custom_domain_name, patch_operations):
    # type: (str, List[Dict[str, str]]) -> Dict[str, Any]
    client = self._client("apigateway")
    exceptions = (client.exceptions.TooManyRequestsException,)
    result = {}
    for patch_operation in patch_operations:
        api_args = {
            "domainName": custom_domain_name,
            "patchOperations": [patch_operation],
        }
        response = self._call_client_method_with_retries(
            client.update_domain_name,
            api_args,
            max_attempts=6,
            should_retry=lambda x: True,
            retryable_exceptions=exceptions,
        )
        result.update(response)

    domain_name = {
        "domain_name": result["domainName"],
        "endpoint_configuration": result["endpointConfiguration"],
        "security_policy": result["securityPolicy"],
    }
    if result.get("regionalCertificateArn"):
        domain_name["certificate_arn"] = result["regionalCertificateArn"]
    else:
        domain_name["certificate_arn"] = result["certificateArn"]

    if result.get("regionalHostedZoneId"):
        domain_name["hosted_zone_id"] = result["regionalHostedZoneId"]
    else:
        domain_name["hosted_zone_id"] = result["distributionHostedZoneId"]

    if result.get("regionalDomainName") is not None:
        domain_name["alias_domain_name"] = result["regionalDomainName"]
    else:
        domain_name["alias_domain_name"] = result["distributionDomainName"]
    return domain_name


# Traceback (KeyError)
Updating custom domain name: api.transferbank.com.br
Traceback (most recent call last):
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/cli/__init__.py", line 646, in main
return cli(obj={})
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/cli/__init__.py", line 206, in deploy
deployed_values = d.deploy(config, chalice_stage_name=stage)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/deploy/deployer.py", line 374, in deploy
return self._deploy(config, chalice_stage_name)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/deploy/deployer.py", line 390, in _deploy
self._executor.execute(plan)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/deploy/executor.py", line 42, in execute
getattr(self, '_do_%s' % instruction.__class__.__name__.lower(),
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/deploy/executor.py", line 55, in _do_apicall
result = method(**final_kwargs)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/awsclient.py", line 702, in update_domain_name
updated_domain_name = self._update_domain_name_v2(kwargs)
File "/Users/rafagan/.virtualenvs/tb-websocket-ws/lib/python3.8/site-packages/chalice/awsclient.py", line 802, in _update_domain_name_v2
'hosted_zone_id': result_data['HostedZoneId'],
KeyError: 'HostedZoneId'

# Fixed code
def _update_domain_name(self, custom_domain_name, patch_operations):
    # type: (str, List[Dict[str, str]]) -> DomainNameResponse
    client = self._client("apigateway")
    exceptions = (client.exceptions.TooManyRequestsException,)
    result = {}
    for patch_operation in patch_operations:
        api_args = {
            "domainName": custom_domain_name,
            "patchOperations": [patch_operation],
        }
        response = self._call_client_method_with_retries(
            client.update_domain_name,
            api_args,
            max_attempts=6,
            should_retry=lambda x: True,
            retryable_exceptions=exceptions,
        )
        result.update(response)

    if result.get("regionalCertificateArn"):
        certificate_arn = result["regionalCertificateArn"]
    else:
        certificate_arn = result["certificateArn"]

    if result.get("regionalHostedZoneId"):
        hosted_zone_id = result["regionalHostedZoneId"]
    else:
        hosted_zone_id = result["distributionHostedZoneId"]

    if result.get("regionalDomainName") is not None:
        alias_domain_name = result["regionalDomainName"]
    else:
        alias_domain_name = result["distributionDomainName"]
    domain_name = {
        "domain_name": result["domainName"],
        "security_policy": result["securityPolicy"],
        "certificate_arn": certificate_arn,
        "hosted_zone_id": hosted_zone_id,
        "alias_domain_name": alias_domain_name,
    }  # type: DomainNameResponse
    return domain_name