- Newest
- Most votes
- Most comments
It's best to separate your own data from the OS and instance configuration. For OS & config you can bootstrap new ASG instances via UserData / cfn-init, or ideally as @takakuni said creating a golden (rich) AMI. Your own data could be on a 2nd EBS volume you detach from old instances and attach to new in the same AZ, or the options @takakuni mentioned.
Unfortunately, I think it will be difficult.
I think it is better to make golden AMI for the root /os volume as much as possible, and to save the data to Fsx for Windows or S3 if you want to share it after the instance is stopped.
The process for a launch hook is as follows; termination hooks are similar. If a hook is active for instance launch:
- instance enters Pending: Wait state.
- AS (AutoScaling) sends info & token to the hook's notification target.
- AS waits until you tell it to continue or timeout ends (1 hour default).
- You can now perform custom setup etc. on the instance.
-
- If you need more time you can make the default wait time longer or record a heartbeat.
- AS puts instance in Pending: Proceed then InService states.
-
- That assumes hook returned "CONTINUE". If it returned "ABANDON" then the instance will instead be terminated.
If you want to run something on an instance when its termination lifecycle hook is triggered you have to listen on the instance somehow, e.g. poll an SQS queue or wait for SNS to invoke an http endpoint.
However on launch, if you know that a launch hook is in place, you can more simply:
- make sure the instance is configured with ASG name & hook name
- run some setup stuff
- query instance ID from metadata
- Call complete-lifecycle-action passing the above data in; if you have the instance ID you don't need the hook event token.
Hi @skinsman, Thank you for your detailed response. However, I am looking for implementation details, preferably using Terraform.
Is it possible to call a custom script (say, Shell or PowerShell) from a Termination Lifecycle Hook which will tag the volumes and make them available? Then, can I have another Launching Lifecycle Hook which will allow me to execute a custom script (shell / powershell) to pick up these volumes and attach them to the new instance? Is this possible? If yes, please provide some detailed steps (with links). If not, what are the alternatives? Please help
Relevant content
- asked 2 years ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated a year ago
- I can't use Amazon EC2 Auto Scaling to launch EC2 instances with encrypted AMIs or encrypted volumesAWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
Thank you @takakuni and @skinsman for your prompt response. However, my golden AMI is already there generated by Packer. Now, I need to attach the existing EBS volumes for other drives (say D, E, F etc) from the terminating /unhealthy instance to the new instance which are part of the ASG. I need to know how to identify these EBS volumes and how to attach them to the new instance. Any links / samples with detailed steps will be really helpful. I was thinking of using Termination LifeCycle Hooks, but I don't know how to call custom shell/powershell scripts from Terminating hook. I don't want to use a Lambda.