in cli/src/pcluster/models/imagebuilder.py [0:0]
def delete(self, force=False): # noqa: C901
"""Delete CFN Stack and associate resources and deregister the image."""
if force or (not self._check_instance_using_image() and not self._check_image_is_shared()):
try:
if AWSApi.instance().cfn.stack_exists(self.image_id):
if self.stack.imagebuilder_image_is_building:
raise BadRequestImageBuilderActionError(
"Image cannot be deleted because EC2 ImageBuilder Image has a running workflow."
)
# Delete stack
AWSApi.instance().cfn.delete_stack(self.image_id)
if AWSApi.instance().ec2.image_exists(image_id=self.image_id):
# Deregister image
AWSApi.instance().ec2.deregister_image(self.image.id)
# Delete snapshot
for snapshot_id in self.image.snapshot_ids:
AWSApi.instance().ec2.delete_snapshot(snapshot_id)
elif AWSApi.instance().ec2.failed_image_exists(image_id=self.image_id):
# Deregister image
AWSApi.instance().ec2.deregister_image(self.failed_image.id)
# Delete snapshot
for snapshot_id in self.failed_image.snapshot_ids:
AWSApi.instance().ec2.delete_snapshot(snapshot_id)
# Delete s3 image directory
try:
self.bucket.check_bucket_exists()
self.bucket.delete_s3_artifacts()
except AWSClientError:
logging.warning(
"S3 bucket associated to the image does not exist, skip image s3 artifacts deletion."
)
# Delete log group
try:
AWSApi.instance().logs.delete_log_group(self._log_group_name)
except AWSClientError:
logging.warning("Unable to delete log group %s.", self._log_group_name)
except (AWSClientError, ImageError) as e:
raise _imagebuilder_error_mapper(e, f"Unable to delete image and stack, due to {str(e)}")