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

Commit c2b1c9b

Browse files
qrilkaketzacoatl
authored andcommitted
Added lifecycle hooks to asg removing asg-lifecycle
1 parent 7718719 commit c2b1c9b

File tree

10 files changed

+99
-207
lines changed

10 files changed

+99
-207
lines changed

examples/asg-lifecycle-hooks/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Example to test 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.
6+
37
## Environment creation and deployment
48

59
To use this example set up AWS credentials and then run the commands in the
@@ -35,10 +39,16 @@ running. You can check the status of the service using
3539
systemctl status lifecycled.service
3640
```
3741

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+
3848

3949
## Test the Notification
4050

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

4353
To change the desired capacity using the console
4454

@@ -54,8 +64,7 @@ To change the desired capacity using the console
5464

5565
Choose Save.
5666

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.
58-
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"
5968

6069
## Destruction
6170

@@ -67,5 +76,5 @@ make clean
6776
```
6877

6978
## Notes
70-
- This example was last tested with `Terraform v0.11.11`
79+
- This example was last tested with `Terraform v0.12.4`
7180
- This example assumes AWS credentials setup with access to the **us-east-2** region.

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ write_files:
3030
content: |
3131
#! /usr/bin/bash
3232
33-
set -euo pipefail
33+
set -euo pipefail
3434
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"
35+
echo "hello from the handler, received $${@-nothing}"
36+
curl http://localhost:3000
37+
echo
38+
sleep 10
39+
echo "goodbye from the handler"
4140
runcmd:
4241
- |
4342
wget https://github.com/buildkite/lifecycled/releases/download/v3.0.2/lifecycled-linux-amd64
@@ -51,4 +50,4 @@ runcmd:
5150
wget https://www.busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
5251
chmod +x busybox-x86_64
5352
nohup ./busybox-x86_64 httpd -f -p 3000 &
54-
wget http://18.222.32.132:3000
53+
curl http://localhost:3000

examples/asg-lifecycle-hooks/main.tf

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ data "template_file" "main" {
2020

2121
vars = {
2222
region = data.aws_region.current.name
23-
stack_name = "${var.lifecycle_name_prefix}-asg"
23+
stack_name = "${var.name_prefix}-asg"
2424
lifecycle_topic = aws_sns_topic.main.arn
2525
elb_name = aws_elb.web.name
2626
}
2727
}
2828

