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

Commit 9111ebb

Browse files
committed
Simplify example and remove ALB
1 parent ced16cc commit 9111ebb

File tree

14 files changed

+92
-645
lines changed

14 files changed

+92
-645
lines changed

examples/asg-lifecycle-hooks/README.md

+70-35
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,100 @@
1-
# Example to test basic ASG integration with lifecycle hooks
1+
# Example of basic ASG integration with lifecycle hooks
2+
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.
215

316
## Environment creation and deployment
417

518
To use this example set up AWS credentials and then run the commands in the
619
following order:
720

821
```
9-
make ssh-key
10-
make init
11-
make plan-vpc
12-
make apply
13-
make plan-subnets
14-
make apply
15-
make plan-gateway
16-
make apply
17-
make plan
18-
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
1932
```
2033

2134
## Testing
2235

23-
Get the public IP address of the newly created ec2 web instance with the AWS console.
24-
25-
SSH into the machine with the command:
36+
SSH into the ASG instance with the command:
2637

27-
```
28-
ssh -i id_rsa ec2-user@<ec2-ip-address>
38+
``` shellsession
39+
$ ssh -i id_rsa ec2-user@<ec2-ip-address>
2940
```
3041

3142
You can see in the machine that `lifecycled` daemon would be
3243
running. You can check the status of the service using
3344

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"}
3460
```
35-
systemctl status lifecycled.service
36-
```
37-
3861

39-
## Test the Notification
40-
41-
To generate a notification for a launch event, update the Auto Scaling group by increasing the desired capacity of the Auto Scaling group by 1. You receive a notification within a few minutes after instance launch.
62+
## Note about lifecycle-handler.sh
4263

43-
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.
4466

45-
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:
4671

47-
On the navigation pane, under Auto Scaling, choose Auto Scaling Groups.
48-
49-
Select your Auto Scaling group.
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+
```
5076

51-
On the Details tab, choose Edit.
77+
You can modify your [script](./cloud-config.yml) to have curl hit it:
5278

53-
For Desired, decrease the current value by 1.
79+
``` shellsession
80+
$ curl http://x.x.x.x
81+
```
5482

55-
Choose Save.
83+
## Test the Notification
5684

57-
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.
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:
5889

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.
5998

6099
## Destruction
61100

@@ -65,7 +104,3 @@ To destroy the test environment run the following commands:
65104
make destroy
66105
make clean
67106
```
68-
69-
## Notes
70-
- This example was last tested with `Terraform v0.11.11`
71-
- This example assumes AWS credentials setup with access to the **us-east-2** region.

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

+6-16
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"
@@ -30,14 +26,13 @@ write_files:
3026
content: |
3127
#! /usr/bin/bash
3228
33-
set -euo pipefail
29+
set -euo pipefail
3430
35-
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`"
36-
aws elb deregister-instances-from-load-balancer --load-balancer-name ${elb_name} --instances $EC2_INSTANCE_ID --region ${region}
37-
echo "hello from the handler"
38-
wget http://18.222.32.132:3000
39-
sleep 120
40-
echo "goodbye from the handler"
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
35+
echo "goodbye from the handler"
4136
runcmd:
4237
- |
4338
wget https://github.com/buildkite/lifecycled/releases/download/v3.0.2/lifecycled-linux-amd64
@@ -47,8 +42,3 @@ runcmd:
4742
echo "lifecycled installed"
4843
- |
4944
systemctl enable lifecycled.service --now
50-
- |
51-
wget https://www.busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
52-
chmod +x busybox-x86_64
53-
nohup ./busybox-x86_64 httpd -f -p 3000 &
54-
wget http://18.222.32.132:3000

examples/asg-simple-lifecycle-hooks/data.tf renamed to examples/asg-lifecycle-hooks/data.tf

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ data "aws_availability_zones" "available" {
3030
data "aws_region" "current" {
3131
}
3232

33+
data "aws_caller_identity" "current" {}
34+
3335
# Cloud init script for the autoscaling group
3436
data "template_file" "main" {
3537
template = file("${path.module}/cloud-config.yml")
@@ -54,6 +56,16 @@ data "aws_iam_policy_document" "permissions" {
5456
aws_sns_topic.main.arn,
5557
]
5658
}
59+
60+
statement {
61+
effect = "Allow"
62+
63+
actions = [
64+
"sqs:*",
65+
]
66+
67+
resources = ["arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:lifecycled-*"]
68+
}
5769

5870
statement {
5971
effect = "Allow"

0 commit comments

Comments
 (0)