Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 1a13938

Browse files
committed
Update
1 parent cba9f2a commit 1a13938

File tree

5 files changed

+94
-329
lines changed

5 files changed

+94
-329
lines changed

examples/asg-lifecycle-hooks/README.md

+69-43
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,100 @@
1-
# Example to test basic ASG integration with lifecycle hooks
1+
# Example of basic ASG integration with lifecycle hooks
22

3-
This example uses [lifecycled](https://github.com/buildkite/lifecycled) to process
4-
lifecycle events. As of version 3.0.2 `lifecycled` supports only instance termination
5-
events and reacts to a termination event for a node it is running on.
3+
We use the [lifecycled](https://github.com/buildkite/lifecycled
4+
"lifecycled") tool to run a script whenever an instance in an ASG gets
5+
an scale-in event.
6+
7+
## lifecycled
8+
9+
lifecyled is an executable provided by buildkite. Note that the
10+
executable will be run on the ASG's instance. Note that it
11+
automatically creates an [SQS
12+
queue](https://github.com/buildkite/lifecycled/commit/fa9f36f25a6ca6ceb3dae1814bacc26b3643392d
13+
"SQS queue") and subscribes to the SNS topic you have created. So,
14+
using this approaches makes you depend on two AWS services.
615

716
## Environment creation and deployment
817

918
To use this example set up AWS credentials and then run the commands in the
1019
following order:
1120

1221
```
13-
make ssh-key
14-
make init
15-
make plan-vpc
16-
make apply
17-
make plan-subnets
18-
make apply
19-
make plan-gateway
20-
make apply
21-
make plan
22-
make apply
22+
$ make ssh-key
23+
$ make init
24+
$ make plan-vpc
25+
$ make apply
26+
$ make plan-subnets
27+
$ make apply
28+
$ make plan-gateway
29+
$ make apply
30+
$ make plan
31+
$ make apply
2332
```
2433

2534
## Testing
2635

27-
Get the public IP address of the newly created ec2 web instance with the AWS console.
36+
SSH into the ASG instance with the command:
2837

29-
SSH into the machine with the command:
30-
31-
```
32-
ssh -i id_rsa ec2-user@<ec2-ip-address>
38+
``` shellsession
39+
$ ssh -i id_rsa ec2-user@<ec2-ip-address>
3340
```
3441

3542
You can see in the machine that `lifecycled` daemon would be
3643
running. You can check the status of the service using
3744

45+
``` shellsession
46+
$ systemctl status lifecycled.service
47+
[ec2-user@ip-10-23-11-146 ~]$ systemctl status lifecycled.service
48+
● lifecycled.service - Autoscale Lifecycle Daemon
49+
Loaded: loaded (/etc/systemd/system/lifecycled.service; enabled; vendor preset: disabled)
50+
Active: active (running) since Fri 2020-06-26 12:39:35 UTC; 3min 19s ago
51+
Main PID: 3412 (lifecycled)
52+
CGroup: /system.slice/lifecycled.service
53+
└─3412 /usr/local/bin/lifecycled --no-spot --sns-topic=arn:aws:sns:ap-south-1:xxxx:sibi-issue163-lifecycle --handler=/usr/local/scripts/lifecycle-handler.sh --json
54+
55+
Jun 26 12:39:35 ip-10-23-11-146.ap-south-1.compute.internal systemd[1]: Started Autoscale Lifecycle Daemon.
56+
Jun 26 12:39:35 ip-10-23-11-146.ap-south-1.compute.internal systemd[1]: Starting Autoscale Lifecycle Daemon...
57+
Jun 26 12:39:35 ip-10-23-11-146.ap-south-1.compute.internal lifecycled[3412]: {"level":"info","msg":"Looking up instance id from metadata service","time":"2020-06-26T12:39:35Z"}
58+
Jun 26 12:39:35 ip-10-23-11-146.ap-south-1.compute.internal lifecycled[3412]: {"instanceId":"i-xxx","level":"info","listener":"autoscaling","msg":"Starting listener","time":"2020-06-26T12:39:35Z"}
59+
Jun 26 12:39:35 ip-10-23-11-146.ap-south-1.compute.internal lifecycled[3412]: {"instanceId":"i-xxx","level":"info","msg":"Waiting for termination notices","time":"2020-06-26T12:39:35Z"}
3860
```
39-
systemctl status lifecycled.service
40-
```
41-
42-
Output from a handler could be seen in the service log e.g. by using
43-
44-
```
45-
journalctl -f -u lifecycled.service
46-
```
47-
48-
49-
## Test the Notification
5061

51-
To generate a notification for a termination event, update the Auto Scaling group by decreasing the desired capacity of the Auto Scaling group by 1. You receive a notification within a few minutes after instance termination.
62+
## Note about lifecycle-handler.sh
5263

53-
To change the desired capacity using the console
64+
For testing that the handler is working properly, you have to observe
65+
that you are able to observe the side effect from the script.
5466

55-
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
67+
One easy way to validate is by spawning a new EC2 instance with a
68+
sample website deployed in it (You could use busybox for it) and then
69+
doing a SSH and watching to see if you are able to observe any logs on
70+
it whenever it's being hit with an HTTP request. Example:
5671

57-
On the navigation pane, under Auto Scaling, choose Auto Scaling Groups.
72+
``` shellsession
73+
ubuntu@ip-10-23-11-56:~/test$ sudo busybox httpd -f -v -p 80 -h .
74+
[::ffff:49.207.192.240]:38924: response:200
75+
```
5876

59-
Select your Auto Scaling group.
77+
You can modify your [script](./cloud-config.yml) to have curl hit it:
6078

61-
On the Details tab, choose Edit.
79+
``` shellsession
80+
$ curl http://x.x.x.x
81+
```
6282

63-
For Desired, decrease the current value by 1.
83+
## Test the Notification
6484

65-
Choose Save.
85+
To generate a notification for a launch event, update the Auto Scaling
86+
group by decreasing the desired capacity of the Auto Scaling group
87+
by 1. That will make the lifecycle handler to get triggered. These are
88+
the steps to decreased the desired capacity using AWS console:
6689

67-
After a few minutes, you'll see that the lifecycle-handler.sh script will be executed and it's side effect operation will be performed: in the log of lifecycled.service you'll see a line with something like "hello from the handler, received autoscaling:EC2_INSTANCE_TERMINATING i-01234567890123456"
90+
* Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
91+
* On the navigation pane, under Auto Scaling, choose Auto Scaling Groups.
92+
* Select your Auto Scaling group.
93+
* On the Details tab, choose Edit.
94+
* For Desired, decrease the current value by 1.
95+
* Choose Save.
96+
* After a few minutes, you'll see that the lifecycle-handler.sh script
97+
will be executed and it's side effect operation will be performed.
6898

6999
## Destruction
70100

@@ -74,7 +104,3 @@ To destroy the test environment run the following commands:
74104
make destroy
75105
make clean
76106
```
77-
78-
## Notes
79-
- This example was last tested with `Terraform v0.12.4`
80-
- This example assumes AWS credentials setup with access to the **us-east-2** region.

examples/asg-lifecycle-hooks/cloud-config.yml

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#cloud-config
22
write_files:
3-
- path: "/index.html"
4-
permissions: "0644"
5-
owner: "root"
6-
content: "hello world"
73
- path: "/etc/systemd/system/lifecycled.service"
84
permissions: "0644"
95
owner: "root"
@@ -32,10 +28,10 @@ write_files:
3228
3329
set -euo pipefail
3430
35-
echo "hello from the handler, received $${@-nothing}"
36-
curl http://localhost:3000
37-
echo
38-
sleep 10
31+
echo "hello from the handler"
32+
# Have a side effect which you can observe
33+
# Example side effect: curl http://x.x.x.x
34+
sleep 120
3935
echo "goodbye from the handler"
4036
runcmd:
4137
- |
@@ -46,8 +42,3 @@ runcmd:
4642
echo "lifecycled installed"
4743
- |
4844
systemctl enable lifecycled.service --now
49-
- |
50-
wget https://www.busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
51-
chmod +x busybox-x86_64
52-
nohup ./busybox-x86_64 httpd -f -p 3000 &
53-
curl http://localhost:3000

0 commit comments

Comments
 (0)