2929
module "vpc" {
3030
source = "../../modules/vpc-scenario-1"
31-
name_prefix = var.name
31+
name_prefix = var.name_prefix
3232
region = var.region
3333
cidr = var.vpc_cidr
3434
azs = local.azs
@@ -67,15 +67,15 @@ data "aws_ami" "linux2" {
6767
}
6868

6969
resource "aws_key_pair" "main" {
70-
key_name = var.name
70+
key_name = var.name_prefix
7171
public_key = file(var.ssh_pubkey)
7272
}
7373

7474
# Security group for the elastic load balancer, web instance, only accessible from ELB
7575
module "elb-sg" {
7676
source = "../../modules/security-group-base"
77-
description = "Allow public access to ELB in ${var.name}"
78-
name = "${var.name}-elb"
77+
description = "Allow public access to ELB in ${var.name_prefix}"
78+
name = "${var.name_prefix}-elb"
7979
vpc_id = module.vpc.vpc_id
8080
}
8181

@@ -111,7 +111,7 @@ module "web-http-elb-sg-rule" {
111111

112112
# Load Balancer
113113
resource "aws_elb" "web" {
114-
name = "${var.name}-elb"
114+
name = "${var.name_prefix}-elb"
115115

116116
health_check {
117117
healthy_threshold = 2
@@ -144,28 +144,27 @@ resource "aws_sns_topic" "main" {
144144
name = "${var.name_prefix}-lifecycle"
145145
}
146146

147-
module "asg-lifecycle" {
148-
source = "../../modules/asg-lifecycle"
149-
name_prefix = var.lifecycle_name_prefix
150-
azs = local.azs
151-
elb_names = [aws_elb.web.name]
152-
subnet_ids = module.vpc.public_subnet_ids
153-
instance_count = "2"
154-
instance_ami = data.aws_ami.linux2.id
155-
instance_type = "t2.nano"
156-
instance_key = aws_key_pair.main.key_name
157-
elb_sg_id = module.elb-sg.id
158-
asg_template_file = data.template_file.main.rendered
159-
sns_topic_arn = aws_sns_topic.main.arn
160-
vpc_id = module.vpc.vpc_id
161-
elb_arn = aws_elb.web.arn
162-
aws_role_arn = aws_iam_role.lifecycle_hook.arn
163-
aws_instance_ec2_name = aws_iam_instance_profile.ec2.name
164-
aws_sg_id = aws_security_group.main.id
147+
module "asg" {
148+
source = "../../modules/asg"
149+
name_prefix = var.name_prefix
150+
azs = local.azs
151+
elb_names = [aws_elb.web.name]
152+
subnet_ids = module.vpc.public_subnet_ids
153+
min_nodes = 1
154+
max_nodes = 4
155+
ami = data.aws_ami.linux2.id
156+
instance_type = "t2.nano"
157+
key_name = aws_key_pair.main.key_name
158+
user_data = data.template_file.main.rendered
159+
enable_terminating_hook = true
160+
lifecycle_sns_topic_arn = aws_sns_topic.main.arn
161+
aws_role_arn = aws_iam_role.lifecycle_hook.arn
162+
iam_profile = aws_iam_instance_profile.ec2.name
163+
security_group_ids = [module.elb-sg.id, aws_security_group.main.id]
165164
}
166165

167166
resource "aws_security_group" "main" {
168-
name = "${var.lifecycle_name_prefix}-sg"
167+
name = "${var.name_prefix}-sg"
169168
description = "Allow access to lifecycled instances"
170169
vpc_id = module.vpc.vpc_id
171170

@@ -249,12 +248,12 @@ data "aws_iam_policy_document" "permissions" {
249248
}
250249

251250
resource "aws_iam_instance_profile" "ec2" {
252-
name = "${var.lifecycle_name_prefix}-ec2-instance-profile"
251+
name = "${var.name_prefix}-ec2-instance-profile"
253252
role = aws_iam_role.ec2.name
254253
}
255254

256255
resource "aws_iam_role" "ec2" {
257-
name = "${var.lifecycle_name_prefix}-ec2-role"
256+
name = "${var.name_prefix}-ec2-role"
258257
assume_role_policy = data.aws_iam_policy_document.ec2_assume.json
259258
}
260259

@@ -278,12 +277,12 @@ data "aws_iam_policy_document" "ec2_assume" {
278277

279278
# Execution role and policies for the lifecycle hook
280279
resource "aws_iam_role" "lifecycle_hook" {
281-
name = "${var.lifecycle_name_prefix}-lifecycle-role"
280+
name = "${var.name_prefix}-lifecycle-role"
282281
assume_role_policy = data.aws_iam_policy_document.asg_assume.json
283282
}
284283

285284
resource "aws_iam_role_policy" "lifecycle_hook" {
286-
name = "${var.lifecycle_name_prefix}-lifecycle-asg-permissions"
285+
name = "${var.name_prefix}-lifecycle-asg-permissions"
287286
role = aws_iam_role.lifecycle_hook.id
288287
policy = data.aws_iam_policy_document.asg_permissions.json
289288
}

examples/asg-lifecycle-hooks/variables.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ variable "extra_tags" {
33
default = {}
44
}
55

6-
variable "lifecycle_name_prefix" {
7-
description = "Prefix used for resource names."
8-
default = "lifecycled-eg"
9-
}
10-
116
variable "name_prefix" {
127
description = "Prefix used for resource names."
138
default = "asg-lc"
149
}
1510

16-
variable "name" {
17-
description = "name of the project, use as prefix to names of resources created"
18-
default = "test-lifecycle-project"
19-
}
20-
2111
variable "region" {
2212
description = "Region where the project will be deployed"
2313
default = "us-east-2"

modules/asg-lifecycle/main.tf

Lines changed: 0 additions & 66 deletions
This file was deleted.

modules/asg-lifecycle/variables.tf

Lines changed: 0 additions & 86 deletions
This file was deleted.

modules/asg-lifecycle/versions.tf

Lines changed: 0 additions & 4 deletions
This file was deleted.

modules/asg/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ The module supports:
1111
* the health checks are not yet parametized, (easy to change)
1212
* the Launch Configuration supports an arbitrary list of security groups
1313
* `lifecycle` and `create_before_destroy` are used to ensure updates are graceful
14+
* lifecycle hooks which get enabled using `enable_launching_hook` and/or
15+
`enable_terminating_hook`, don't forget to set proper `lifecycle_sns_topic_arn`
16+
and `aws_role_arn` for setting up SNS
1417
* public IPs may be enabled/disabled
1518
* supports appending `extra_tags`
1619
* all important details (instance type, ami, key, user data, iam profile) are

0 commit comments

Comments
 (0)