// Package main implements the IntegraCI serverless-deploy Dagger module. // // One-stop module for deploying functions to: // - AWS Lambda (zip & container image) // - GCP Cloud Function (gen2) // - Azure Function (zip) // // All providers share a common ServerlessResult contract so the IntegraCI // ServerlessDeployWorkflow can render uniform UI/audit data. // // Usage: // // dagger call lambda --source ./build.zip --name my-fn --runtime python3.12 --role-arn $ROLE // dagger call cloud-function --source ./build.zip --name my-fn --runtime python312 --region us-central1 package main import ( "context" "dagger/serverless-deploy/internal/dagger" "fmt" "strings" ) // ServerlessDeploy is the multi-provider FaaS deploy module. type ServerlessDeploy struct{} // ServerlessResult summarises a single function deployment. type ServerlessResult struct { Provider string `json:"provider"` FunctionName string `json:"function_name"` Region string `json:"region"` Version string `json:"version,omitempty"` InvokeURL string `json:"invoke_url,omitempty"` Output string `json:"output,omitempty"` Success bool `json:"success"` } // Lambda packages and deploys a function to AWS Lambda using the AWS CLI. func (m *ServerlessDeploy) Lambda( ctx context.Context, // Build artifact (.zip with handler). source *dagger.File, // Function name in AWS. name string, // Runtime e.g. python3.12 / nodejs20.x / provided.al2023. runtime string, // IAM role ARN with execution permissions. roleARN string, // AWS region. // +optional // +default="us-east-1" region string, // Handler entry point. // +optional // +default="app.handler" handler string, // AWS access key ID secret. awsAccessKey *dagger.Secret, // AWS secret access key secret. awsSecretKey *dagger.Secret, // Function memory in MB. // +optional // +default=128 memory int, // Timeout seconds. // +optional // +default=10 timeout int, ) (*ServerlessResult, error) { c := dag.Container(). From("amazon/aws-cli:2.17.0"). WithMountedFile("/work/fn.zip", source). WithSecretVariable("AWS_ACCESS_KEY_ID", awsAccessKey). WithSecretVariable("AWS_SECRET_ACCESS_KEY", awsSecretKey). WithEnvVariable("AWS_REGION", region) upsert := strings.Join([]string{ fmt.Sprintf("aws lambda get-function --function-name %s >/dev/null 2>&1", name), "&&", fmt.Sprintf("aws lambda update-function-code --function-name %s --zip-file fileb:///work/fn.zip", name), "||", strings.Join([]string{ "aws lambda create-function", "--function-name " + name, "--runtime " + runtime, "--role " + roleARN, "--handler " + handler, fmt.Sprintf("--memory-size %d", memory), fmt.Sprintf("--timeout %d", timeout), "--zip-file fileb:///work/fn.zip", }, " "), }, " ") c = c.WithExec([]string{"sh", "-c", upsert + " || true"}) out, _ := c.Stdout(ctx) return &ServerlessResult{ Provider: "aws_lambda", FunctionName: name, Region: region, Output: out, Success: strings.Contains(out, `"FunctionArn"`) || !strings.Contains(strings.ToLower(out), "error"), }, nil } // CloudFunction deploys a Gen2 Cloud Function via gcloud. func (m *ServerlessDeploy) CloudFunction( ctx context.Context, source *dagger.Directory, name string, // Runtime e.g. python312 / nodejs20. runtime string, // HTTP trigger entry point. entrypoint string, region string, // Service-account JSON key. serviceAccount *dagger.Secret, // +optional // +default="256MB" memory string, // +optional // +default=60 timeout int, ) (*ServerlessResult, error) { c := dag.Container(). From("google/cloud-sdk:slim"). WithMountedDirectory("/src", source). WithMountedSecret("/work/sa.json", serviceAccount). WithExec([]string{"sh", "-c", "gcloud auth activate-service-account --key-file=/work/sa.json"}). WithExec([]string{"sh", "-c", strings.Join([]string{ "cd /src && gcloud functions deploy " + name, "--gen2", "--runtime=" + runtime, "--entry-point=" + entrypoint, "--region=" + region, "--memory=" + memory, fmt.Sprintf("--timeout=%ds", timeout), "--trigger-http", "--allow-unauthenticated", "--source=.", "|| true", }, " ")}) out, _ := c.Stdout(ctx) return &ServerlessResult{ Provider: "gcp_cloud_function", FunctionName: name, Region: region, Output: out, Success: strings.Contains(out, "https://"), }, nil } // AzureFunction deploys via az func. func (m *ServerlessDeploy) AzureFunction( ctx context.Context, source *dagger.Directory, name string, resourceGroup string, region string, // Azure SP credentials JSON. credentialsJSON *dagger.Secret, ) (*ServerlessResult, error) { c := dag.Container(). From("mcr.microsoft.com/azure-cli:2.67.0"). WithMountedDirectory("/src", source). WithMountedSecret("/work/sp.json", credentialsJSON). WithExec([]string{"sh", "-c", `AZ=$(cat /work/sp.json) && ` + `az login --service-principal -u $(echo $AZ | jq -r .clientId) -p $(echo $AZ | jq -r .clientSecret) --tenant $(echo $AZ | jq -r .tenantId) || true`, }). WithExec([]string{"sh", "-c", strings.Join([]string{ "cd /src && func azure functionapp publish " + name, "--resource-group " + resourceGroup, "|| true", }, " ")}) out, _ := c.Stdout(ctx) return &ServerlessResult{ Provider: "azure_function", FunctionName: name, Region: region, Output: out, Success: !strings.Contains(strings.ToLower(out), "error"), }, nil }