Skip to content

Commit f5f86b5

Browse files
feat: Add pip_tmp_dir to allow setting the location of the pip temporary directory (#230)
1 parent 2227070 commit f5f86b5

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ source_path = [
391391
}, {
392392
path = "src/python3.8-app1",
393393
pip_requirements = true,
394+
pip_tmp_dir = "/tmp/dir/location"
394395
prefix_in_zip = "foo/bar1",
395396
}, {
396397
path = "src/python3.8-app2",
@@ -442,6 +443,7 @@ Few notes:
442443
- `commands` - List of commands to run. If specified, this argument overrides `pip_requirements`.
443444
- `:zip [source] [destination]` is a special command which creates content of current working directory (first argument) and places it inside of path (second argument).
444445
- `pip_requirements` - Controls whether to execute `pip install`. Set to `false` to disable this feature, `true` to run `pip install` with `requirements.txt` found in `path`. Or set to another filename which you want to use instead.
446+
- `pip_tmp_dir` - Set the base directory to make the temporary directory for pip installs. Can be useful for Docker in Docker builds.
445447
- `prefix_in_zip` - If specified, will be used as a prefix inside zip-archive. By default, everything installs into the root of zip-archive.
446448

447449
### Building in Docker

examples/build-package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Note that this example may create resources which cost money. Run `terraform des
3737
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | n/a |
3838
| <a name="module_lambda_layer_pip_requirements"></a> [lambda\_layer\_pip\_requirements](#module\_lambda\_layer\_pip\_requirements) | ../.. | n/a |
3939
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
40+
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
4041
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |
4142
| <a name="module_package_file"></a> [package\_file](#module\_package\_file) | ../../ | n/a |
4243
| <a name="module_package_file_with_pip_requirements"></a> [package\_file\_with\_pip\_requirements](#module\_package\_file\_with\_pip\_requirements) | ../../ | n/a |

examples/build-package/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ module "package_dir" {
2727
source_path = "${path.module}/../fixtures/python3.8-app1"
2828
}
2929

30+
# Create zip-archive of a single directory where "pip install" will also be executed (default for python runtime) and set temporary directory for pip install
31+
module "package_dir_pip_dir" {
32+
source = "../../"
33+
34+
create_function = false
35+
36+
runtime = "python3.8"
37+
source_path = [{
38+
path = "${path.module}/../fixtures/python3.8-app1"
39+
pip_tmp_dir = "${path.cwd}/../fixtures"
40+
pip_requirements = "${path.module}/../fixtures/python3.8-app1/requirements.txt"
41+
}]
42+
}
43+
3044
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
3145
module "package_dir_without_pip_install" {
3246
source = "../../"

package.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ def cd(path, silent=False):
118118

119119

120120
@contextmanager
121-
def tempdir():
121+
def tempdir(dir=None):
122122
"""Creates a temporary directory and then deletes it afterwards."""
123123
prefix = 'terraform-aws-lambda-'
124-
path = tempfile.mkdtemp(prefix=prefix)
124+
path = tempfile.mkdtemp(prefix=prefix, dir=dir)
125125
cmd_log.info('mktemp -d %sXXXXXXXX # %s', prefix, shlex.quote(path))
126126
try:
127127
yield path
@@ -648,7 +648,7 @@ def plan(self, source_path, query):
648648
step = lambda *x: build_plan.append(x)
649649
hash = source_paths.append
650650

651-
def pip_requirements_step(path, prefix=None, required=False):
651+
def pip_requirements_step(path, prefix=None, required=False, tmp_dir=None):
652652
requirements = path
653653
if os.path.isdir(path):
654654
requirements = os.path.join(path, 'requirements.txt')
@@ -657,7 +657,7 @@ def pip_requirements_step(path, prefix=None, required=False):
657657
raise RuntimeError(
658658
'File not found: {}'.format(requirements))
659659
else:
660-
step('pip', runtime, requirements, prefix)
660+
step('pip', runtime, requirements, prefix, tmp_dir)
661661
hash(requirements)
662662

663663
def commands_step(path, commands):
@@ -735,10 +735,10 @@ def commands_step(path, commands):
735735

736736
if pip_requirements and runtime.startswith('python'):
737737
if isinstance(pip_requirements, bool) and path:
738-
pip_requirements_step(path, prefix, required=True)
738+
pip_requirements_step(path, prefix, required=True, tmp_dir=claim.get('pip_tmp_dir'))
739739
else:
740740
pip_requirements_step(pip_requirements, prefix,
741-
required=True)
741+
required=True, tmp_dir=claim.get('pip_tmp_dir'))
742742

743743
if path:
744744
step('zip', path, prefix)
@@ -784,8 +784,8 @@ def execute(self, build_plan, zip_stream, query):
784784
else:
785785
zs.write_file(source_path, prefix=prefix, timestamp=ts)
786786
elif cmd == 'pip':
787-
runtime, pip_requirements, prefix = action[1:]
788-
with install_pip_requirements(query, pip_requirements) as rd:
787+
runtime, pip_requirements, prefix, tmp_dir = action[1:]
788+
with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
789789
if rd:
790790
if pf:
791791
self._zip_write_with_filter(zs, pf, rd, prefix,
@@ -825,7 +825,7 @@ def _zip_write_with_filter(zip_stream, path_filter, source_path, prefix,
825825

826826

827827
@contextmanager
828-
def install_pip_requirements(query, requirements_file):
828+
def install_pip_requirements(query, requirements_file, tmp_dir):
829829
# TODO:
830830
# 1. Emit files instead of temp_dir
831831

@@ -836,6 +836,7 @@ def install_pip_requirements(query, requirements_file):
836836
runtime = query.runtime
837837
artifacts_dir = query.artifacts_dir
838838
docker = query.docker
839+
temp_dir = query.temp_dir
839840
docker_image_tag_id = None
840841

841842
if docker:
@@ -868,7 +869,7 @@ def install_pip_requirements(query, requirements_file):
868869
working_dir = os.getcwd()
869870

870871
log.info('Installing python requirements: %s', requirements_file)
871-
with tempdir() as temp_dir:
872+
with tempdir(tmp_dir) as temp_dir:
872873
requirements_filename = os.path.basename(requirements_file)
873874
target_file = os.path.join(temp_dir, requirements_filename)
874875
shutil.copyfile(requirements_file, target_file)

0 commit comments

Comments
 (0)