# Buggy code
def _update_domain_name_v2(self, api_args):
    # type: (Dict[str, Any]) -> Dict[str, Any]
    client = self._client("apigatewayv2")
    exceptions = (client.exceptions.TooManyRequestsException,)

    result = self._call_client_method_with_retries(
        client.update_domain_name,
        api_args,
        max_attempts=6,
        should_retry=lambda x: True,
        retryable_exceptions=exceptions,
    )
    result_data = result["DomainNameConfigurations"][0]
    domain_name = {
        "domain_name": result["DomainName"],
        "endpoint_configuration": result_data["EndpointType"],
        "security_policy": result_data["SecurityPolicy"],
        "hosted_zone_id": result_data["HostedZoneId"],
        "certificate_arn": result_data["CertificateArn"],
    }
    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_v2(self, api_args):
    # type: (Dict[str, Any]) -> DomainNameResponse
    client = self._client("apigatewayv2")
    exceptions = (client.exceptions.TooManyRequestsException,)

    result = self._call_client_method_with_retries(
        client.update_domain_name,
        api_args,
        max_attempts=6,
        should_retry=lambda x: True,
        retryable_exceptions=exceptions,
    )
    result_data = result["DomainNameConfigurations"][0]
    domain_name = {
        "domain_name": result["DomainName"],
        "alias_domain_name": result_data["ApiGatewayDomainName"],
        "security_policy": result_data["SecurityPolicy"],
        "hosted_zone_id": result_data["HostedZoneId"],
        "certificate_arn": result_data["CertificateArn"],
    }  # type: DomainNameResponse
    return domain_name