-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.tf
75 lines (64 loc) · 2.04 KB
/
role.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
data "aws_caller_identity" "current" {}
# Trust relationship policy document for AWS Service.
data "aws_iam_policy_document" "lambda" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "ecs" {
statement {
actions = [
"ecs:RunTask"
]
resources = [
"arn:aws:ecs:${var.region}:${data.aws_caller_identity.current.account_id}:task-definition/${var.ecs_task_name}:*"
]
condition {
test = "ArnLike"
variable = "ecs:cluster"
values = [
"arn:aws:ecs:${var.region}:${data.aws_caller_identity.current.account_id}:cluster/${var.ecs_cluster_name}"
]
}
}
statement {
actions = ["iam:PassRole"]
resources = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/*"
]
condition {
test = "StringLike"
variable = "iam:PassedToService"
values = [
"ecs-tasks.amazonaws.com"
]
}
}
}
# ------------------------------------------------------------------------------------------------
# Create Roles
# ------------------------------------------------------------------------------------------------
resource "aws_iam_role" "lambda_role" {
name = "s3-ecs-task-lambda_role"
assume_role_policy = data.aws_iam_policy_document.lambda.json
tags = {
Name = "arcdemo-lambda-role"
}
}
# ------------------------------------------------------------------------------------------------
# Attach Policies to Role
# ------------------------------------------------------------------------------------------------
resource "aws_iam_role_policy_attachment" "lamba_exec_role_eni" {
role = aws_iam_role.lambda_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
resource "aws_iam_role_policy" "ecs_lambda" {
name = "lambda_run_ecs"
role = aws_iam_role.lambda_role.id
policy = data.aws_iam_policy_document.ecs.json
}