Topic 1 Question #3
A company is adopting AWS CodeDeploy to automate its application deployments for a Java-Apache Tomcat application with an Apache Webserver. The development team started with a proof of concept, created a deployment group for a developer environment, and performed functional tests within the application. After completion, the team will create additional deployment groups for staging and production.The current log level is configured within the Apache settings, but the team wants to change this configuration dynamically when the deployment occurs, so that they can set different log level configurations depending on the deployment group without having a different application revision for each group.How can these requirements be met with the LEAST management overhead and without requiring different script versions for each deployment group?
- A.
Tag the Amazon EC2 instances depending on the deployment group. Then place a script into the application revision that calls the metadata service and the EC2 API to identify which deployment group the instance is part of. Use this information to configure the log level settings. Reference the script as part of the AfterInstall lifecycle hook in the appspec.yml file.
- B.
Create a script that uses the CodeDeploy environment variable DEPLOYMENT_GROUP_ NAME to identify which deployment group the instance is part of. Use this information to configure the log level settings. Reference this script as part of the BeforeInstall lifecycle hook in the appspec.yml file.
- C.
Create a CodeDeploy custom environment variable for each environment. Then place a script into the application revision that checks this environment variable to identify which deployment group the instance is part of. Use this information to configure the log level settings. Reference this script as part of the ValidateService lifecycle hook in the appspec.yml file.
- D.
Create a script that uses the CodeDeploy environment variable DEPLOYMENT_GROUP_ID to identify which deployment group the instance is part of to configure the log level settings. Reference this script as part of the Install lifecycle hook in the appspec.yml file.
Answer: B
This scenario requires environment-specific Apache log level configuration during CodeDeploy deployments with minimal management overhead, no separate application revisions, and no per-deployment-group script versions. The optimal solution leverages native CodeDeploy functionality to avoid custom configuration, additional IAM permissions, or external API calls. The suggested answer B uses the predefined DEPLOYMENT_GROUP_NAME environment variable that CodeDeploy automatically injects into all lifecycle hook script execution environments, eliminating the need for custom variable configuration or external context lookups. Running the configuration script in the BeforeInstall lifecycle hook ensures the log level setting is applied before the application is installed and started, so the Apache service uses the correct log level immediately upon launch with no extra restarts required. This approach meets all requirements with the least management overhead, as no additional resources, permissions, or per-group configuration are needed beyond the single universal script referenced in the appspec.yml that works across all deployment groups.
Option Analysis:
A. Incorrect. Tagging EC2 instances per deployment group and calling the EC2 metadata service and EC2 API adds unnecessary management overhead, including maintaining instance tag consistency, adding IAM permissions for instances to call EC2 APIs, and risk of error if instances are reused across deployment groups. This approach is far less efficient than using native CodeDeploy environment variables.
B. Correct. CodeDeploy natively provides the DEPLOYMENT_GROUP_NAME predefined environment variable to all lifecycle hook scripts with no extra configuration required. Using this human-readable variable to select the appropriate log level requires no custom setup, no separate script versions, and no additional API calls. The BeforeInstall lifecycle hook is appropriate for this configuration change, as it runs before the application is installed or started, ensuring the log level is applied before the Apache service launches.
C. Incorrect. Creating custom CodeDeploy environment variables for each deployment group adds unnecessary management overhead compared to using the native, out-of-the-box DEPLOYMENT_GROUP_NAME variable. Additionally, the ValidateService lifecycle hook runs after the application has already started, so modifying log level at this stage would require a restart of Apache, introducing extra steps and potential downtime, making this approach inefficient and high risk.
D. Incorrect. While DEPLOYMENT_GROUP_ID is a valid predefined CodeDeploy variable, it is an opaque alphanumeric identifier, not a human-readable name, so mapping IDs to log levels requires extra maintenance whenever deployment groups are added, removed, or modified, increasing management overhead. Additionally, the Install lifecycle hook runs while the application revision is being unpacked and copied to its target location, so modifying configuration during this phase can lead to conflicts with the deployment process itself, making this an inappropriate hook for configuration changes.
Key Concepts:
1. CodeDeploy Predefined Environment Variables: CodeDeploy automatically injects a set of standard environment variables during all lifecycle hook executions, including DEPLOYMENT_GROUP_NAME, DEPLOYMENT_GROUP_ID, and APPLICATION_NAME, which eliminate the need for custom logic or external API calls to identify deployment context.
2. CodeDeploy Lifecycle Hook Order: The sequence of CodeDeploy lifecycle hooks (ApplicationStop, DownloadBundle, BeforeInstall, Install, AfterInstall, ApplicationStart, ValidateService) dictates the appropriate timing for configuration changes to avoid conflicts, ensure settings are applied before application startup, and prevent unneeded service restarts.
3. Multi-Environment Deployment Best Practices: Deployment groups are designed to support environment-specific configurations without requiring separate application revisions, leveraging native CodeDeploy features to minimize management overhead for dev, staging, and production deployment pipelines.
References:
CodeDeploy AppSpec File Structure - Hooks,
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html
CodeDeploy Predefined Environment Variables,
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-environment-variables.html