What is likely causing this issue?
The two affected instances failed to fetch the new deployment.
A failed AfterInstall lifecycle event hook caused the CodeDeploy agent to roll back to the previous version on the affected instances.
The CodeDeploy agent was not installed in two affected instances.
EC2 Auto Scaling launched two new instances while the new deployment had not yet finished, causing the previous version to be deployed on the affected instances.
Explanations:
If the affected instances failed to fetch the new deployment, the deployment would have failed outright for those instances. Since the deployment finished successfully, this isn’t the cause.
A failed AfterInstall lifecycle event hook would cause a rollback, but only for those instances that experienced the failure. Since the deployment was successful, this isn’t the reason for the issue.
If the CodeDeploy agent were not installed on the affected instances, the deployment would have failed completely for those instances, which is not the case here.
EC2 Auto Scaling could have launched new instances during the deployment, and if the new instances were not updated with the latest revision in time, they would have received the previous version, causing the discrepancy.