-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
135 lines (113 loc) · 3.02 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module "tags" {
source = "rhythmictech/tags/terraform"
version = "~> 1.1"
tags = var.tags
names = [
var.name,
"rolling-restart",
"lambda",
]
}
module "lambda_version" {
source = "rhythmictech/find-release-by-semver/github"
version = "~> 1.1.1"
repo_name = local.repo_name
repo_owner = local.repo_owner
version_constraint = var.lambda_version_constraint
}
locals {
lambda_version = module.lambda_version.target_version
lambda_version_tag = module.lambda_version.version_info.release_tag
}
resource "null_resource" "lambda_zip" {
triggers = {
on_version_change = local.lambda_version
}
provisioner "local-exec" {
command = "curl -Lso lambda-${local.lambda_version}.zip https://github.com/${local.repo_full_name}/releases/download/${local.lambda_version_tag}/lambda.zip"
}
}
data "external" "sha" {
program = [
"${path.module}/getsha.sh"
]
query = {
repo_full_name = local.repo_full_name
tag = local.lambda_version_tag
}
}
data "aws_iam_policy_document" "lambda_assume_role_policy" {
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name_prefix = module.tags.name32
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
tags = module.tags.tags
}
data "aws_iam_policy_document" "lambda_policy_doc" {
statement {
actions = [
"autoscaling:SetInstanceHealth"
]
resources = [
"arn:aws:autoscaling:${local.region}:${local.account_id}:autoScalingGroup:*:autoScalingGroupName/${var.asg_name}"
]
}
statement {
actions = [
"autoscaling:DescribeAutoScalingGroups"
]
resources = [
"*"
]
}
statement {
actions = [
"codepipeline:PutJobSuccessResult",
"codepipeline:PutJobFailureResult"
]
resources = [
"*"
]
}
}
resource "aws_iam_role_policy" "this" {
name_prefix = module.tags.name
role = aws_iam_role.this.name
policy = data.aws_iam_policy_document.lambda_policy_doc.json
}
resource "aws_iam_role_policy_attachment" "lambda-execution-role-attach" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "random_uuid" "lambda_uuid" {}
resource "aws_lambda_function" "this" {
filename = "lambda-${local.lambda_version}.zip"
function_name = "${module.tags.name32}_${substr(random_uuid.lambda_uuid.result, 0, 31)}"
role = aws_iam_role.this.arn
handler = "rolling-restart.handler"
runtime = "python3.8"
timeout = 600
source_code_hash = data.external.sha.result.sha
tags = module.tags.tags
environment {
variables = {
ASG_NAME = var.asg_name
LOGLEVEL = var.loglevel
}
}
lifecycle {
ignore_changes = [
last_modified
]
}
depends_on = [null_resource.lambda_zip]
}