
Answer-first summary for fast verification
Answer: Use EC2 Auto Scaling lifecycle hooks to run a custom script to send data to the audit system when instances are launched and terminated.
## Explanation **Correct Answer: B - Use EC2 Auto Scaling lifecycle hooks to run a custom script to send data to the audit system when instances are launched and terminated.** ### Why Option B is Correct: 1. **Lifecycle Hooks Purpose**: EC2 Auto Scaling lifecycle hooks are specifically designed to pause instances in specific states (like launching or terminating) to perform custom actions. 2. **Both Launch and Termination Events**: Lifecycle hooks can be configured for both `EC2_INSTANCE_LAUNCHING` and `EC2_INSTANCE_TERMINATING` events, which perfectly matches the requirement to send data "as soon as they are launched and terminated." 3. **Immediate Execution**: When a lifecycle hook is triggered, the instance is paused in its current state, allowing scripts to run immediately before the instance transitions to the next state. 4. **Reliability**: Lifecycle hooks ensure the script runs even if the instance fails to boot properly or terminates unexpectedly. ### Why Other Options are Less Efficient: **Option A (Scheduled Lambda function):** - Not immediate - relies on scheduling - Requires remote execution which may fail if instances aren't reachable - More complex architecture **Option C (User data in launch configuration):** - Only works for launch events, not termination events - User data scripts run during boot, but may fail if the instance doesn't boot successfully - No mechanism for termination events **Option D (Custom script invoked by Auto Scaling group):** - Auto Scaling groups don't have built-in mechanisms to invoke scripts on instances - This would require custom implementation and may not be reliable - No standard way to handle termination events ### Key AWS Concepts: - **Lifecycle Hooks**: Allow you to perform custom actions when instances launch or terminate - **Notification Target**: Lifecycle hooks can send notifications to SQS queues or SNS topics, which can trigger Lambda functions to run scripts - **Heartbeat Timeout**: You can configure how long the instance stays in the wait state ### Implementation Approach: 1. Create lifecycle hooks for `EC2_INSTANCE_LAUNCHING` and `EC2_INSTANCE_TERMINATING` 2. Configure notification target (SNS or SQS) 3. Create Lambda function that processes the notifications and runs the audit script 4. The Lambda function can use SSM Run Command or directly execute scripts on instances This solution provides the most efficient, reliable, and AWS-native approach to meet the requirements.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
A company recently deployed a new auditing system to centralize information about operating system versions, patching, and installed software for Amazon EC2 instances. A solutions architect must ensure all instances provisioned through EC2 Auto Scaling groups successfully send reports to the auditing system as soon as they are launched and terminated.
Which solution achieves these goals MOST efficiently?
A
Use a scheduled AWS Lambda function and run a script remotely on all EC2 instances to send data to the audit system.
B
Use EC2 Auto Scaling lifecycle hooks to run a custom script to send data to the audit system when instances are launched and terminated.
C
Use an EC2 Auto Scaling launch configuration to run a custom script through user data to send data to the audit system when instances are launched and terminated.
D
Run a custom script on the instance operating system to send data to the audit system. Configure the script to be invoked by the EC2 Auto Scaling group when the instance starts and is terminated.