generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vpc_policies.tf
99 lines (80 loc) · 2.19 KB
/
vpc_policies.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
locals {
use_vpc = (length(var.vpc.subnet_ids) > 0 && length(var.vpc.security_group_ids) > 0)
}
### Policies needed if not on VPN
data "aws_iam_policy_document" "non_vpc_policies" {
count = local.use_vpc ? 0 : 1
statement {
sid = "CloudWatchAccess"
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"*"
]
}
statement {
sid = "ECRImageAccess"
effect = "Allow"
actions = [
"ecr:GetDownloadUrlForlayer",
"ecr:BatchGetImage"
]
resources = [
var.ecr_arn
]
}
}
resource "aws_iam_policy" "non_vpc_policies" {
count = local.use_vpc ? 0 : 1
name = "${var.name}_non_vpc"
path = "/"
policy = data.aws_iam_policy_document.non_vpc_policies[0].json
tags = local.common_tags
}
resource "aws_iam_role_policy_attachment" "non_vpc_policies" {
count = local.use_vpc ? 0 : 1
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.non_vpc_policies[0].arn
}
### Policies needed if on VPC
data "aws_iam_policy_document" "vpc_policies" {
count = local.use_vpc ? 1 : 0
statement {
sid = "ECRImageAccess"
effect = "Allow"
actions = [
"ecr:GetDownloadUrlForlayer",
"ecr:BatchGetImage"
]
resources = [
var.ecr_arn
]
}
}
resource "aws_iam_policy" "vpc_policies" {
count = local.use_vpc ? 1 : 0
name = "${var.name}_vpc"
path = "/"
policy = data.aws_iam_policy_document.vpc_policies[0].json
tags = local.common_tags
}
resource "aws_iam_role_policy_attachment" "vpc_policies" {
count = local.use_vpc ? 1 : 0
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.vpc_policies[0].arn
}
# Use AWS managed IAM policy
####
# Provides minimum permissions for a Lambda function to execute while
# accessing a resource within a VPC - create, describe, delete network
# interfaces and write permissions to CloudWatch Logs.
####
resource "aws_iam_role_policy_attachment" "AWSLambdaVPCAccessExecutionRole" {
count = local.use_vpc ? 1 : 0
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}