Skip to content

Commit 8370372

Browse files
committed
Update code examples
1 parent d6e8e02 commit 8370372

File tree

18 files changed

+353
-530
lines changed

18 files changed

+353
-530
lines changed

code/ruby/07-testing-terraform-code/web-server-full-di.rb

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,20 @@ def initialize(web_service)
2222
def handle(path)
2323
case path
2424
when "/"
25-
self.hello
25+
[200, 'text/plain', 'Hello, World']
2626
when "/api"
27-
self.api
27+
[201, 'application/json', '{"foo":"bar"}']
2828
when "/web-service"
29-
self.web_service
30-
else
31-
self.not_found
32-
end
33-
end
34-
35-
def hello
36-
[200, 'text/plain', 'Hello, World']
37-
end
38-
39-
def api
40-
[201, 'application/json', '{"foo":"bar"}']
41-
end
42-
29+
# New endpoint that calls a web service
4330
=begin
44-
45-
def web_service
46-
uri = URI("http://www.example.org")
47-
response = Net::HTTP.get_response(uri)
48-
[response.code.to_i, response['Content-Type'], response.body]
49-
end
31+
uri = URI("http://www.example.org")
32+
response = Net::HTTP.get_response(uri)
33+
[response.code.to_i, response['Content-Type'], response.body]
5034
=end
51-
52-
def web_service
53-
@web_service.proxy
54-
end
55-
56-
def not_found
57-
[404, 'text/plain', 'Not Found']
35+
@web_service.proxy
36+
else
37+
[404, 'text/plain', 'Not Found']
38+
end
5839
end
5940
end
6041

code/ruby/07-testing-terraform-code/web-server-full.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,13 @@ class Handlers
1515
def handle(path)
1616
case path
1717
when "/"
18-
self.hello
18+
[200, 'text/plain', 'Hello, World']
1919
when "/api"
20-
self.api
20+
[201, 'application/json', '{"foo":"bar"}']
2121
else
22-
self.not_found
22+
[404, 'text/plain', 'Not Found']
2323
end
2424
end
25-
26-
def hello
27-
[200, 'text/plain', 'Hello, World']
28-
end
29-
30-
def api
31-
[201, 'application/json', '{"foo":"bar"}']
32-
end
33-
34-
def not_found
35-
[404, 'text/plain', 'Not Found']
36-
end
3725
end
3826

3927
# This will only run if this script was called directly from the CLI, but

code/ruby/08-terraform-team/web-server.rb

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,15 @@ def initialize(web_service)
2222
def handle(path)
2323
case path
2424
when "/"
25-
self.hello
25+
[200, 'text/plain', 'Hello, World']
2626
when "/api"
27-
self.api
27+
[201, 'application/json', '{"foo":"bar"}']
2828
when "/web-service"
29-
self.web_service
29+
@web_service.proxy
3030
else
31-
self.not_found
31+
[404, 'text/plain', 'Not Found']
3232
end
3333
end
34-
35-
def hello
36-
[200, 'text/plain', 'Hello, World']
37-
end
38-
39-
def api
40-
[201, 'application/json', '{"foo":"bar"}']
41-
end
42-
43-
def web_service
44-
@web_service.proxy
45-
end
46-
47-
def not_found
48-
[404, 'text/plain', 'Not Found']
49-
end
5034
end
5135

5236
class WebService

code/terraform/02-intro-to-terraform-syntax/webserver-cluster/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ resource "aws_launch_configuration" "example" {
1919
echo "Hello, World" > index.html
2020
nohup busybox httpd -f -p ${var.server_port} &
2121
EOF
22+
23+
# Required when using a launch configuration with an auto scaling group.
24+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
25+
lifecycle {
26+
create_before_destroy = true
27+
}
2228
}
2329

2430
resource "aws_autoscaling_group" "example" {

code/terraform/03-terraform-state/file-layout-example/stage/services/webserver-cluster/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ resource "aws_launch_configuration" "example" {
1414
instance_type = "t2.micro"
1515
security_groups = [aws_security_group.instance.id]
1616
user_data = data.template_file.user_data.rendered
17+
18+
# Required when using a launch configuration with an auto scaling group.
19+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
20+
lifecycle {
21+
create_before_destroy = true
22+
}
1723
}
1824

1925
data "template_file" "user_data" {

code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ resource "aws_launch_configuration" "example" {
77
instance_type = var.instance_type
88
security_groups = [aws_security_group.instance.id]
99
user_data = data.template_file.user_data.rendered
10+
11+
# Required when using a launch configuration with an auto scaling group.
12+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
13+
lifecycle {
14+
create_before_destroy = true
15+
}
1016
}
1117

1218
data "template_file" "user_data" {

code/terraform/05-tips-and-tricks/loops-and-if-statements/modules/services/webserver-cluster/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ resource "aws_launch_configuration" "example" {
1212
? data.template_file.user_data[0].rendered
1313
: data.template_file.user_data_new[0].rendered
1414
)
15+
16+
# Required when using a launch configuration with an auto scaling group.
17+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
18+
lifecycle {
19+
create_before_destroy = true
20+
}
1521
}
1622

1723
data "template_file" "user_data" {

code/terraform/05-tips-and-tricks/zero-downtime-deployment/modules/services/webserver-cluster/main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ resource "aws_launch_configuration" "example" {
88
security_groups = [aws_security_group.instance.id]
99

1010
user_data = data.template_file.user_data.rendered
11+
12+
# Required when using a launch configuration with an auto scaling group.
13+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
14+
lifecycle {
15+
create_before_destroy = true
16+
}
1117
}
1218

1319
data "template_file" "user_data" {
@@ -22,8 +28,8 @@ data "template_file" "user_data" {
2228
}
2329

2430
resource "aws_autoscaling_group" "example" {
25-
# Explicitly depend on the launch configuration's name so each time it's replaced,
26-
# this ASG is also replaced
31+
# Explicitly depend on the launch configuration's name so each time it's
32+
# replaced, this ASG is also replaced
2733
name = "${var.cluster_name}-${aws_launch_configuration.example.name}"
2834

2935
launch_configuration = aws_launch_configuration.example.name

code/terraform/06-production-grade-infrastructure/small-modules/modules/cluster/asg-rolling-deploy/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ resource "aws_launch_configuration" "example" {
77
instance_type = var.instance_type
88
security_groups = [aws_security_group.instance.id]
99
user_data = var.user_data
10+
11+
# Required when using a launch configuration with an auto scaling group.
12+
# https://www.terraform.io/docs/providers/aws/r/launch_configuration.html
13+
lifecycle {
14+
create_before_destroy = true
15+
}
1016
}
1117

1218
resource "aws_autoscaling_group" "example" {

code/terraform/07-testing-terraform-code/examples/hello-world-app/standalone/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
variable "mysql_config" {
77
description = "The config for the MySQL DB"
88

9-
type = object({
9+
type = object({
1010
address = string
1111
port = number
1212
})

0 commit comments

Comments
 (0)