Understand Lifecycle Hooks:轉(zhuǎn)載

https://linuxacademy.com/blog/amazon-web-services-2/understand-lifecycle-hooks/

對理解生命周期掛鉤比較好的文章,比AWS官網(wǎng)寫得透徹感覺瓢省。

When it comes to using Auto Scaling for your application, a common question is "How can we configure an instance with our application and its dependencies, and make it ready to serve traffic?" To answer this question, we can turn to lifecycle hooks. Lifecycle hooks are powerful, because they let us pause the creation (or termination) of an instance so that we can sneak in and perform custom actions. We can, for example:

1.Download required files

2..Configure the instance

3.Perform any other step required to make the instance ready

We can also perform an action before the instance terminates to download critical data or log files. Once those actions are complete, we resume the process of launching (or terminating) the instance, and we make it available for our load balancer to send it traffic. While we could use this with a manual approach, we can (and most likely want to) make this an automated process. But how does that work? Let's take a look. For this article, let's focus our attention on using lifecycle hooks on instance launch instead of on instance termination.

How do Lifecycle Hooks Work?

Before we answer the question fully, let's take a look at how lifecycle hooks work. To create a lifecycle hook, we call the CLI command:

aws autoscaling put-lifecycle-hook --lifecycle-hook-name hook-name --auto-scaling-group-name my-group-name --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING

Notice the--lifecycle-transitionparameter, which can be changed toEC2_INSTANCE_TERMINATING. This parameter tells Auto Scaling that any time it launches an instance, it needs to put the instance in awaitstate before putting it in aninServicestate. Here's what it looks like:

Auto Scaling receives a scale out event telling it to add an instance or more. The instance is in apendingstate, and because we have added a lifecycle hook, the instance goes into apending:waitstate. This is when we can run our custom actions. Once the custom actions are complete and we make a call to complete the lifecycle action, the instance changes to apending:proceedstate before finally going into theinServicestate.

What Custom Actions Can We Run?

Depending on our needs, we have a few different options for actions that we can run.

1.Using CloudWatch events to invoke a Lambda function

2.Using a notification target like Amazon SNS or SQS to send notifications

3.Running a script on the instance

With option number one, we can call a Lambda function, give it some information, and get a result back. This is possible because Auto Scaling submits an event for a lifecycle action (like instance launching or terminating) toCloudWatch events. That event can, in turn, invoke the Lambda function and pass in information about the instance that is launching or terminating, as well as a special token that we can use to control the lifecycle action. With option number two, we can send messages with information about the instance and again have a token to control the lifecycle action. The third option is also very powerful because it allows us to run commands on the instance itself withuser data. This user data can download updates, application files and configuration files, run commands on the instance to get it ready to serve traffic, and more.

Completing Actions

So now we know what lifecycle hooks are and what options we have when performing custom actions, but how do we complete the action and resume the instance launch? Because at the end of the day, that instance has a role to do and we need it to be ready as soon as possible. I already mentioned making a call to resume the instance launch process. That call, from the CLI, looks like this:

aws autoscaling complete-lifecycle-action --lifecycle-hook-name my-lifecycle-hook --auto-scaling-group-name my-auto-scaling-group --lifecycle-action-result CONTINUE --lifecycle-action-token bcd2f1b8-9a78-44d3-8a7a-4dd07d7cf635

As mentioned a few paragraphs above, whatever option we choose for our custom actions, we get a special token that is auniversally unique identifier(UUID). We can use it here to complete the action. When completing the action, we don't necessarily need to go forward. In the command example, I use the--lifecycle-action-resultoption to specifyCONTINUE, but I could also specifyABANDONif something went wrong and we don't want the instance to proceed. UsingABANDONtells Auto Scaling that it can terminate the instance, and it stops all actions including any other remaining lifecycle hooks. But this is not the only way to complete actions. We also have a timeout. By default, this timeout is set to 60 minutes. If our actions run, and if we don't complete the action before the timeout expires, then the timeout will automatically conclude actions. If you need more time to complete an action, but the timeout is going to expire, you can send aheartbeatto renew the timeout.

Conclusion

We've now learned about lifecycle hooks and what we can do with them, as well as what options we have when using them. But before I leave you to use these hooks, do let me warn you about one more potential snag. When an instance is getting ready to serve traffic, Auto Scaling uses a cooldown period to make sure that it doesn't trigger another scale out event while another instance is already being provisioned (or being taken down). Because otherwise we would end up with too many or too few instances. When we implement lifecycle hooks, depending on how much work we perform, we could be adding a non-trivial amount of time to how long it takes for an instance to be ready to serve traffic. If that's the case, we may need to increase the cool down period or else Auto Scaling thinks it needs to launch more instances. Do keep this in mind. I hope you've enjoyed this write up, and I hope you've learned something useful from reading it. If you're interested in learning more, check out theAWS DevOps Engineer Professional level coursewhere we cover this and more advanced deployment topics.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市吊说,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌掂僵,老刑警劉巖到千,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異湿镀,居然都是意外死亡,警方通過查閱死者的電腦和手機伐憾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進店門勉痴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人树肃,你說我怎么就攤上這事蒸矛。” “怎么了胸嘴?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵雏掠,是天一觀的道長。 經(jīng)常有香客問我劣像,道長乡话,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任驾讲,我火速辦了婚禮蚊伞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吮铭。我一直安慰自己时迫,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布谓晌。 她就那樣靜靜地躺著掠拳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪纸肉。 梳的紋絲不亂的頭發(fā)上溺欧,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天,我揣著相機與錄音柏肪,去河邊找鬼姐刁。 笑死,一個胖子當(dāng)著我的面吹牛烦味,可吹牛的內(nèi)容都是我干的聂使。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼谬俄,長吁一口氣:“原來是場噩夢啊……” “哼柏靶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起溃论,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤屎蜓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后钥勋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炬转,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年算灸,在試婚紗的時候發(fā)現(xiàn)自己被綠了返吻。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡乎婿,死狀恐怖测僵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情谢翎,我是刑警寧澤捍靠,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站森逮,受9級特大地震影響榨婆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜褒侧,卻給世界環(huán)境...
    茶點故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一良风、第九天 我趴在偏房一處隱蔽的房頂上張望谊迄。 院中可真熱鬧,春花似錦烟央、人聲如沸统诺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粮呢。三九已至,卻和暖如春钞艇,著一層夾襖步出監(jiān)牢的瞬間啄寡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工哩照, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留挺物,地道東北人。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓飘弧,卻偏偏與公主長得像姻乓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子眯牧,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,446評論 2 348

推薦閱讀更多精彩內(nèi)容