You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自动打包Visual Studio AWS Serverless项目并拆分构建发布流水线?

Can I automate packaging an AWS Serverless app with dotnet lambda to split build/release pipelines?

Absolutely! You can split your build and release pipelines by automating the packaging of your entire AWS Serverless application—this is a fantastic practice for keeping your CI/CD stages clean, decoupled, and easier to debug. Here's a step-by-step guide using the dotnet lambda CLI and AWS SAM (Serverless Application Model) tools, which works seamlessly with VSTS (now Azure DevOps) or any scriptable environment:

Step 1: Package Individual Lambda Functions

Start by packaging each Lambda function in your Serverless app separately using the dotnet lambda package command you already discovered. This generates deployment-ready .zip files for each function, which we’ll later assemble into the full Serverless app package.

Assume your project has a structure like this:

MyServerlessApp/
├─ UserManagementFunction/
│  └─ UserManagementFunction.csproj
├─ OrderProcessingFunction/
│  └─ OrderProcessingFunction.csproj
└─ template.yml (your SAM template)

Create a script (Bash or PowerShell) to loop through each function and package it:

Bash Script Example

# Create a dist directory to store all packaged zips
mkdir -p dist

# Package UserManagementFunction
cd UserManagementFunction
dotnet lambda package --configuration Release --output-package ../dist/user-management.zip
cd ..

# Package OrderProcessingFunction
cd OrderProcessingFunction
dotnet lambda package --configuration Release --output-package ../dist/order-processing.zip
cd ..

PowerShell Script Example

# Create dist directory if it doesn't exist
if (-not (Test-Path "dist")) {
    New-Item -ItemType Directory -Path "dist"
}

# Package UserManagementFunction
Set-Location UserManagementFunction
dotnet lambda package --configuration Release --output-package ../dist/user-management.zip
Set-Location ..

# Package OrderProcessingFunction
Set-Location OrderProcessingFunction
dotnet lambda package --configuration Release --output-package ../dist/order-processing.zip
Set-Location ..

Step 2: Update Your SAM Template

Ensure your SAM template.yml references the packaged .zip files in the CodeUri field for each function. This tells AWS where to find the code for each Lambda when deploying:

Resources:
  UserManagementFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dist/user-management.zip
      Handler: UserManagementFunction::UserManagementFunction.Function::FunctionHandler
      Runtime: dotnet6
      # ... additional properties (IAM roles, triggers, etc.)

  OrderProcessingFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dist/order-processing.zip
      Handler: OrderProcessingFunction::OrderProcessingFunction.Function::FunctionHandler
      Runtime: dotnet6
      # ... additional properties

Step 3: Package the Full Serverless Application

To turn your local function zips and SAM template into a deployable artifact (ready for the release pipeline), use the AWS SAM CLI’s sam package command. This does two key things:

  1. Uploads your packaged function zips to an S3 bucket (you’ll need to create one beforehand)
  2. Generates a new "packaged" SAM template where CodeUri paths are replaced with the S3 URLs of your zips

Run this from your project root:

aws sam package \
  --template-file template.yml \
  --s3-bucket your-deployment-bucket-name \
  --output-template-file dist/packaged-template.yml

Step 4: Integrate with Your CI/CD Pipeline

Build Pipeline (VSTS/Azure DevOps)

  • Add a step to run your packaging script.
  • Publish the dist/ directory (containing all function zips and packaged-template.yml) as build artifacts. This keeps your build stage focused solely on creating deployable assets.

Release Pipeline (VSTS/Azure DevOps)

  • Retrieve the build artifacts from the previous stage.
  • Deploy the application using the packaged-template.yml with the AWS SAM CLI:
    aws sam deploy \
      --template-file dist/packaged-template.yml \
      --stack-name your-serverless-stack-name \
      --capabilities CAPABILITY_IAM
    

Quick Note on dotnet lambda deploy-serverless

While this command can deploy your entire app in one go, it combines packaging and deployment—so it’s not ideal for splitting your pipeline. The approach above gives you full control over separating build (packaging) and release (deployment) stages.

Key Prerequisites

  • Install Amazon.Lambda.Tools via dotnet tool install -g Amazon.Lambda.Tools
  • Install the AWS SAM CLI
  • Configure AWS credentials in your CI/CD environment (for VSTS, use the AWS Toolkit extension to manage credentials securely)

内容的提问来源于stack exchange,提问作者Kevin Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:25:53