Skip to content

Default behavior of tempfile.mkdtemp(suffix=None, prefix=None, dir=None) changed in Python 3.12 #592

@sassdavid

Description

@sassdavid

Description

We use a module to create Lambda layers built in Docker. Recently, I tried changing the pip_tmp_dir to a path relative to the module (${path.module}) to avoid constant discrepancies in the plan between my local environment and our CI/CD pipeline. I noticed differences between the local run and the CI/CD pipeline run.

On my computer, the Python version is 3.12, but our pipelines use 3.10. I found that there was a change in Python 3.12: https://docs.python.org/3/library/tempfile.html#tempfile.mkdtemp

It states: "Changed in version 3.12: mkdtemp() now always returns an absolute path, even if dir is relative."

This change is beneficial for us as it allows the use of a relative path for pip_tmp_dir, thus avoiding constant changes in the plan due to ${path.cwd} differences.

Could you suggest a way to achieve the same behavior with older Python versions? Additionally, is there any concern about always returning the absolute path in this file: https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/master/package.py#L123?

If your request is for a new feature, please use the Feature request template.

  • ✋ I have searched the open/closed issues and my issue is not listed.

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

  • Module version [Required]: 7.7.0

  • Terraform version: 1.9.2

  • Provider version(s):
    • provider registry.terraform.io/hashicorp/aws v5.57.0
    • provider registry.terraform.io/hashicorp/external v2.3.3
    • provider registry.terraform.io/hashicorp/local v2.5.1
    • provider registry.terraform.io/hashicorp/null v3.2.2

Reproduction Code [Required]

module "s3fileloader_lambda_function_layer" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "7.7.0"

  create = local.create_s3fileloader

  create_function = false
  create_package  = true
  create_layer    = true

  layer_name  = "${local.lambda_name}-pip-requirements"
  description = "Layer for supporting 's3fileloader' lambda function"

  compatible_runtimes = ["python3.12"]
  runtime             = "python3.12"
  artifacts_dir       = "${path.root}/builds/lambda_layers/"

  build_in_docker = true
  docker_additional_options = [
    "--network", "host"
  ]

  source_path = [
    {
      path             = "${path.module}/s3fileloader"
      pip_tmp_dir      = "${path.module}/s3fileloader"
      prefix_in_zip    = "python"
      pip_requirements = true
    }
  ]

  store_on_s3               = true
  s3_prefix                 = "lambda_layers/"
  s3_object_storage_class   = "STANDARD"
  s3_bucket                 = module.s3fileloader_lambda_function_sources.s3_bucket_id
  s3_server_side_encryption = "aws:kms"
  s3_object_tags            = local.tags

  recreate_missing_package     = false
  trigger_on_package_timestamp = false

  tags = local.tags
}

Steps to reproduce the behavior:

Run the module definition above with python3.10 and python3.12.

Expected behavior

The Lambda layer was created correctly.

Actual behavior

Python 3.10:

The script fails here, presumably due to the relative path: https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/master/package.py#L1108

[2024-07-11T09:21:27.996Z] module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): FileNotFoundError: [Errno 2] No such file or directory: './s3fileloader/terraform-aws-lambda-4jrqazb4/requirements.txt'

[2024-07-11T09:21:21.436Z] module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): zip: creating './builds/lambda_layers/50440c451250a62316026ec292815a320d9bbca0100cf05b80febf54fd137adc.zip' archive
[2024-07-11T09:21:21.436Z] module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): Installing python requirements: ./s3fileloader/requirements.txt
[2024-07-11T09:21:21.436Z] module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): > mktemp -d terraform-aws-lambda-XXXXXXXX # ./s3fileloader/terraform-aws-lambda-4jrqazb4
[2024-07-11T09:21:21.436Z] module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): > cd ./s3fileloader/terraform-aws-lambda-4jrqazb4

Python 3.12:

The script does not fail.

module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): zip: creating './builds/lambda_layers/50440c451250a62316026ec292815a320d9bbca0100cf05b80febf54fd137adc.zip' archive
module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): Installing python requirements: ./s3fileloader/requirements.txt
module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): > mktemp -d terraform-aws-lambda-XXXXXXXX # /home/user/src/bitbucket/company/Terraform-AWS/tf-scripts/.terragrunt-cache/J5cPGP4LIGaljTfIVUC_seYg5ek/37ujJTU5vwGX_XeoGQN7WeHvNDI/s3fileloader/terraform-aws-lambda-79x0pniy
module.s3fileloader_lambda_function_layer.null_resource.archive[0] (local-exec): > cd /home/user/src/bitbucket/company/Terraform-AWS/tf-scripts/.terragrunt-cache/J5cPGP4LIGaljTfIVUC_seYg5ek/37ujJTU5vwGX_XeoGQN7WeHvNDI/s3fileloader/terraform-aws-lambda-79x0pniy

Terminal Output Screenshot(s)

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions