title: Using a portable Makefile for AWS Lambda
body: <p>I'm using a Makefile for building a lambda extension. It works fine when I run a <code>sam build</code> command from Azure DevOps using Ubuntu. However, sometimes I want to build from my PC and the Makefile doesn't work because commands are different. I suppose I could setup command aliases for dos but that seems treacherous. I'd prefer to use something like <a href="https://stackoverflow.com/questions/714100/os-detecting-makefile">OS detecting makefile</a> but when I tried that it didn't work out.</p>
  <p>Here's the pertinent snippet from my template:</p>
  <pre><code>  TokenCacheLayer:
      Type: AWS::Serverless::LayerVersion
      Metadata:
        BuildMethod: makefile
      Properties:
        LayerName: !Ref TokenCacheLayerName
        Description: AWS Lambda extension to cache tokens aquired from an external API.
        ContentUri: ./token-cache
        LicenseInfo: MIT-0
        CompatibleRuntimes:
          - nodejs12.x
        RetentionPolicy: Retain
  </code></pre>
  <p>Here's my Makefile:</p>
  <pre><code>build-TokenCacheLayer:
      cp -R extensions &quot;$(ARTIFACTS_DIR)&quot;
      cp -R token-cache-extension &quot;$(ARTIFACTS_DIR)&quot;
  </code></pre>
  <p>Here's the 2 ways I've tried altering to make it OS independent:</p>
  <pre><code>build-TokenCacheLayer:
      ifeq ($(OS),Windows_NT)
          xcopy extensions &quot;$(ARTIFACTS_DIR)\extensions&quot;
          xcopy token-cache-extension &quot;$(ARTIFACTS_DIR)&quot;
      else
          cp -R extensions &quot;$(ARTIFACTS_DIR)&quot;
          cp -R token-cache-extension &quot;$(ARTIFACTS_DIR)&quot;
      endif
  </code></pre>
  <p>and</p>
  <pre><code>ifeq ($(OS),Windows_NT)
      build-TokenCacheLayer:
          xcopy extensions &quot;$(ARTIFACTS_DIR)\extensions&quot;
          xcopy token-cache-extension &quot;$(ARTIFACTS_DIR)&quot;
  else
      build-TokenCacheLayer:
          cp -R extensions &quot;$(ARTIFACTS_DIR)&quot;
          cp -R token-cache-extension &quot;$(ARTIFACTS_DIR)&quot;
  endif
  </code></pre>
  <p>The first way I got the error:</p>
  <pre><code>Error: CustomMakeBuilder:MakeBuild - Make Failed: process_begin: CreateProcess(NULL, cp -R extensions D:\Development\AWS.Integration\buildpro\layers\sam-build\TokenCacheLayer, ...) failed.
  make (e=2): The system cannot find the file specified.
  make: *** [D:\Development\AWS.Integration\buildpro\layers\token-cache\Makefile:6: build-TokenCacheLayer] Error 2
  </code></pre>
  <p>The second gave me this:</p>
  <pre><code>Error: CustomMakeBuilder:MakeBuild - Make Failed: D:\Development\AWS.Integration\buildpro\layers\token-cache\Makefile:2: *** recipe commences before first target.  Stop.
  </code></pre>
  <h2 id="update-d6py">Update</h2>
  <p>I went back and tried the first way again and while I'm not getting an error it doesn't copy the files either.</p>