求助:基于Jenkins实现Lambda上.NET Core微服务的打包与持续部署
Hey there! Let's work through getting your .NET Lambda deployment sorted from Jenkins—you’re already halfway there with the build done, so we’ll focus on the zip packaging and Lambda push steps.
First, make sure your Jenkins build is generating the right artifacts for Lambda. For .NET Core/.NET 6+ Lambda functions, you need the output from dotnet publish, not just the compiled binaries.
In your Jenkins build step, ensure you’re running a publish command like this:
dotnet publish -c Release -o ./publish -r linux-x64 --self-contained false
-r linux-x64: Targets Lambda’s default Linux runtime (match this to your Lambda’s configured runtime)--self-contained false: Uses Lambda’s pre-installed .NET runtime (smaller package size; usetrueonly if you need a custom runtime)-o ./publish: Drops all required files into apublishfolder—this is what we’ll package.
Lambda expects a zip where the root contains your function’s DLL, runtime config, and any dependencies—don’t zip the publish folder itself, zip its contents.
For Linux/macOS Jenkins agents:
Add a shell build step to zip the contents:
cd ./publish zip -r lambda-deploy.zip ./*
For Windows Jenkins agents:
Use a PowerShell build step instead:
Compress-Archive -Path ./publish/* -DestinationPath lambda-deploy.zip -Force
The -Force flag overwrites any existing zip from previous builds.
You’ve got two reliable options here—pick whichever fits your workflow.
Option 1: Use AWS CLI in Jenkins
First, set up AWS credentials in Jenkins:
- Go to Jenkins > Manage Jenkins > Manage Credentials
- Add a "AWS Credentials" entry with your access key and secret key (ensure this IAM user has the
lambda:UpdateFunctionCodepermission)
Then add a shell/PowerShell build step to run the update command:
# Linux/macOS aws lambda update-function-code --function-name YOUR_LAMBDA_NAME --zip-file fileb://./publish/lambda-deploy.zip
# Windows aws lambda update-function-code --function-name YOUR_LAMBDA_NAME --zip-file "fileb://./publish/lambda-deploy.zip"
Replace YOUR_LAMBDA_NAME with your actual Lambda function name. If your zip is larger than 50MB, upload it to S3 first and use the --s3-bucket and --s3-key parameters instead of --zip-file.
Option 2: Use the Jenkins AWS Lambda Plugin
- Install the AWS Lambda Plugin from Jenkins’ plugin marketplace
- Add a "Deploy to AWS Lambda" post-build step:
- Select your AWS credentials from the dropdown
- Enter your Lambda function name
- Set the "Zip File Path" to
./publish/lambda-deploy.zip - Optional: Add a version description for tracking deployments
- Wrong Zip Structure: If Lambda throws "invalid runtime" or entry point errors, unzip your package—you should see your
.dllfile directly at the root, not nested inside apublishfolder. - Permission Errors: Double-check your IAM user has
lambda:UpdateFunctionCode(andlambda:PublishVersionif you’re creating versions). - Runtime Mismatch: Ensure your
dotnet publishruntime (-r) matches the runtime configured in your Lambda function (e.g.,dotnet6,dotnet8). - Large Packages: If your zip exceeds 50MB, upload it to S3 first and use the S3 parameters in the AWS CLI command.
内容的提问来源于stack exchange,提问作者Suleman Mehmood

