BrainyBeeBrainyBee
ExploreBlogStart Studying
HomeAWS Certified DevOps Engineer - Professional (DOP-C02)Mastering Application Health via Exit Codes
Study Guide820 words

Mastering Application Health via Exit Codes

Measuring application health based on application exit codes

Mastering Application Health via Exit Codes

Measuring application health based on exit codes is a fundamental skill for DevOps engineers. In the context of AWS, these numeric values (0–255) are the primary mechanism by which services like AWS CodeBuild, AWS CodeDeploy, and Amazon ECS determine if a process succeeded or failed.

Learning Objectives

  • Define standard POSIX exit codes and their meanings.
  • Implement logic in CI/CD pipelines to catch and respond to non-zero exit codes.
  • Configure AWS CodeBuild and CodeDeploy to interpret application health through script return values.
  • Distinguish between application-level failures and system-level signals (e.g., SIGKILL vs. SIGTERM).

Key Terms & Glossary

  • Exit Code (Return Code): An integer returned by a child process to its parent process when it finishes execution.
  • POSIX: A set of formal standards that define the API for software compatible with Unix/Linux systems, including exit code conventions.
  • $?: A shell variable in Linux/Unix that stores the exit status of the last executed command.
  • SIGKILL (137): An exit code indicating the process was forcefully terminated (often due to Out of Memory/OOM conditions).
  • Idempotency: The property where an operation can be applied multiple times without changing the result beyond the initial application, critical for exit code retry logic.

The "Big Idea"

In an automated ecosystem like AWS, the infrastructure cannot "see" your code's internal logic. It relies on the Exit Code Interface. By standardizing how your application exits, you enable AWS services to automatically roll back failed deployments, trigger CloudWatch alarms, or halt a pipeline before bad code reaches production.

Formula / Concept Box

Exit CodeTypical MeaningAWS Service Action
0SuccessContinue to next step/Success state
1General ErrorStop pipeline / Mark Build as FAILED
126Command cannot executeCheck permissions on the script/binary
127Command not foundCheck $PATH or installation step
130Terminated by Ctrl+CManual intervention detected
137Fatal error (Signal 9)Often indicates Out of Memory (OOM) in ECS

Hierarchical Outline

  • I. Anatomy of an Exit Code
    • Standard Range: 0 to 255.
    • Reserved Codes: 1, 2, 126, 127, 128+ are reserved by the shell.
  • II. Integration with AWS CodeBuild
    • buildspec.yml execution: Each command in the commands section must return 0 for the phase to succeed.
    • finally blocks: Commands that run regardless of previous exit codes.
  • III. Health Checks in AWS CodeDeploy
    • Lifecycle Event Hooks: Scripts (e.g., ValidateService) must exit with 0 to proceed to the next lifecycle event.
  • IV. Container Health (ECS/EKS)
    • Task State: ECS monitors the exit code of the primary container to determine if the Task should be restarted.

Visual Anchors

Application Execution Flow

Loading Diagram...
Figure 1 — Mermaid diagram

Mapping Exit Code Categories

Compiling TikZ diagram…
⏳
Running TeX engine…
This may take a few seconds
Figure 2 — TikZ diagram

Definition-Example Pairs

  • Term: Exit Code 127
  • Definition: The shell was unable to find the command specified in the script.
  • Example: In a CodeBuild buildspec.yml, if you type npm runn build (typo) instead of npm run build, the shell returns 127, and AWS CodeBuild marks the phase as failed immediately.

Worked Examples

Example 1: Custom Health Check Script

Suppose you need to verify if a database is reachable before allowing a CodeDeploy deployment to finish. You write a script check_db.sh:

bash
#!/bin/bash # Attempt to ping the DB python3 check_connection.py STATUS=$? if [ $STATUS -eq 0 ]; then echo "Database is healthy." exit 0 else echo "Database unreachable! Code: $STATUS" exit 1 fi

[!TIP] Always capture the exit status immediately using STATUS=$? because subsequent commands (like echo) will overwrite the $? variable.

Checkpoint Questions

  1. What exit code is returned if a process is killed by the system due to an Out of Memory (OOM) error?
  2. In a shell script, which special variable holds the exit code of the last executed command?
  3. True or False: If an AWS CodeDeploy AfterInstall script exits with code 1, the deployment will still be marked as successful.
▶Click to expand answers
  1. 137 (128 + Signal 9).
  2. $?
  3. False. Any non-zero exit code in a CodeDeploy hook causes the deployment to fail.

Muddy Points & Cross-Refs

  • Exit Code 137 vs 143: Both indicate external termination. 137 is a SIGKILL (immediate, no cleanup), while 143 is SIGTERM (graceful request to stop). Understanding this helps diagnose if your ECS task was stopped by the user (143) or crashed by the kernel (137).
  • Cross-Ref: For more on how these codes trigger alerts, see the CloudWatch Alarms study guide.

Comparison Tables

Shell Built-in Exit Codes

CodeReasonFix
126Permission DeniedRun chmod +x script.sh
127Command Not FoundCheck your PATH or installation path
128+nFatal Error Signal "n"Identify signal (e.g., n=9 is SIGKILL)
All AWS Certified DevOps Engineer - Professional (DOP-C02) Study Resources

Related Notes

  • Mastering AWS Alerting and Automated Remediation1,050 words
  • Study Guide: Analyzing Failed Deployments in AWS940 words
  • Incident Analysis: Troubleshooting Failed Processes in AWS1,050 words
  • Mastering AWS Monitoring & Security Analytics: Logs, Metrics, and Findings1,050 words
  • AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch920 words
  • Analyzing Real-Time Log Streams with Amazon Kinesis Data Streams985 words
  • CloudWatch Anomaly Detection Alarms: Professional Study Guide820 words
  • AWS Application Storage Patterns: EBS, EFS, and S31,054 words
  • Lab: Automating Security Controls and Data Protection with AWS Secrets Manager and Config942 words
  • Master Study Guide: Automating Security Controls & Data Protection (AWS DOP-C02)1,184 words
  • Mastering AWS CloudFormation StackSets: Multi-Account & Multi-Region Orchestration895 words
  • Mastering System Configuration Changes in AWS945 words

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up.

Start Studying

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free
AWS Certified DevOps Engineer - Professional (DOP-C02) ResourcesExplore All HivesBlogHome

© 2026 BrainyBee. Free AI-powered exam prep.

Loading Diagram...
Flowchart, top to bottom. Start Script/Process connects to Command Success?. B connects to Exit 0 (Yes). B connects to Exit 1-255 (No). C connects to AWS Pipeline: PROCEED. D connects to AWS Pipeline: FAIL/HALT. F connects to Trigger CloudWatch Alarm.