-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3CloudWatchIam.tf
180 lines (164 loc) · 4.84 KB
/
s3CloudWatchIam.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Define the IAM Role
resource "aws_iam_role" "app_instance_role" {
name = "app-instance-role"
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "ec2.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
}
# S3 Policy for Accessing S3 Resources
resource "aws_iam_policy" "s3_access_policy" {
name = "s3-access-policy"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource" : "*"
}
]
})
}
# CloudWatch Policy for Logging and Metrics
resource "aws_iam_policy" "cloudwatch_agent_policy" {
name = "cloudwatch-agent-policy"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"cloudwatch:PutMetricData",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource" : "*"
}
]
})
}
# Define the IAM Role for Lambda execution
resource "aws_iam_role" "lambda_execution_role" {
name = "lambda-execution-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
# SNS Policy to allow EC2 role to publish to the SNS topic
resource "aws_iam_policy" "sns_publish_policy" {
name = "sns-publish-policy"
description = "Policy to allow EC2 instance to publish to SNS topic"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : "sns:Publish",
"Resource" : aws_sns_topic.user_verification_topic.arn
}
]
})
}
# Attach both policies to the IAM Role
resource "aws_iam_role_policy_attachment" "attach_s3_policy" {
role = aws_iam_role.app_instance_role.name
policy_arn = aws_iam_policy.s3_access_policy.arn
}
resource "aws_iam_role_policy_attachment" "attach_cloudwatch_policy" {
role = aws_iam_role.app_instance_role.name
policy_arn = aws_iam_policy.cloudwatch_agent_policy.arn
}
# Create an IAM Instance Profile for the Role
resource "aws_iam_instance_profile" "app_instance_profile" {
name = "app-instance-profile"
role = aws_iam_role.app_instance_role.name
}
# Attach SNS publish policy to the EC2 role
resource "aws_iam_role_policy_attachment" "attach_sns_publish_policy" {
role = aws_iam_role.app_instance_role.name
policy_arn = aws_iam_policy.sns_publish_policy.arn
}
# Create an IAM Instance Profile for the Lambda function (if needed)
resource "aws_iam_instance_profile" "lambda_instance_profile" {
name = "lambda-instance-profile"
role = aws_iam_role.lambda_execution_role.name
}
resource "aws_iam_policy" "secrets_access_policy" {
name = "SecretsAccessPolicy"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "secretsmanager:GetSecretValue"
Effect = "Allow"
"Resource" : [
aws_secretsmanager_secret.db_password_secret.arn,
aws_secretsmanager_secret.email_credentials_secret.arn
]
}
]
})
}
# Attach secrets_access_policy to the EC2 role
resource "aws_iam_role_policy_attachment" "attach_secrets_access_policy" {
role = aws_iam_role.app_instance_role.name
policy_arn = aws_iam_policy.secrets_access_policy.arn
}
resource "aws_iam_policy" "lambda_secrets_manager_policy" {
name = "LambdaSecretsManagerPolicy"
description = "Policy that allows Lambda to access Secrets Manager and KMS"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Sid = "AllowSecretsManagerGetSecretValue"
Effect = "Allow"
Action = "secretsmanager:GetSecretValue"
Resource = "arn:aws:secretsmanager:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:secret:email-credentials-*"
},
{
Sid = "AllowKMSDecrypt"
Effect = "Allow"
Action = [
"kms:Decrypt",
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"],
Resource = "arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:key/${aws_kms_key.secrets_key.id}"
}
]
})
}
resource "aws_iam_role_policy_attachment" "attach_lambda_secrets_manager_policy" {
policy_arn = aws_iam_policy.lambda_secrets_manager_policy.arn
role = aws_iam_role.lambda_execution_role.name
}