Skip to content

Remove deployment ID on ec2

0

I use code pipeline to deploy app on ec2. Every deployments will store artifact in /opt/codedeploy-agent/deployment-root/deployment-group-id/deploymentID. I want to know that If I remove some deployment ID to free some spaces on EBS, does this affect the rollback behavior of code deploy ?

asked 2 years ago379 views
3 Answers
2

Yes, deleting deployment folders under /opt/codedeploy-agent/deployment-root/deployment-group-id/deploymentID can affect the rollback behavior in AWS CodeDeploy.

When CodeDeploy performs a deployment, it stores the deployment artifacts (such as files, scripts, and other resources) in these directories. If you remove a specific deployment directory (identified by its deployment ID), you might lose the ability to rollback to that particular deployment version, since CodeDeploy won't be able to access the necessary files to revert to that state.

What Happens if You Delete Deployment IDs:

Rollback Impact: If you delete a deployment directory, CodeDeploy won't be able to roll back to that deployment version because the artifacts will no longer be available. This can lead to a failed rollback if CodeDeploy tries to revert to a version whose artifacts have been deleted.

Space Management: Deleting old deployments can help free up disk space on your EBS volume, but it's essential to do this cautiously and ideally, only for older deployments that you are confident you won't need to rollback to.

Recommendations:

Retention Settings: Instead of manually deleting deployment directories, consider adjusting the retention settings in CodeDeploy to automatically manage the number of retained deployments. This way, CodeDeploy will automatically clean up old deployments while keeping enough history to support rollbacks.

Backup: If you still want to remove old deployments manually, consider taking a backup of the deployment directories before deletion, just in case you need to restore them later.

EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
EXPERT
reviewed 2 years ago
1
Accepted Answer

1. Automate Cleanup with a Script:

You can create a script to automatically delete old deployment directories while keeping a certain number of the most recent ones.

Here's a basic example in Bash that you could run as a cron job on your EC2 instances:

#!/bin/bash

DEPLOYMENT_ROOT="/opt/codedeploy-agent/deployment-root"
MAX_DEPLOYMENTS=5

for group in "$DEPLOYMENT_ROOT"/*/; do
    cd "$group" || continue
    deployments=($(ls -d */ | sort -r))
    
    if [ ${#deployments[@]} -gt $MAX_DEPLOYMENTS ]; then
        echo "Cleaning up old deployments in $group"
        to_delete=("${deployments[@]:$MAX_DEPLOYMENTS}")
        
        for deployment in "${to_delete[@]}"; do
            rm -rf "$deployment"
            echo "Deleted: $deployment"
        done
    fi
done

This script:

Lists all deployment directories in reverse order (newest first).

Keeps the latest $MAX_DEPLOYMENTS and deletes the rest.

Setup as a Cron Job: To run this script regularly, you can set it up as a cron job on your instances.

crontab -e

Add a line like this to run the script daily at midnight:

0 0 * * * /path/to/cleanup-script.sh

2. Use a CloudWatch Agent:

You can use the CloudWatch agent to monitor disk space and trigger an automated cleanup process when disk usage exceeds a certain threshold. This involves setting up a CloudWatch Alarm that triggers a Lambda function or an SSM Automation document to clean up old deployments.

3. Lambda Function for Cleanup (If the Instances Are Managed via Systems Manager):

If your instances are managed via AWS Systems Manager (SSM), you can create a Lambda function that periodically connects to your instances and performs the cleanup.

Here's an outline of the approach:

Create an SSM document that runs the cleanup script on your instances.

Schedule the document to run periodically via EventBridge (formerly CloudWatch Events)

4. Using AWS Systems Manager Maintenance Windows:

You can set up an AWS Systems Manager Maintenance Window that runs a cleanup command (similar to the script above) on a regular schedule. This method provides better control over when the cleanup occurs and ensures it only runs during specified times.

EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
  • Thanks for your response. I will try to implement that

0

Thanks you for response. I search for retention setup in codedeploy but do not see any. Can you show me to setup these ?

answered 2 years 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.