| 9 | secrets_manager = boto3.client('secretsmanager') |
| 10 | |
| 11 | def on_create(event): |
| 12 | response = iot.create_keys_and_certificate(setAsActive=True) |
| 13 | certificate_id = response['certificateId'] |
| 14 | certificate_pem = response['certificatePem'] |
| 15 | key_pair = response['keyPair'] |
| 16 | |
| 17 | if not certificate_id or not certificate_pem or not key_pair: |
| 18 | raise ValueError('Failed to create keys and certificate') |
| 19 | |
| 20 | secrets_manager.create_secret( |
| 21 | Name=SECRET_NAME, |
| 22 | SecretString=json.dumps({ |
| 23 | 'cert': certificate_pem, |
| 24 | 'keyPair': key_pair['PrivateKey'] |
| 25 | }) |
| 26 | ) |
| 27 | |
| 28 | return { |
| 29 | 'PhysicalResourceId': certificate_id, |
| 30 | 'Data': { |
| 31 | 'certificateId': certificate_id |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | def on_delete(event): |
| 36 | try: |