question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iot client register_certificate missing resourceArn and resourceId in ResourceAlreadyExistsException

See original GitHub issue

I’m working on a registration script. When I register a certificate, which is already registered I get back ResourceAlreadyExistsException.

import boto3
import botocore
from collections import namedtuple

#debug
boto3.set_stream_logger(name='botocore')

CAResponse = namedtuple("CAResponse", "certId arn")

client = boto3.client('iot')
try:
    with open("device_pem", 'r') as device_pem_file, open("rootCA.pem", 'r') as root_pem_file:
        device_pem_string = device_pem_file.read()
        root_pem_string = root_pem_file.read()
        try:
            response = self.client.register_certificate(
                certificatePem=device_pem_string,
                caCertificatePem=root_pem_string
            )
            if "certificateId" in response and "certificateArn" in response:
                return CAResponse(certId=response["certificateId"], arn=response["certificateArn"])

        except botocore.exceptions.ClientError as e:
            print "%s register errror %r " % (e, e.response)
            raise e

except IOError as e:
    raise e

The exception is a botocore.errorfactory.ResourceAlreadyExistsException which I can’t seem to find the BotoCore source. When I cast it as a botocore.exceptions.ClientError I only get a Message & Code in the error.response.

With the logger running I can see that AWS gives the following response:

2017-02-13 17:03:52,634 botocore.parsers [DEBUG] Response body:
{
"message":"The certificate is already provisioned or registered",
"resourceArn":"target_arn",
"resourceId":"target_id"
}

Is there any way to retrieve this response body, or parse it out of the exception? As I see it there’s no function to retrieve the resourceArn & rescueId only by going through all of them wich will be a hassle when working with thousands of devices.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
apregoecommented, Dec 21, 2018

Hi! you have to catch it from the client itself. eg from a cloudwatch logs client:

    cw_client = boto3.client('logs')
    try:
        cw_client.create_log_stream(
            logGroupName='someloggroup',
            logStreamName='somestream'
        )
    except cw_client.exceptions.ResourceAlreadyExistsException as e:
        print('Log Stream already created. Continuing to publishing the log')
1reaction
mschepercommented, Jun 27, 2017

It seems this exception class gets created on the fly, so I don’t think it’s possible to import it. Has anybody found a way to catch it? I can import and catch botocore.errorfactory.BaseClientExceptions; is there a function to test whether it’s a ResourceAlreadyExistsException?

Read more comments on GitHub >

github_iconTop Results From Across the Web

RegisterCertificate - AWS IoT
Registers a device certificate with AWS IoT in the same certificate mode as the signing CA. If you have more than one CA...
Read more >
class IoT. Client - Boto3 Docs 1.26.33 documentation - AWS
A low-level client representing AWS IoT ... import boto3 client = boto3.client('iot') ... ResourceAlreadyExistsException; IoT.Client.exceptions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found