-
Notifications
You must be signed in to change notification settings - Fork 61
/
iam.tf
87 lines (72 loc) · 2.02 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
#aws iam role for host -same account queries
resource "aws_iam_role" "bastion_service_role" {
name = var.service_name == "bastion-service" ? format(
"%s-%s-%s_bastion",
var.environment_name,
data.aws_region.current.name,
var.vpc,
) : var.service_name
count = local.assume_role_no
assume_role_policy = data.aws_iam_policy_document.bastion_service_role_assume[0].json
tags = var.tags
}
data "aws_iam_policy_document" "bastion_service_role_assume" {
count = local.assume_role_no
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = [
"ec2.amazonaws.com",
]
}
}
}
#########################
#Instance profile to assume role
#########################
resource "aws_iam_instance_profile" "bastion_service_profile" {
name = var.service_name == "bastion-service" ? format(
"%s-%s-%s_bastion",
var.environment_name,
data.aws_region.current.name,
var.vpc,
) : var.service_name
count = local.assume_role_no
role = aws_iam_role.bastion_service_role[0].name
}
data "aws_iam_policy_document" "check_ssh_authorized_keys" {
count = local.assume_role_no
statement {
effect = "Allow"
actions = [
"iam:ListUsers",
"iam:GetGroup",
"iam:GetSSHPublicKey",
"iam:ListSSHPublicKeys",
"iam:GetUser",
"iam:ListGroups",
"ec2:DescribeTags",
]
resources = [
"*",
]
}
}
resource "aws_iam_policy" "check_ssh_authorized_keys" {
name = var.service_name == "bastion-service" ? format(
"%s-%s-%s_bastion",
var.environment_name,
data.aws_region.current.name,
var.vpc,
) : var.service_name
count = local.assume_role_no
policy = data.aws_iam_policy_document.check_ssh_authorized_keys[0].json
}
resource "aws_iam_role_policy_attachment" "check_ssh_authorized_keys" {
role = aws_iam_role.bastion_service_role[0].name
count = local.assume_role_no
policy_arn = aws_iam_policy.check_ssh_authorized_keys[0].arn
}