forked from schubergphilis/terraform-aws-mcaf-edgedevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
148 lines (130 loc) · 3.76 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
136
137
138
139
140
141
142
143
144
145
146
147
148
locals {
create_role = var.ssm_activation_role_id != null ? 0 : 1
iot_policy = var.iot_policy != null ? var.iot_policy : data.aws_iam_policy_document.default.json
ssm_activation_role_id = var.ssm_activation_role_id != null ? var.ssm_activation_role_id : aws_iam_role.ssm_activation[0].id
}
data "aws_iam_policy_document" "default" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"iot:Publish",
"iot:Subscribe",
"iot:Connect",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
]
resources = [
"*"
]
}
statement {
effect = "Allow"
actions = [
"greengrass:AssumeRoleForGroup",
"greengrass:CreateCertificate",
"greengrass:GetDeployment",
"greengrass:GetDeploymentArtifacts",
"greengrass:UpdateCoreDeploymentStatus",
"greengrass:GetConnectivityInfo",
"greengrass:UpdateConnectivityInfo"
]
resources = [
"*"
]
}
}
resource "aws_iot_policy" "default" {
name = "IoTCoreFullAccess-${var.name}"
policy = local.iot_policy
}
resource "aws_iot_thing" "default" {
name = var.name
}
resource "aws_iot_certificate" "default" {
active = true
}
data "http" "root_ca" {
url = "https://www.amazontrust.com/repository/AmazonRootCA1.pem"
}
resource "aws_iot_policy_attachment" "default" {
policy = aws_iot_policy.default.name
target = aws_iot_certificate.default.arn
}
resource "aws_iot_thing_principal_attachment" "default" {
principal = aws_iot_certificate.default.arn
thing = aws_iot_thing.default.name
}
resource "aws_ssm_parameter" "certificate_pem" {
name = "/${var.name}/iot/certificate-pem"
type = "SecureString"
value = aws_iot_certificate.default.certificate_pem
key_id = var.kms_key_id
tags = var.tags
}
resource "aws_ssm_parameter" "public_key" {
name = "/${var.name}/iot/public-key"
type = "SecureString"
value = aws_iot_certificate.default.public_key
key_id = var.kms_key_id
tags = var.tags
}
resource "aws_ssm_parameter" "private_key" {
name = "/${var.name}/iot/private-key"
type = "SecureString"
value = aws_iot_certificate.default.private_key
key_id = var.kms_key_id
tags = var.tags
}
resource "aws_ssm_parameter" "root_ca_crt" {
name = "/${var.name}/iot/root-ca-crt"
type = "SecureString"
value = data.http.root_ca.response_body
key_id = var.kms_key_id
tags = var.tags
}
data "aws_iam_policy_document" "ssm_activation" {
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["ssm.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ssm_activation" {
count = local.create_role
name = "SSMActivation-${var.name}"
assume_role_policy = data.aws_iam_policy_document.ssm_activation.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "ssm_activation" {
count = local.create_role
role = aws_iam_role.ssm_activation[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
}
resource "aws_ssm_activation" "default" {
name = var.name
description = "SSM Activation for ${var.name}"
iam_role = local.ssm_activation_role_id
expiration_date = timeadd(timestamp(), var.expiration_duration)
registration_limit = 1
depends_on = [aws_iam_role_policy_attachment.ssm_activation]
tags = var.tags
lifecycle {
ignore_changes = [
expiration_date
]
}
}
resource "aws_ssm_parameter" "ssm_activation" {
name = "/${var.name}/iot/ssm-activation"
type = "SecureString"
value = aws_ssm_activation.default.activation_code
key_id = var.kms_key_id
tags = var.tags
}