-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathmain.tf
167 lines (133 loc) · 4.1 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
terraform {
required_version = ">= 0.12"
}
provider "aws" {
region = var.aws_region
}
provider "archive" {}
data "archive_file" "zip" {
type = "zip"
source_file = "hello_lambda.py"
output_path = "hello_lambda.zip"
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
sid = ""
effect = "Allow"
principals {
identifiers = ["lambda.amazonaws.com"]
type = "Service"
}
actions = ["sts:AssumeRole"]
}
}
data "aws_partition" "current" {}
data "aws_iam_policy" "AWSLambdaVPCAccessExecutionRole" {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
data "aws_iam_policy" "AmazonElasticFileSystemClientFullAccess" {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonElasticFileSystemClientFullAccess"
}
resource "aws_iam_role" "iam_role_for_lambda" {
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "AWSLambdaVPCAccessExecutionRole-attach" {
role = aws_iam_role.iam_role_for_lambda.name
policy_arn = data.aws_iam_policy.AWSLambdaVPCAccessExecutionRole.arn
}
resource "aws_iam_role_policy_attachment" "AmazonElasticFileSystemClientFullAccess-attach" {
role = aws_iam_role.iam_role_for_lambda.name
policy_arn = data.aws_iam_policy.AmazonElasticFileSystemClientFullAccess.arn
}
# Default VPC
resource "aws_default_vpc" "default" {
}
data "aws_availability_zones" "available" {
state = "available"
}
# Two default subnets in the Default VPC
resource "aws_default_subnet" "default_az1" {
availability_zone = data.aws_availability_zones.available.names[0]
}
resource "aws_default_subnet" "default_az2" {
availability_zone = data.aws_availability_zones.available.names[1]
}
# Default security group
resource "aws_default_security_group" "default" {
vpc_id = aws_default_vpc.default.id
ingress {
protocol = -1
self = true
from_port = 0
to_port = 0
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# EFS file system
resource "aws_efs_file_system" "efs_for_lambda" {
tags = {
Name = "efs_for_lambda"
}
}
# Two mount targets connect the file system to the subnets
resource "aws_efs_mount_target" "mount_target_az1" {
file_system_id = aws_efs_file_system.efs_for_lambda.id
subnet_id = aws_default_subnet.default_az1.id
security_groups = [aws_default_security_group.default.id]
}
resource "aws_efs_mount_target" "mount_target_az2" {
file_system_id = aws_efs_file_system.efs_for_lambda.id
subnet_id = aws_default_subnet.default_az2.id
security_groups = [aws_default_security_group.default.id]
}
# EFS access point used by lambda file system
resource "aws_efs_access_point" "access_point_lambda" {
file_system_id = aws_efs_file_system.efs_for_lambda.id
root_directory {
path = "/lambda"
creation_info {
owner_gid = 1000
owner_uid = 1000
permissions = "777"
}
}
posix_user {
gid = 1000
uid = 1000
}
}
resource "aws_lambda_function" "example_lambda" {
function_name = "hello_lambda"
filename = data.archive_file.zip.output_path
source_code_hash = data.archive_file.zip.output_base64sha256
role = aws_iam_role.iam_role_for_lambda.arn
handler = "hello_lambda.lambda_handler"
runtime = "python3.12"
timeout = 60
environment {
variables = {
greeting = "Hello"
}
}
vpc_config {
subnet_ids = [aws_default_subnet.default_az1.id, aws_default_subnet.default_az2.id]
security_group_ids = [aws_default_security_group.default.id]
}
file_system_config {
arn = aws_efs_access_point.access_point_lambda.arn
local_mount_path = "/mnt/efs"
}
# Explicitly declare dependency on EFS mount target.
# When creating or updating Lambda functions, mount target must be in 'available' lifecycle state.
depends_on = [
aws_efs_mount_target.mount_target_az1,
aws_efs_mount_target.mount_target_az2,
]
}