Packer Windows EC2 Out Of Memory building with Dotnet / MSBuild

We are using packer to build an AMI of a Windows Server 2022 Server with installation of Unreal Engine 5 source version.

We installed the prerequisites for building the source engine using PowerShell scripts, which includes .Net, Visual Studio etc.

The problem we encounter consistently is either out of memory or fatal error C1060: compiler is out of heap space when we try to build the source engine with dotnet but the specification of the EC2 instance (m7i-flex.2xlarge) we are using is quite high with 128 GB of RAM.

A test we did is use packer to build an AMI that only install the prerequisites and clone the UE5 repository. We are then able to build the source engine when RDP directly.

Any help and pointers would be very much appreciated, please let me know if more info is needed. Thanks!