forked from prashantkalkar/stateful_application_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance_profile.tf
65 lines (58 loc) · 1.97 KB
/
instance_profile.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
resource "aws_iam_instance_profile" "node_instance_profile" {
name = "${var.app_name}-instance-profile"
role = aws_iam_role.node_role.name
}
resource "aws_iam_role" "node_role" {
name = "${var.app_name}-role"
assume_role_policy = data.aws_iam_policy_document.instance-assume-role-policy.json
}
data "aws_iam_policy_document" "instance-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "ssm_policy" {
role = aws_iam_role.node_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "instance_userdata_policy" {
role = aws_iam_role.node_role.name
policy_arn = aws_iam_policy.node_policy.arn
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
resource "aws_iam_policy" "node_policy" {
name = "${var.app_name}-node-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ec2:AttachVolume",
"ec2:AttachNetworkInterface",
"autoscaling:CompleteLifecycleAction",
"autoscaling:RecordLifecycleActionHeartbeat"
]
Effect = "Allow"
Resource = [
"arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:network-interface/*",
"arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:instance/*",
"arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:volume/*",
"arn:aws:autoscaling:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:autoScalingGroup:*"
]
},
{
Action = [
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeVolumes",
]
Effect = "Allow"
Resource = "*"
}
]
})
}