terraform-aws-modules / terraform-aws-lambda

Terraform module, which takes care of a lot of AWS Lambda/serverless tasks (build dependencies, packages, updates, deployments) in countless combinations 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws
Apache License 2.0
885 stars 658 forks source link

Docker-build example fails: failed to solve with frontend dockerfile.v0 #576

Open juliangrueber opened 1 month ago

juliangrueber commented 1 month ago

Versions

Reproduction Code [Required]

Following example

(https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/4f77bfcd4083857d6aefccb5a891ee00ffd559b0/examples/container-image/main.tf#L111)

The way I use the module:


terraform {
  required_version = ">= 1.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.32"
    }
    docker = {
      source  = "kreuzwerker/docker"
      version = ">= 3.0"
    }
  }
}

provider "aws" {
  region = "eu-west-1"

}

provider "docker" {
  registry_auth {
    address  = format("%v.dkr.ecr.%v.amazonaws.com", data.aws_caller_identity.current.account_id, data.aws_region.current.name)
    username = data.aws_ecr_authorization_token.token.user_name
    password = data.aws_ecr_authorization_token.token.password
  }
}

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_ecr_authorization_token" "token" {}

locals {
  source_path   = "${path.root}/../python/src/handlers/data_ingestion_processor"
  path_include  = ["**"]
  path_exclude  = ["**/__pycache__/**"]
  files_include = setunion([for f in local.path_include : fileset(local.source_path, f)]...)
  files_exclude = setunion([for f in local.path_exclude : fileset(local.source_path, f)]...)
  files         = sort(setsubtract(local.files_include, local.files_exclude))

  dir_sha = sha1(join("", [for f in local.files : filesha1("${local.source_path}/${f}")]))
}

module "docker_build" {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git//modules/docker-build?ref=v7.4.0"

  create_ecr_repo = true
  ecr_repo        = "aws-sample"
  ecr_repo_lifecycle_policy = jsonencode({
    "rules" : [
      {
        "rulePriority" : 1,
        "description" : "Keep only the last 2 images",
        "selection" : {
          "tagStatus" : "any",
          "countType" : "imageCountMoreThan",
          "countNumber" : 2
        },
        "action" : {
          "type" : "expire"
        }
      }
    ]
  })
  use_image_tag = true

  source_path = local.source_path
  platform    = "linux/amd64"

  triggers = {
    dir_sha = local.dir_sha
  }

}

Steps to reproduce the behavior:

terraform init
terraform apply 

Expected behavior

Build docker image and push it to the ECR repo which is build part of the module

Actual behavior

│ Error: failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile parse error line 1: unknown instruction: PAXHEADERS.0/DOCKERFILE0000000000000000000000000000002000000000000012203
│ 
│ 
│ 
│   with module.docker_build.docker_image.this,
│   on .terraform/modules/docker_build/modules/docker-build/main.tf line 12, in resource "docker_image" "this":
│   12: resource "docker_image" "this" {

Terminal Output Screenshot(s)

see above

Additional context

Dockerfile

FROM public.ecr.aws/lambda/python:3.12

COPY handler.py .
CMD ["handler.lambda_handler"]

Building the docker in the directory of the Dockerfile manually from

output of manual docker build:

❯ docker build -t image-test  ./../python/src/handlers/data_ingestion_processor
[+] Building 0.1s (5/5) FINISHED                                                                                                                                                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                                                                            0.0s
 => => transferring dockerfile: 32B                                                                                                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                                                                                                                 0.0s
 => [internal] load build context                                                                                                                                                                                                               0.0s
 => => transferring context: 32B                                                                                                                                                                                                                0.0s
 => CACHED [1/1] COPY handler.py .                                                                                                                                                                                                              0.0s
 => exporting to image                                                                                                                                                                                                                          0.0s
 => => exporting layers                                                                                                                                                                                                                         0.0s
 => => writing image sha256:7e516ba7ae1d195058061dd463df2137ac042278ca2f924c66e116b737dbc793                                                                                                                                                    0.0s
 => => naming to docker.io/library/image-test  
github-actions[bot] commented 1 day ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days