-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathiamrole.tf
76 lines (69 loc) · 2.33 KB
/
iamrole.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
#Create a role
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
resource "aws_iam_role" "ec2_role" {
name = "${var.name}-ec2-role"
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
#Attach role to policy
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
resource "aws_iam_role_policy_attachment" "custom" {
role = aws_iam_role.ec2_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
#Attach role to an instance profile
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile
resource "aws_iam_instance_profile" "ec2_profile" {
name = "${var.name}-ec2-profile"
role = aws_iam_role.ec2_role.name
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy
resource "aws_iam_policy" "custom_policy_ssm" {
name = "${var.name}-custom-policy-ssm"
path = "/"
description = "A policy to allow Linux Amazon EC2 instances to talk with ssm parameters."
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ssm:GetParameters",
"ssm:GetParameter"
]
Effect = "Allow"
Resource = "${aws_ssm_parameter.cloudwatch_linux_config.arn}"
},
{
"Effect" : "Allow",
"Action" : [
"kms:Decrypt"
],
"Resource" : "${aws_kms_key.custom_kms_key.arn}"
}
]
})
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment
resource "aws_iam_role_policy_attachment" "policy_attachment_custom_ssm" {
role = aws_iam_role.ec2_role.name
policy_arn = aws_iam_policy.custom_policy_ssm.arn
}
resource "aws_iam_role_policy_attachment" "custom_cw_agent" {
role = aws_iam_role.ec2_role.name
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}