aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.58k stars 479 forks source link

package/deploy-serverless is packaging AspNetCore dependencies (Amazon.Lambda.Tools v2.0.1) #201

Closed Shockolate closed 6 years ago

Shockolate commented 6 years ago

Hello, As outlined in the re:Invent video, it is my understanding that the lambda compute substrate has the AspNetCore package stored locally. The demo shows a small deployment package with no AspNetCore dependencies.

However, when using the latest tools version for .NET Core 2.0 support, I notice that the AspNetCore dependencies are being packaged, causing a package 10x the size in the video. Is this a bug, or a feature yet to be released?

It is crucial to minimize deployment package size to minimize cold-start time.

EDIT: I am using the exact same NuGet packages and versions as defined in the WebAPI Blueprint.

EDIT2: How I'm calling those CLI tools. Excuse the Ruby-isms: Package:

  Dir.chdir(File.join(SRC_DIR, APPLICATION_NAME)) do
    cmd = 'dotnet lambda package ' \
    '--configuration Release ' \
    '--framework netcoreapp2.0 ' \
    "--output_package #{File.join(PACKAGE_DIR, 'deployment_package.zip')} " \
    '/maxcpucount:1'
    puts "Executing command: #{cmd}"
    raise 'Error packaging.' unless system(cmd)
  end

Deployment

  Dir.chdir(File.join(SRC_DIR, APPLICATION_NAME)) do
    cmd = 'dotnet lambda deploy-serverless ' \
    "--region #{region[:region]} " \
    '--configuration Release ' \
    '--framework netcoreapp2.0 ' \
    "--package #{File.join(PACKAGE_DIR, 'deployment_package.zip')} " \
    "--s3-bucket <BucketNamePrefix>-#{region[:region]} " \
    '--s3-prefix DeploymentPackages/ ' \
    "--template #{File.join(ROOT, 'serverless.infrastructure.json')} " \
    '--template-parameters ' \
      "ServiceNameParameter=#{APPLICATION_NAME};" \
      "EnvironmentParameter=#{environment} " \
    "--stack-name #{stack_name} " \
    '--stack-wait true '
    puts "Executing command: #{cmd}"
    raise 'Error deploying to environment!' unless system(cmd)
  end
normj commented 6 years ago

Is this a Web API project created before updating Visual Studio to the latest toolkit? Can you share the csproj file?

Shockolate commented 6 years ago

The original solution was a Web API Project (with Tests) that was created with the blueprint from the previous VS Toolkit. When the latest version was released, I downloaded & installed it, and created a parallel project with the same Blueprint.

I then made changes to the original solution:

Thanks for your attention, @normj

My .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="2.0.0" />
    <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.4" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.0.1" />
  </ItemGroup>

</Project>
normj commented 6 years ago

Ah I forgot in the .NET Core 1.0 project template I set the Sdk at the root to Microsoft.NET.Sdk. I did that to prevent the refs folder being generated in the package bundle. Probably wasn't my best idea :)

Can you update

<Project Sdk="Microsoft.NET.Sdk">

to

<Project Sdk="Microsoft.NET.Sdk.Web">

I'm finishing up my blog post on Serverless ASP.NET Core, I'll add a note about this migration detail.

Shockolate commented 6 years ago

That did the trick! Thanks, @normj