-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_security.tf
88 lines (71 loc) · 2.2 KB
/
lambda_security.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
# Role for the Lambda to assume
resource "aws_iam_role" "lambda_execution_role" {
name = "${local.application_name}-lambda-execution"
assume_role_policy = data.aws_iam_policy_document.lambda_execution_role.json
description = "${local.application_name} Lambda Execution Role"
}
# Boilterplate-y policy that allows Lambda to assume this role
data "aws_iam_policy_document" "lambda_execution_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
actions = ["sts:AssumeRole"]
}
}
# The meat of the Lambda
data "aws_iam_policy_document" "lambda_access_policy" {
statement {
effect = "Allow"
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["${aws_cloudwatch_log_group.lambda_log_group.arn}:*"]
}
statement {
effect = "Allow"
actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DetachNetworkInterface",
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = ["ssm:GetParameters", "ssm:GetParameter"]
resources = aws_ssm_parameter.secure_param.*.arn
}
statement {
effect = "Allow"
actions = ["kms:Decrypt"]
resources = [aws_kms_key.key.arn]
}
}
resource "aws_iam_role_policy" "lambda_kms_policy" {
name = "${local.application_name}-lambda-policy"
role = aws_iam_role.lambda_execution_role.id
policy = data.aws_iam_policy_document.lambda_access_policy.json
}
## Security Group for Lambda in VPC
# ---------------------------------
resource "aws_security_group" "api_rules" {
count = var.lambda_vpc_id == "" ? 0 : 1
name = "${local.application_name}-lambda-SG"
description = "Allows output traffic but no inbound -- requests come from API Gateway"
vpc_id = var.lambda_vpc_id
# No ingress -- block everything, only API Gateway should be talking to this.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = local.tags
}
locals {
lambda_security_group_id = var.lambda_vpc_id == "" ? null : [aws_security_group.api_rules[0].id]
}