-
-
Notifications
You must be signed in to change notification settings - Fork 709
/
Copy pathmain.tf
498 lines (407 loc) · 15.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
provider "aws" {
region = "eu-west-1"
# Make it faster by skipping something
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
}
resource "random_pet" "this" {
length = 2
}
#################
# Build packages
#################
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime with requirements.txt present)
module "package_dir" {
source = "../../"
create_function = false
build_in_docker = true
runtime = "python3.12"
source_path = "${path.module}/../fixtures/python-app1"
artifacts_dir = "${path.root}/builds/package_dir/"
}
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime with requirements.txt present) and set temporary directory for pip install
module "package_dir_pip_dir" {
source = "../../"
create_function = false
build_in_docker = true
runtime = "python3.12"
source_path = [{
path = "${path.module}/../fixtures/python-app1"
pip_tmp_dir = "${path.cwd}/../fixtures"
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
}]
artifacts_dir = "${path.root}/builds/package_dir_pip_dir/"
}
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
module "package_dir_poetry" {
source = "../../"
create_function = false
build_in_docker = true
runtime = "python3.12"
docker_image = "build-python-poetry"
docker_file = "${path.module}/../fixtures/python-app-poetry/docker/Dockerfile"
source_path = [
{
path = "${path.module}/../fixtures/python-app-poetry"
poetry_install = true
}
]
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
}
# Create zip-archive of a src directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
module "package_src_poetry" {
source = "../../"
create_function = false
build_in_docker = true
runtime = "python3.12"
docker_image = "build-python-poetry"
docker_file = "${path.module}/../fixtures/python-app-src-poetry/docker/Dockerfile"
source_path = [
"${path.module}/../fixtures/python-app-src-poetry/src",
{
path = "${path.module}/../fixtures/python-app-src-poetry/pyproject.toml"
poetry_install = true
}
]
artifacts_dir = "${path.root}/builds/package_src_poetry/"
}
# Create zip-archive of a src directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
module "package_src_poetry2" {
source = "../../"
create_function = false
build_in_docker = true
runtime = "python3.12"
docker_image = "build-python-poetry"
docker_file = "${path.module}/../fixtures/python-app-src-poetry/docker/Dockerfile"
source_path = [
"${path.module}/../fixtures/python-app-src-poetry/src",
"${path.module}/../fixtures/python-app-src-poetry/pyproject.toml"
]
artifacts_dir = "${path.root}/builds/package_src_poetry2/"
}
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (not using docker)
module "package_dir_poetry_no_docker" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
{
path = "${path.module}/../fixtures/python-app-poetry"
poetry_install = true
}
]
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
}
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
module "package_dir_without_pip_install" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
{
path = "${path.module}/../fixtures/python-app1"
pip_requirements = false
# pip_requirements = true # Will run "pip install" with default requirements.txt
}
]
}
# Create zip-archive of a single file (without running "pip install")
module "package_file" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = "${path.module}/../fixtures/python-app1/index.py"
}
# Create zip-archive which contains:
# 1. A single file - index.py
# 2. Run "pip install" with specified requirements.txt into "vendor" directory inside of zip-archive
module "package_file_with_pip_requirements" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
"${path.module}/../fixtures/python-app1/index.py",
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
prefix_in_zip = "vendor"
}
]
}
# Create zip-archive which contains:
# 1. A single file - index.py
# 2. Run "pip install" with specified requirements.txt with additional options CLI --only-binary=:all: --platform manylinux2014_x86_64
module "package_file_with_pip_requirements_and_pip_additional_options" {
source = "../../"
create_function = false
runtime = "python3.12"
pip_additional_options = ["-only-binary=:all:", "--platform", "manylinux2014_x86_64"]
source_path = [
"${path.module}/../fixtures/python-app1/index.py",
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
}
]
}
# Create zip-archive which contains:
# 1. A single file - index.py
# 2. Content of directory "dir2"
# 3. Install pip requirements
# "pip install" is running in a Docker container for the specified runtime
module "package_with_pip_requirements_in_docker" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
"${path.module}/../fixtures/python-app1/index.py",
"${path.module}/../fixtures/python-app1/dir1/dir2",
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
}
]
build_in_docker = true
}
# Create zip-archive which contains:
# 1. A single file - index.py
# 2. Content of directory "dir2"
# 3. Install pip requirements
# "pip install" is running in a Docker container for the specified runtime
# The docker entrypoint is overridden, allowing you to run additional commands within the container
module "package_with_pip_requirements_in_docker_overriding_entrypoint" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
"${path.module}/../fixtures/python-app1/index.py",
"${path.module}/../fixtures/python-app1/dir1/dir2",
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
}
]
hash_extra = "package_with_pip_requirements_in_docker_overriding_entrypoint"
build_in_docker = true
docker_additional_options = [
"-e", "MY_ENV_VAR='My environment variable value'",
"-v", "${abspath(path.module)}/../fixtures/python-app1/docker/entrypoint.sh:/entrypoint/entrypoint.sh:ro",
]
docker_entrypoint = "/entrypoint/entrypoint.sh"
}
# Create zip-archive which contains content of directory with commands and patterns applied.
#
# Notes:
# 1. `:zip` is a special command which creates content of current working
# directory (first argument) and places it inside of path (second argument).
# 2. Patterns (Python Regex) apply to all elements before putting them in zip-archive
module "package_with_commands_and_patterns" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
{
path = "${path.module}/../fixtures/python-app1"
commands = [
":zip",
"cd `mktemp -d`",
"pip install --target=. -r ${abspath(path.module)}/../fixtures/python-app1/requirements.txt",
":zip . vendor/",
]
patterns = [
"!vendor/colorful-0.5.4.dist-info/RECORD",
"!vendor/colorful-.+.dist-info/.*",
"!vendor/colorful/__pycache__/?.*",
]
}
]
}
# Some use cases might require the production packages are deployed while maintaining local node_modules folder
# This example saves the node_modules folder by moving it to an ignored directory
# After the zip file is created with production node_modules, the dev node_modules folder is restored
module "npm_package_with_commands_and_patterns" {
source = "../../"
create_function = false
runtime = "nodejs18.x"
source_path = [
{
path = "${path.module}/../fixtures/node-app"
commands = [
"[ ! -d node_modules ] || mv node_modules node_modules_temp",
"npm install --production",
":zip",
"rm -rf node_modules",
"[ ! -d node_modules_temp ] || mv node_modules_temp node_modules",
]
patterns = [
"!node_modules_temp/.*"
]
}
]
}
# Create zip-archive with various sources and patterns.
# Note, that it is possible to write comments in patterns.
module "package_with_patterns" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
},
"${path.module}/../fixtures/python-app1/index.py",
{
path = "${path.module}/../fixtures/python-app1/index.py"
patterns = <<END
# To use comments in heredoc patterns set the env var:
# export TF_LAMBDA_PACKAGE_PATTERN_COMMENTS=true
# The intent for comments just to use in examples and demo snippets.
# To write a comment start it with double spaces and the # sign
# if it follows a pattern.
!index\.py # Exclude a file with 'index.py' name
index\..* # Re-include
END
},
{
path = "${path.module}/../fixtures/dirtree"
prefix_in_zip = "vendor"
patterns = <<END
# To see step by step output how files and folders was skipped or added
# set an env var in your shell by a command similar to:
# export TF_LAMBDA_PACKAGE_LOG_LEVEL=DEBUG
# To play with this demo pattern you can go to a ../fixtures folder
# and run there a dirtree.sh script to create a demo dirs tree
# matchable by following patterns.
!abc/.* # Filter out everything in an abc folder with the folder itself
!abc/.+ # Filter out just all folder's content but keep the folder even if it's empty
abc/def/.* # Re-include everything in abc/def sub folder
!abc/def/ghk/.* # Filter out again in abc/def/ghk sub folder
!.*/fiv.*
!.*/zi--.*
########################################################################
# Special cases
# !.*/ # Removes all explicit directories from a zip file, mean while
# the zip file still valid and unpacks correctly but will not
# contain any empty directories.
# !.*/bar/ # To remove just some directory use more qualified path to it.
# !([^/]+/){3,} # Remove all directories more than 3 folders in depth.
END
}
]
}
# Create zip-archive with the dependencies specified in requirements.txt
# which will be building in a docker container where SSH agent will be available
# (to access private repositories, for example).
module "package_with_docker" {
source = "../../"
create_function = false
runtime = "python3.12"
source_path = [
"${path.module}/../fixtures/python-app1/index.py",
"${path.module}/../fixtures/python-app1/dir1/dir2",
{
pip_requirements = "${path.module}/../fixtures/python-app1/requirements.txt"
}
]
hash_extra = "something-unique-to-not-conflict-with-module.package_with_pip_requirements_in_docker"
build_in_docker = true
docker_pip_cache = true
docker_with_ssh_agent = true
# docker_file = "${path.module}/../fixtures/python-app1/docker/Dockerfile"
docker_build_root = "${path.module}/../fixtures/python-app1/docker"
docker_image = "public.ecr.aws/sam/build-python3.12:latest"
}
# Create zip-archive of a single directory where "npm install" will also be executed (default for nodejs runtime)
module "package_dir_with_npm_install" {
source = "../../"
create_function = false
runtime = "nodejs14.x"
source_path = "${path.module}/../fixtures/nodejs14.x-app1"
}
# Create zip-archive of a single directory without running "npm install" (which is the default for nodejs runtime)
module "package_dir_without_npm_install" {
source = "../../"
create_function = false
runtime = "nodejs14.x"
source_path = [
{
path = "${path.module}/../fixtures/nodejs14.x-app1"
npm_requirements = false
# npm_requirements = true # Will run "npm install" with package.json
}
]
}
# Create zip-archive of a single directory where "npm install" will also be executed using docker
module "package_with_npm_requirements_in_docker" {
source = "../../"
create_function = false
runtime = "nodejs14.x"
source_path = "${path.module}/../fixtures/nodejs14.x-app1"
build_in_docker = true
hash_extra = "something-unique-to-not-conflict-with-module.package_dir_with_npm_install"
}
################################
# Build package in Docker and
# use it to deploy Lambda Layer
################################
module "lambda_layer" {
source = "../../"
create_layer = true
layer_name = "${random_pet.this.id}-layer-dockerfile"
compatible_runtimes = ["python3.12"]
source_path = "${path.module}/../fixtures/python-app1"
hash_extra = "extra-hash-to-prevent-conflicts-with-module.package_dir"
build_in_docker = true
runtime = "python3.12"
docker_image = "public.ecr.aws/sam/build-python3.12:latest"
docker_file = "${path.module}/../fixtures/python-app1/docker/Dockerfile"
artifacts_dir = "${path.root}/builds/lambda_layer/"
}
module "lambda_layer_poetry" {
source = "../../"
create_layer = true
layer_name = "${random_pet.this.id}-layer-poetry-dockerfile"
compatible_runtimes = ["python3.12"]
source_path = [
{
path = "${path.module}/../fixtures/python-app-poetry"
poetry_install = true
poetry_tmp_dir = "${path.cwd}/../fixtures"
}
]
hash_extra = "extra-hash-to-prevent-conflicts-with-module.package_dir"
build_in_docker = true
runtime = "python3.12"
docker_image = "build-python-poetry"
docker_file = "${path.module}/../fixtures/python-app-poetry/docker/Dockerfile"
artifacts_dir = "${path.root}/builds/lambda_layer_poetry/"
}
#######################
# Deploy from packaged
#######################
module "lambda_function_from_package" {
source = "../../"
create_package = false
local_existing_package = module.package_with_commands_and_patterns.local_filename
function_name = "${random_pet.this.id}-function-packaged"
handler = "index.lambda_handler"
runtime = "python3.12"
layers = [
module.lambda_layer.lambda_layer_arn
]
}
################################################
# Layer that supports requirements.txt install #
###############################################
module "lambda_layer_pip_requirements" {
source = "../.."
create_function = false
create_layer = true
layer_name = "${random_pet.this.id}-layer-pip-requirements"
compatible_runtimes = ["python3.12"]
runtime = "python3.12" # required to force layers to do pip install
source_path = [
{
path = "${path.module}/../fixtures/python-app1"
pip_requirements = true
prefix_in_zip = "python" # required to get the path correct
}
]
}