Skip to content

Commit 7555e62

Browse files
author
Ryan Ragnell
committed
fix: Make quiet_archive_local_exec properly suppress Poetry/pip/npm output
- Pass quiet flag from Terraform to package.py via query data - Add quiet flag to build_data dictionary in prepare_command - Suppress stdout/stderr in Poetry, pip, and npm subprocess calls when quiet=true - Add example demonstrating quiet packaging functionality - Update documentation and apply formatting fixes Fixes issue where quiet_archive_local_exec only affected Terraform's local-exec output but not the actual Poetry/pip/npm subprocess calls within package.py. Resolves #706
1 parent 1c3b16a commit 7555e62

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

examples/build-package/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
Configuration in this directory creates deployment packages in a variety of combinations.
44

5+
This example demonstrates various packaging scenarios including:
6+
- Python packages with pip requirements
7+
- Poetry-based Python packages
8+
- Node.js packages with npm
9+
- Docker-based builds
10+
- **Quiet packaging** - suppressing Poetry/pip/npm output during builds using `quiet_archive_local_exec = true`
11+
512
Look into [Runtimes Examples](https://github.com/terraform-aws-modules/terraform-aws-lambda/tree/master/examples/runtimes) for more ways to build and deploy AWS Lambda Functions using supported runtimes (Rust, Go, Java).
613

714
## Usage
@@ -44,6 +51,7 @@ Note that this example may create resources which cost money. Run `terraform des
4451
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
4552
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
4653
| <a name="module_package_dir_poetry_no_docker"></a> [package\_dir\_poetry\_no\_docker](#module\_package\_dir\_poetry\_no\_docker) | ../../ | n/a |
54+
| <a name="module_package_dir_poetry_quiet"></a> [package\_dir\_poetry\_quiet](#module\_package\_dir\_poetry\_quiet) | ../../ | n/a |
4755
| <a name="module_package_dir_with_npm_install"></a> [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a |
4856
| <a name="module_package_dir_with_npm_install_lock_file"></a> [package\_dir\_with\_npm\_install\_lock\_file](#module\_package\_dir\_with\_npm\_install\_lock\_file) | ../../ | n/a |
4957
| <a name="module_package_dir_without_npm_install"></a> [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a |

examples/build-package/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ module "package_dir_poetry_no_docker" {
119119
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
120120
}
121121

122+
# Create zip-archive with Poetry dependencies and demonstrate quiet packaging output
123+
module "package_dir_poetry_quiet" {
124+
source = "../../"
125+
126+
create_function = false
127+
128+
runtime = "python3.12"
129+
130+
source_path = [
131+
{
132+
path = "${path.module}/../fixtures/python-app-poetry"
133+
poetry_install = true
134+
}
135+
]
136+
artifacts_dir = "${path.root}/builds/package_dir_poetry_quiet/"
137+
quiet_archive_local_exec = true # Suppress Poetry/pip output during packaging
138+
}
139+
122140
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
123141
module "package_dir_without_pip_install" {
124142
source = "../../"

package.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,11 @@ def __init__(
293293
compress_type=zipfile.ZIP_DEFLATED,
294294
compresslevel=None,
295295
timestamp=None,
296+
quiet=False,
296297
):
297298
self.timestamp = timestamp
298299
self.filename = zip_filename
300+
self.quiet = quiet
299301

300302
if not (self.filename and isinstance(self.filename, str)):
301303
raise ValueError("Zip file path must be provided")
@@ -312,7 +314,8 @@ def open(self):
312314
raise zipfile.BadZipFile("ZipStream object can't be reused")
313315
self._ensure_base_path(self.filename)
314316
self._tmp_filename = "{}.tmp".format(self.filename)
315-
self._log.info("creating '%s' archive", self.filename)
317+
if not self.quiet:
318+
self._log.info("creating '%s' archive", self.filename)
316319
self._zip = zipfile.ZipFile(self._tmp_filename, "w", self._compress_type)
317320
return self
318321

@@ -356,7 +359,8 @@ def write_dirs(self, *base_dirs, prefix=None, timestamp=None):
356359
"""
357360
self._ensure_open()
358361
for base_dir in base_dirs:
359-
self._log.info("adding content of directory: %s", base_dir)
362+
if not self.quiet:
363+
self._log.info("adding content of directory: %s", base_dir)
360364
for path in emit_dir_content(base_dir):
361365
arcname = os.path.relpath(path, base_dir)
362366
self._write_file(path, prefix, arcname, timestamp)
@@ -382,10 +386,11 @@ def _write_file(self, file_path, prefix=None, name=None, timestamp=None):
382386
if prefix:
383387
arcname = os.path.join(prefix, arcname)
384388
zinfo = self._make_zinfo_from_file(file_path, arcname)
385-
if zinfo.is_dir():
386-
self._log.info("adding: %s/", arcname)
387-
else:
388-
self._log.info("adding: %s", arcname)
389+
if not self.quiet:
390+
if zinfo.is_dir():
391+
self._log.info("adding: %s/", arcname)
392+
else:
393+
self._log.info("adding: %s", arcname)
389394
if timestamp is None:
390395
timestamp = self.timestamp
391396
date_time = self._timestamp_to_date_time(timestamp)
@@ -1170,7 +1175,15 @@ def install_pip_requirements(query, requirements_file, tmp_dir):
11701175
cmd_log.info(shlex_join(pip_command))
11711176
log_handler and log_handler.flush()
11721177
try:
1173-
check_call(pip_command, env=subproc_env)
1178+
if query.quiet:
1179+
check_call(
1180+
pip_command,
1181+
env=subproc_env,
1182+
stdout=subprocess.DEVNULL,
1183+
stderr=subprocess.DEVNULL,
1184+
)
1185+
else:
1186+
check_call(pip_command, env=subproc_env)
11741187
except FileNotFoundError as e:
11751188
raise RuntimeError(
11761189
"Python interpreter version equal "
@@ -1346,7 +1359,15 @@ def copy_file_to_target(file, temp_dir):
13461359
cmd_log.info(poetry_commands)
13471360
log_handler and log_handler.flush()
13481361
for poetry_command in poetry_commands:
1349-
check_call(poetry_command, env=subproc_env)
1362+
if query.quiet:
1363+
check_call(
1364+
poetry_command,
1365+
env=subproc_env,
1366+
stdout=subprocess.DEVNULL,
1367+
stderr=subprocess.DEVNULL,
1368+
)
1369+
else:
1370+
check_call(poetry_command, env=subproc_env)
13501371

13511372
os.remove(pyproject_target_file)
13521373
if poetry_lock_target_file:
@@ -1443,7 +1464,15 @@ def install_npm_requirements(query, requirements_file, tmp_dir):
14431464
cmd_log.info(shlex_join(npm_command))
14441465
log_handler and log_handler.flush()
14451466
try:
1446-
check_call(npm_command, env=subproc_env)
1467+
if query.quiet:
1468+
check_call(
1469+
npm_command,
1470+
env=subproc_env,
1471+
stdout=subprocess.DEVNULL,
1472+
stderr=subprocess.DEVNULL,
1473+
)
1474+
else:
1475+
check_call(npm_command, env=subproc_env)
14471476
except FileNotFoundError as e:
14481477
raise RuntimeError(
14491478
"Nodejs interpreter version equal "
@@ -1719,6 +1748,7 @@ def prepare_command(args):
17191748
"runtime": runtime,
17201749
"artifacts_dir": artifacts_dir,
17211750
"build_plan": build_plan,
1751+
"quiet": query.quiet,
17221752
}
17231753
if docker:
17241754
build_data["docker"] = docker
@@ -1778,12 +1808,13 @@ def build_command(args):
17781808

17791809
# Zip up the build plan and write it to the target filename.
17801810
# This will be used by the Lambda function as the source code package.
1781-
with ZipWriteStream(filename) as zs:
1811+
with ZipWriteStream(filename, quiet=getattr(query, 'quiet', False)) as zs:
17821812
bpm = BuildPlanManager(args, log=log)
17831813
bpm.execute(build_plan, zs, query)
17841814

17851815
os.utime(filename, ns=(timestamp, timestamp))
1786-
log.info("Created: %s", shlex.quote(filename))
1816+
if not getattr(query, 'quiet', False):
1817+
log.info("Created: %s", shlex.quote(filename))
17871818
if log.isEnabledFor(logging.DEBUG):
17881819
with open(filename, "rb") as f:
17891820
log.info("Base64sha256: %s", source_code_hash(f.read()))

package.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ data "external" "archive_prepare" {
4040
)
4141

4242
recreate_missing_package = var.recreate_missing_package
43+
quiet = var.quiet_archive_local_exec
4344
}
4445
}
4546

0 commit comments

Comments
 (0)