terraform-aws-modules/terraform-aws-lambda

Adding platform option in docker-build module

GonzalezAndrew opened this issue ยท 6 comments

Is your request related to a new offering from AWS?

Is this functionality available in the AWS provider for Terraform? See CHANGELOG.md, too.

  • No ๐Ÿ›‘: This feature request is related to the docker provider version 2.22.0.

Is your request related to a problem? Please describe.

When building on M1 Mac, the option to build docker images in other platforms would be a nice feature. Currently when using the docker-build module, the default platform on M1 Mac will be arm but I would like to build x86_64 architecture.

Describe the solution you'd like.

Change the docker_registry_image block in docker-build module to include platform argument:

resource "docker_registry_image" "this" {
  name = local.ecr_image_name

  build {
    context    = var.source_path
    dockerfile = var.docker_file_path
    build_args = var.build_args
    platform = var.platform
  }

  keep_remotely = var.keep_remotely
}

Describe alternatives you've considered.

Setting the AWS Lambda architecture to arm64.

Additional context

This can be closed out since I have found a work around which has fixed the issue I was dealing with.

For those who find this issue and want a work around here is what I did. First in your Dockerfile, ensure you are adding the --platform=linux/x86_64 with the FROM. Below is an example Dockerfile:

FROM --platform=linux/x86_64 public.ecr.aws/lambda/python:3.9

# ensure libs are up to date
RUN : \
    && yum -y update --security \
    && yum clean all \
    && rm -rf /var/cache/yum \
    :

WORKDIR ${LAMBDA_TASK_ROOT}
COPY ./*.py ${LAMBDA_TASK_ROOT}
COPY requirements.txt ${LAMBDA_TASK_ROOT}

RUN : \
    && python3.9 -m pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}" \
    &&:

CMD ["app.lambda_handler"]

Second, in the module block where you are defining the lambda function, add the architectures = ["x86_64"] input. For example:

module "lambda_function" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "3.3.1"

  # basic config
  function_name  = local.deployment_name
  description    = "A lambda function."
  create_package = false
  publish        = true 

  # architecture config
  memory_size = 2000
  timeout     = 300

  # container config
  image_uri                      = module.image_build.image_uri
  package_type                   = "Image"
  architectures                  = ["x86_64"]
  
  # role and policy config
  attach_policy_statements = true
  policy_statements = {
    WriteCloudWatchLogs = {
      effect    = "Allow",
      actions   = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"],
      resources = ["*"]
    },
    KMSDecrypt = {
      effect    = "Allow",
      actions   = ["kms:Decrypt"],
      resources = ["*"]
    }
  }

}

Again, I would like to think the maintainers for this amazing module! You all are awesome! :)

You are welcome, @GonzalezAndrew !

Thanks for sharing the solution with us! I would be looking for it myself in a few days because I need it :)

This solution doesn't appear to be working for me. I made both changes, would you mind posting a full example (if possible)?

Hi @micah5

I have just tried out my work around again and can confirm that this solution works. I can also note that I have deployed several lambda functions using the work around I described above.

Here is a link to the example I just ran which works for me: example

Below are some screenshots of the lambda function using a different architecture:
aws
container

Hi @micah5

I have just tried out my work around again and can confirm that this solution works. I can also note that I have deployed several lambda functions using the work around I described above.

Here is a link to the example I just ran which works for me: example

Below are some screenshots of the lambda function using a different architecture: aws container

Thank you for the help- I was able to get it working by using your example

I'm going to lock this issue because it has been closed for 30 days โณ. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.