-
Notifications
You must be signed in to change notification settings - Fork 14
/
iam.tf
82 lines (67 loc) · 1.73 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
# IAM role so API Gateway has the necessary permissions to SendMessage to SQS queue
resource "aws_iam_role" "apiSQS" {
name = "apigateway_sqs"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data "template_file" "gateway_policy" {
template = file("policies/api-gateway-permission.json")
vars = {
sqs_arn = aws_sqs_queue.queue.arn
}
}
resource "aws_iam_policy" "api_policy" {
name = "api-sqs-cloudwatch-policy"
policy = data.template_file.gateway_policy.rendered
}
resource "aws_iam_role_policy_attachment" "api_exec_role" {
role = aws_iam_role.apiSQS.name
policy_arn = aws_iam_policy.api_policy.arn
}
# Add a Lambda permission that allows the specific SQS to invoke it
data "template_file" "lambda_policy" {
template = file("policies/lambda-permission.json")
vars = {
sqs_arn = aws_sqs_queue.queue.arn
}
}
resource "aws_iam_policy" "lambda_sqs_policy" {
name = "lambda_policy_db"
description = "IAM policy for lambda Being invoked by SQS"
policy = data.template_file.lambda_policy.rendered
}
resource "aws_iam_role" "lambda_exec_role" {
name = "${var.name}-lambda-db"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_role_policy" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = aws_iam_policy.lambda_sqs_policy.arn
}