This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
iam.tf
127 lines (106 loc) · 2.57 KB
/
iam.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
data "aws_caller_identity" "current" {}
/*
* Lambda IAM
*/
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
effect = "Allow"
actions = [
"sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "lambda" {
statement {
effect = "Allow"
actions = [
"autoscaling:CompleteLifecycleAction",
]
resources = [
var.autoscaling_group_arn
]
}
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = [
format("%s:*", aws_cloudwatch_log_group.lambda_log_group.arn)
]
}
statement {
effect = "Allow"
actions = [
"ecs:ListContainerInstances",
"ecs:DescribeContainerInstances",
"ecs:UpdateContainerInstancesState",
]
resources = [
var.ecs_cluster_arn,
format("%s/*", var.ecs_cluster_arn),
format("arn:aws:ecs:%s:%s:container-instance/%s/*", var.region, data.aws_caller_identity.current.account_id, var.ecs_cluster_name)
]
}
statement {
effect = "Allow"
actions = [
"sns:Publish",
]
resources = [
aws_sns_topic.topic.arn
]
}
}
resource "aws_iam_role" "lambda" {
name = format("%s-draining-function-role", var.autoscaling_group_name)
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
tags = var.tags
}
resource "aws_iam_role_policy" "lambda_execution_policy" {
name = format("%s-draining-function-policy", var.autoscaling_group_name)
role = aws_iam_role.lambda.id
policy = data.aws_iam_policy_document.lambda.json
}
/*
* Autoscaling
*/
data "aws_iam_policy_document" "lifecycle_assume_role" {
statement {
effect = "Allow"
actions = [
"sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"autoscaling.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lifecycle" {
name = format("%s-lifecycle-role", var.autoscaling_group_name)
assume_role_policy = data.aws_iam_policy_document.lifecycle_assume_role.json
tags = var.tags
}
data "aws_iam_policy_document" "lifecycle_policy" {
statement {
effect = "Allow"
actions = [
"sns:Publish",
]
resources = [
aws_sns_topic.topic.arn
]
}
}
resource "aws_iam_role_policy" "lifecycle_execution_policy" {
name = format("%s-lifecycle-policy", var.autoscaling_group_name)
role = aws_iam_role.lifecycle.id
policy = data.aws_iam_policy_document.lifecycle_policy.json
}