Skip to content

Batch Job Definitions are not able to be deleted, but this blocks the CDK from creating these resources

0

We get a "resource already exists in stack" error using the CDK when trying to create an AWS Batch job definition in one of our Stacks. The job definition name already exists (manually created before the move to CDK) so the CDK can't create it, but we can't fully delete it to allow the CDK to create it. It appears batch job definitions can only be deactivated and then automatically deleted after 180 days.

We don't have the option to use a new/different job definition name in the CDK, but also can't wait 180 days to have the batch job definition deleted before redeploying our Stack. What are we supposed to do in this scenario?

1 Answer
0

You're correct that AWS Batch job definitions cannot be immediately deleted and are only permanently deleted after 180 days following deregistration. This creates a challenge when migrating from manually created resources to CDK-managed ones.

Since you can't wait 180 days and can't change the job definition name, you have a few options:

  1. Import the existing job definition into your CDK stack using the appropriate import methods. This would allow the CDK to manage the existing resource rather than trying to create a new one.

  2. Use a revision strategy. AWS Batch job definitions are versioned with revisions. When you register a job definition with the same name as an existing one, it creates a new revision rather than replacing the old one. You could modify your CDK code to work with this revision system rather than expecting to create a brand new resource.

  3. If using the AWS CDK Batch Alpha construct, check if you can configure it to use a specific revision or to update an existing job definition rather than creating a new one.

  4. As a last resort, you could temporarily modify your CDK stack to use a different name for the job definition, deploy it, and then update your application to use this new definition. Once the old definition is eventually deleted (after 180 days), you could switch back to the original name if needed.

The key issue is that AWS Batch job definitions are designed to maintain historical versions for tracking and auditing purposes, which is why they can't be immediately deleted.
Sources
How do you clean up old Batch job definitions? | AWS re:Post
Use RegisterJobDefinition with an AWS SDK or CLI - AWS SDK Code Examples
interface JobDefinitionContainer · AWS CDK
Using the Terraform AWS Cloud Control provider for managing AWS Batch resources | AWS HPC Blog

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Relevant content