-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlambda-token.tf
125 lines (109 loc) · 3.37 KB
/
lambda-token.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
data "archive_file" "token" {
type = "zip"
output_path = "${path.module}/.zip/${module.labels.id}_token.zip"
source_file = "${path.module}/templates/lambda-placeholder.js"
}
data "aws_iam_policy_document" "token_policy" {
statement {
actions = [
"s3:*",
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:DeleteNetworkInterface",
"secretsmanager:GetSecretValue",
"ssm:GetParameter"
]
resources = ["*"]
}
}
data "aws_iam_policy_document" "token_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
}
}
resource "aws_iam_policy" "token_policy" {
name = "${module.labels.id}-lambda-token-policy"
path = "/"
policy = data.aws_iam_policy_document.token_policy.json
}
resource "aws_iam_role" "token" {
name = "${module.labels.id}-lambda-token"
assume_role_policy = data.aws_iam_policy_document.token_assume_role.json
tags = module.labels.tags
}
resource "aws_iam_role_policy_attachment" "token_policy" {
role = aws_iam_role.token.name
policy_arn = aws_iam_policy.token_policy.arn
}
resource "aws_iam_role_policy_attachment" "token_logs" {
role = aws_iam_role.token.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_lambda_function" "token" {
filename = "${path.module}/.zip/${module.labels.id}_token.zip"
function_name = "${module.labels.id}-token"
source_code_hash = data.archive_file.token.output_base64sha256
role = aws_iam_role.token.arn
runtime = "nodejs10.x"
handler = "token.handler"
memory_size = 128
timeout = 15
tags = module.labels.tags
vpc_config {
security_group_ids = [module.lambda_sg.id]
subnet_ids = module.vpc.private_subnets
}
environment {
variables = {
CONFIG_VAR_PREFIX = local.config_var_prefix,
NODE_ENV = "production"
}
}
lifecycle {
ignore_changes = [
source_code_hash,
]
}
}
module "lambda_sg" {
source = "./modules/security-group"
open_egress = true
name = "${module.labels.id}-lambda-token"
environment = var.environment
vpc_id = module.vpc.vpc_id
tags = module.labels.tags
}
resource "aws_security_group_rule" "lambda_ingress" {
description = "Allows backend services to accept connections from ALB"
type = "ingress"
from_port = 0
to_port = 65535
protocol = "-1"
cidr_blocks = [var.vpc_cidr]
security_group_id = module.lambda_sg.id
}
resource "aws_security_group_rule" "lambda_egress_vpc" {
description = "Allows outbound connections to VPC CIDR block"
type = "egress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = [var.vpc_cidr]
security_group_id = module.lambda_sg.id
}
resource "aws_security_group_rule" "lambda_egress_endpoints" {
description = "Allows outbound connections to VPC S3 endpoint"
type = "egress"
from_port = 443
to_port = 443
protocol = "tcp"
prefix_list_ids = [module.vpc.vpc_endpoint_s3_pl_id]
security_group_id = module.lambda_sg.id
}