Skip to content

Commit 320a996

Browse files
authored
Merge pull request #4 from druids/0.13-rewrite
Rewrite module to Terraform 0.13
2 parents c8b82fa + 748f510 commit 320a996

File tree

8 files changed

+87
-67
lines changed

8 files changed

+87
-67
lines changed

cloudwatch.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
resource "aws_cloudwatch_event_rule" "rule" {
2-
name = "${var.cloudwatch_event_rule_name}"
3-
schedule_expression = "${var.cloudwatch_event_rule_schedule_expression}"
2+
name = var.cloudwatch_event_rule_name
3+
schedule_expression = var.cloudwatch_event_rule_schedule_expression
44
}
55

66
resource "aws_cloudwatch_event_target" "refresh_lambda" {
7-
rule = "${aws_cloudwatch_event_rule.rule.id}"
8-
arn = "${aws_lambda_function.refresh.arn}"
7+
rule = aws_cloudwatch_event_rule.rule.id
8+
arn = aws_lambda_function.refresh.arn
99
}

example/main.tf

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
provider "aws" {
2-
version = "2.70.0"
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 3.0"
6+
}
7+
}
38
}
49

510
resource "aws_vpc" "vpc" {
@@ -8,7 +13,7 @@ resource "aws_vpc" "vpc" {
813

914
resource "aws_subnet" "subnet" {
1015
cidr_block = "10.0.0.0/24"
11-
vpc_id = "${aws_vpc.vpc.id}"
16+
vpc_id = aws_vpc.vpc.id
1217
}
1318

1419
resource "aws_launch_template" "tpl" {
@@ -22,36 +27,37 @@ resource "aws_autoscaling_group" "grp" {
2227
min_size = 1
2328
desired_capacity = 1
2429

25-
vpc_zone_identifier = ["${aws_subnet.subnet.id}"]
30+
vpc_zone_identifier = [aws_subnet.subnet.id]
2631

2732
mixed_instances_policy {
2833
launch_template {
2934
launch_template_specification {
30-
launch_template_name = "${aws_launch_template.tpl.name}"
35+
launch_template_name = aws_launch_template.tpl.name
3136
}
3237
}
3338
}
3439
}
3540

3641
module "refresh" {
37-
source = "../"
42+
source = "../"
43+
depends_on = [aws_launch_template.tpl, aws_autoscaling_group.grp]
3844

39-
launch_template_name = "${aws_launch_template.tpl.name}"
40-
autoscaling_group_name = "${aws_autoscaling_group.grp.name}"
45+
launch_template_name = aws_launch_template.tpl.name
46+
autoscaling_group_name = aws_autoscaling_group.grp.name
4147
}
4248

4349
output "asg_arn" {
44-
value = "${module.refresh.asg_arn}"
50+
value = module.refresh.asg_arn
4551
}
4652

4753
output "lt_arn" {
48-
value = "${module.refresh.lt_arn}"
54+
value = module.refresh.lt_arn
4955
}
5056

5157
output "lt_id" {
52-
value = "${module.refresh.lt_id}"
58+
value = module.refresh.lt_id
5359
}
5460

5561
output "lt_name" {
56-
value = "${module.refresh.lt_name}"
62+
value = module.refresh.lt_name
5763
}

iam.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ data "aws_iam_policy_document" "lambda" {
2929
actions = ["autoscaling:StartInstanceRefresh"]
3030

3131
resources = [
32-
"${data.aws_autoscaling_group.group.arn}",
32+
data.aws_autoscaling_group.group.arn,
3333
]
3434
}
3535

@@ -50,7 +50,7 @@ data "aws_iam_policy_document" "lambda" {
5050
]
5151

5252
resources = [
53-
"${data.aws_launch_template.template.arn}",
53+
data.aws_launch_template.template.arn,
5454
]
5555
}
5656

@@ -78,13 +78,13 @@ data "aws_iam_policy_document" "lambda" {
7878
}
7979

8080
resource "aws_iam_role" "lambda" {
81-
name = "${var.lambda_role_name}"
82-
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
83-
description = "${var.lambda_role_description}"
81+
name = var.lambda_role_name
82+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
83+
description = var.lambda_role_description
8484
}
8585

8686
resource "aws_iam_role_policy" "lambda" {
87-
name_prefix = "${var.lambda_role_name}"
88-
policy = "${data.aws_iam_policy_document.lambda.json}"
89-
role = "${aws_iam_role.lambda.id}"
87+
name_prefix = var.lambda_role_name
88+
policy = data.aws_iam_policy_document.lambda.json
89+
role = aws_iam_role.lambda.id
9090
}

lambda.tf

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
resource "aws_lambda_function" "refresh" {
2-
filename = "${data.archive_file.lambda.output_path}"
2+
filename = data.archive_file.lambda.output_path
33

4-
function_name = "${var.lambda_name}"
4+
function_name = var.lambda_name
55
handler = "lambda.handler"
6-
role = "${aws_iam_role.lambda.arn}"
6+
role = aws_iam_role.lambda.arn
77
runtime = "python3.8"
8-
timeout = "${var.lambda_timeout}"
9-
source_code_hash = "${data.archive_file.lambda.output_base64sha256}"
8+
timeout = var.lambda_timeout
9+
source_code_hash = data.archive_file.lambda.output_base64sha256
1010

11-
description = "${var.lambda_description}"
11+
description = var.lambda_description
1212

1313
environment {
1414
variables = {
15-
AUTO_SCALING_GROUP_NAME = "${var.autoscaling_group_name}"
16-
DESCRIBE_INSTANCE_REFRESHES_MAX_RECORDS = "${var.describe_instance_refreshes_max_records}"
17-
REFRESH_INSTANCE_WARMUP = "${var.instance_refresh_instance_warmup}"
18-
REFRESH_MIN_HEALTHY_PERCENTAGE = "${var.instance_refresh_min_healthy_percentage}"
19-
SSM_PARAMETER_NAME = "${var.ami_ssm_parameter}"
15+
AUTO_SCALING_GROUP_NAME = var.autoscaling_group_name
16+
DESCRIBE_INSTANCE_REFRESHES_MAX_RECORDS = var.describe_instance_refreshes_max_records
17+
REFRESH_INSTANCE_WARMUP = var.instance_refresh_instance_warmup
18+
REFRESH_MIN_HEALTHY_PERCENTAGE = var.instance_refresh_min_healthy_percentage
19+
SSM_PARAMETER_NAME = var.ami_ssm_parameter
2020
}
2121
}
2222
}
2323

2424
resource "aws_lambda_permission" "allow_cloudwatch_events" {
2525
statement_id = "AllowCloudWatchEventsInvokeFunction"
2626
action = "lambda:InvokeFunction"
27-
function_name = "${aws_lambda_function.refresh.function_name}"
27+
function_name = aws_lambda_function.refresh.function_name
2828
principal = "events.amazonaws.com"
29-
source_arn = "${aws_cloudwatch_event_rule.rule.arn}"
29+
source_arn = aws_cloudwatch_event_rule.rule.arn
3030
}

main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ data "aws_caller_identity" "current" {}
22
data "aws_region" "current" {}
33

44
data "null_data_source" "lambda_file" {
5-
inputs {
6-
filename = "${substr("${path.module}/functions/lambda.py", length(path.cwd) + 1, -1)}"
5+
inputs = {
6+
filename = "${path.module}/functions/lambda.py"
77
}
88
}
99

1010
data "null_data_source" "lambda_archive" {
11-
inputs {
12-
filename = "${substr("${path.module}/functions/lambda.zip", length(path.cwd) + 1, -1)}"
11+
inputs = {
12+
filename = "${path.module}/functions/lambda.zip"
1313
}
1414
}
1515

1616
data "archive_file" "lambda" {
1717
type = "zip"
18-
source_file = "${data.null_data_source.lambda_file.outputs.filename}"
19-
output_path = "${data.null_data_source.lambda_archive.outputs.filename}"
18+
source_file = data.null_data_source.lambda_file.outputs.filename
19+
output_path = data.null_data_source.lambda_archive.outputs.filename
2020
}
2121

2222
data "aws_autoscaling_group" "group" {
23-
name = "${var.autoscaling_group_name}"
23+
name = var.autoscaling_group_name
2424
}
2525

2626
data "aws_launch_template" "template" {
27-
name = "${var.launch_template_name}"
27+
name = var.launch_template_name
2828
}

outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
output "asg_arn" {
2-
value = "${data.aws_autoscaling_group.group.arn}"
2+
value = data.aws_autoscaling_group.group.arn
33
}
44

55
output "lt_arn" {
6-
value = "${data.aws_launch_template.template.arn}"
6+
value = data.aws_launch_template.template.arn
77
}
88

99
output "lt_id" {
10-
value = "${data.aws_launch_template.template.id}"
10+
value = data.aws_launch_template.template.id
1111
}
1212

1313
output "lt_name" {
14-
value = "${data.aws_launch_template.template.name}"
14+
value = data.aws_launch_template.template.name
1515
}

variables.tf

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
11
variable "ami_ssm_parameter" {
22
default = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
33
description = "Name of SSM parameter containing the current AMI"
4-
type = "string"
4+
type = string
55
}
66

77
variable "autoscaling_group_name" {
88
description = "Name of the auto scaling group to refresh"
9-
type = "string"
9+
type = string
1010
}
1111

1212
variable "cloudwatch_event_rule_name" {
1313
description = "Name of the CloudWatch Event Rule"
1414
default = "ASGRefreshInstancesEventRule"
15-
type = "string"
15+
type = string
1616
}
1717

1818
variable "cloudwatch_event_rule_schedule_expression" {
1919
description = "Schedule expression for CloudWatch Event Rule"
2020
default = "cron(0 0 * * ? *)"
21-
type = "string"
21+
type = string
2222
}
2323

2424
variable "describe_instance_refreshes_max_records" {
2525
description = "Page size for boto3 when calling autoscaling:DescribeInstanceRefreshes (max is 100)"
26-
default = "100"
27-
type = "string"
26+
default = 100
27+
type = number
2828
}
2929

3030
variable "instance_refresh_instance_warmup" {
3131
description = "Instance warmup time for instance refresh"
32-
default = "300"
33-
type = "string"
32+
default = 300
33+
type = number
3434
}
3535

3636
variable "instance_refresh_min_healthy_percentage" {
3737
description = "Minimum healthy percentage for instance refresh"
38-
default = "90"
39-
type = "string"
38+
default = 90
39+
type = number
4040
}
4141

4242
variable "launch_template_version_description" {
4343
description = "Description of the new launch template version in Python's f-string format"
4444
default = "Automated AMI refresh to \"{image_id}\""
45-
type = "string"
45+
type = string
4646
}
4747

4848
variable "lambda_description" {
4949
description = "Description of the Lambda function"
5050
default = "Keeps ASG Launch Template updated with most recent AMI read from SSM Parameter"
51-
type = "string"
51+
type = string
5252
}
5353

5454
variable "lambda_name" {
5555
description = "Name of the Lambda function"
5656
default = "ASGRefreshInstances"
57-
type = "string"
57+
type = string
5858
}
5959

6060
variable "lambda_role_description" {
6161
description = "Role description for the Lambda function"
6262
default = ""
63-
type = "string"
63+
type = string
6464
}
6565

6666
variable "lambda_role_name" {
6767
description = "Role name for the Lambda function"
6868
default = "ASGRefreshInstancesLambdaRole"
69-
type = "string"
69+
type = string
7070
}
7171

7272
variable "lambda_timeout" {
7373
description = "Timeout for Lambda function in seconds"
7474
default = 60
75-
type = "string"
75+
type = number
7676
}
7777

7878
variable "launch_template_name" {
7979
description = "Name of the launch template used by auto scaling group to refresh"
80-
type = "string"
80+
type = string
8181
}
8282

8383
variable "launch_template_source_version" {
8484
description = "Source version for the new launch template"
8585
default = "$Default"
86-
type = "string"
86+
type = string
8787
}

versions.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_providers {
3+
archive = {
4+
source = "hashicorp/archive"
5+
}
6+
aws = {
7+
source = "hashicorp/aws"
8+
}
9+
null = {
10+
source = "hashicorp/null"
11+
}
12+
}
13+
required_version = ">= 0.13"
14+
}

0 commit comments

Comments
 (0)