|
| 1 | +# Using the SwiftPM Plugins |
| 2 | + |
| 3 | +@Metadata { |
| 4 | + @PageKind(article) |
| 5 | + @PageColor(orange) |
| 6 | + @SupportedLanguage(swift) |
| 7 | + @PageImage(source: "lambda.png", alt: "AWS Lambda", purpose: icon) |
| 8 | +} |
| 9 | + |
| 10 | +Scaffold, build, and deploy your Lambda function with the bundled SwiftPM command plugins. |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +Swift AWS Lambda Runtime ships three SwiftPM command plugins that cover the full |
| 15 | +lifecycle of a Lambda function, from creating the project to deploying it on AWS: |
| 16 | + |
| 17 | +- **`lambda-init`** — scaffold a new Lambda function from a template. |
| 18 | +- **`lambda-build`** — compile and package your function for Amazon Linux 2023. |
| 19 | +- **`lambda-deploy`** — deploy, update, or delete your function on AWS, including |
| 20 | + its IAM role and an optional Function URL. |
| 21 | + |
| 22 | +The plugins become available as soon as your package depends on |
| 23 | +`swift-aws-lambda-runtime`. You invoke them with `swift package <plugin-name>`. |
| 24 | +Each plugin accepts `--help` to print its full list of options, and `--verbose` |
| 25 | +to produce detailed output for debugging. |
| 26 | + |
| 27 | +Because SwiftPM plugins run in a sandbox, some plugins require you to explicitly |
| 28 | +grant permissions on the command line: |
| 29 | + |
| 30 | +- `lambda-init` needs `--allow-writing-to-package-directory` to write files. |
| 31 | +- `lambda-build` needs `--allow-network-connections docker` to |
| 32 | + reach the build container. |
| 33 | +- `lambda-deploy` needs `--allow-network-connections all:443` to call the AWS APIs. |
| 34 | + |
| 35 | +> Tip: You can see a quick end-to-end walkthrough in <doc:quick-setup>. |
| 36 | +
|
| 37 | +## lambda-init |
| 38 | + |
| 39 | +`lambda-init` scaffolds a HelloWorld Lambda function into your package. By |
| 40 | +default it creates a function that receives a JSON document and responds with |
| 41 | +another JSON document. It detects your package's entry point file, backs it up |
| 42 | +with a `.bak` extension, and replaces it with the generated template. |
| 43 | + |
| 44 | +```sh |
| 45 | +swift package lambda-init --allow-writing-to-package-directory |
| 46 | +``` |
| 47 | + |
| 48 | +Pass `--with-url` to instead scaffold a function that is exposed through a |
| 49 | +Function URL. |
| 50 | + |
| 51 | +```sh |
| 52 | +swift package lambda-init --allow-writing-to-package-directory --with-url |
| 53 | +``` |
| 54 | + |
| 55 | +### Options |
| 56 | + |
| 57 | +| Option | Description | |
| 58 | +| --- | --- | |
| 59 | +| `--with-url` | Create a Lambda function exposed with a URL. | |
| 60 | +| `--allow-writing-to-package-directory` | Don't ask for permission to write files. | |
| 61 | +| `--verbose` | Produce verbose output for debugging. | |
| 62 | +| `--help` | Show help information. | |
| 63 | + |
| 64 | +## lambda-build |
| 65 | + |
| 66 | +`lambda-build` compiles your executable targets for Amazon Linux 2023 and |
| 67 | +packages them into deployment ZIP archives. The build runs inside a container, |
| 68 | +so you must have [Docker](https://docs.docker.com/desktop/install/mac-install/) |
| 69 | +(or `container`) installed and started. |
| 70 | + |
| 71 | +```sh |
| 72 | +swift package --allow-network-connections docker lambda-build |
| 73 | +``` |
| 74 | + |
| 75 | +By default the plugin builds in `release` configuration, uses the latest Swift |
| 76 | +`amazonlinux2023` base image, and strips debug symbols from the binary. The |
| 77 | +resulting archive is written under |
| 78 | +`.build/plugins/AWSLambdaBuilder/outputs/...`. |
| 79 | + |
| 80 | +> Note: Amazon Linux 2 is deprecated since June 30, 2026. The plugin defaults |
| 81 | +> to Amazon Linux 2023. |
| 82 | +
|
| 83 | +To build for a specific Swift version, or to keep debug symbols: |
| 84 | + |
| 85 | +```sh |
| 86 | +swift package --allow-network-connections docker lambda-build \ |
| 87 | + --swift-version 6.3 \ |
| 88 | + --no-strip |
| 89 | +``` |
| 90 | + |
| 91 | +### Options |
| 92 | + |
| 93 | +| Option | Description | |
| 94 | +| --- | --- | |
| 95 | +| `--output-path <path>` | The path of the binary package. (default: `.build/plugins/AWSLambdaBuilder/outputs/...`) | |
| 96 | +| `--products <list>` | The list of executable targets to build. (default: taken from `Package.swift`) | |
| 97 | +| `--configuration <name>` | The build configuration, `debug` or `release`. (default: `release`) | |
| 98 | +| `--swift-version <version>` | The Swift version to use for building. (default: latest) Cannot be combined with `--base-docker-image`. | |
| 99 | +| `--base-docker-image <name>` | The base Docker image to build with. (default: `swift:<version>-amazonlinux2023`) Cannot be combined with `--swift-version`. | |
| 100 | +| `--disable-docker-image-update` | Do not attempt to update the Docker image. | |
| 101 | +| `--cross-compile <method>` | The cross-compilation method: `docker`, `container`, `swift-static-sdk`, or `custom-sdk`. (default: `docker`) `swift-static-sdk` and `custom-sdk` are not yet supported. | |
| 102 | +| `--no-strip` | Do not strip debug symbols from the binary. | |
| 103 | +| `--verbose` | Produce verbose output for debugging. | |
| 104 | +| `--help` | Show help information. | |
| 105 | + |
| 106 | +## lambda-deploy |
| 107 | + |
| 108 | +`lambda-deploy` deploys the ZIP archive produced by `lambda-build` to AWS. It |
| 109 | +manages the full IAM role lifecycle, automatically creating a role with the |
| 110 | +`AWSLambdaBasicExecutionRole` policy when you don't provide an existing one. It |
| 111 | +also stages large archives (over 50 MB) through S3. |
| 112 | + |
| 113 | +> Be sure [to have an AWS Account](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-creating.html) |
| 114 | +> and the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) |
| 115 | +> installed and configured (`aws configure`) before deploying. |
| 116 | +
|
| 117 | +```sh |
| 118 | +swift package --allow-network-connections all:443 lambda-deploy |
| 119 | +``` |
| 120 | + |
| 121 | +On success, the plugin reports the function ARN and a ready-to-use |
| 122 | +`aws lambda invoke` command. |
| 123 | + |
| 124 | +To expose the function through a Function URL, use `--with-url`. The URL is |
| 125 | +protected with `AWS_IAM` authentication, restricted to authenticated principals |
| 126 | +in your AWS account: |
| 127 | + |
| 128 | +```sh |
| 129 | +swift package --allow-network-connections all:443 lambda-deploy --with-url |
| 130 | +``` |
| 131 | + |
| 132 | +When you're done, delete the function, its IAM role, and the Function URL (if |
| 133 | +any) with `--delete`: |
| 134 | + |
| 135 | +```sh |
| 136 | +swift package --allow-network-connections all:443 lambda-deploy --delete |
| 137 | +``` |
| 138 | + |
| 139 | +### Options |
| 140 | + |
| 141 | +| Option | Description | |
| 142 | +| --- | --- | |
| 143 | +| `--with-url` | Create a Function URL using `AWS_IAM` authentication. | |
| 144 | +| `--delete` | Delete the Lambda function, its IAM role, and Function URL (if any). | |
| 145 | +| `--region <region>` | The AWS region to deploy to. (default: resolved from AWS configuration) | |
| 146 | +| `--profile <profile-name>` | The named AWS profile to use for credentials and region. (default: default credential provider chain) | |
| 147 | +| `--iam-role <role-arn>` | The ARN of an existing IAM role for the function. (default: create a new role) | |
| 148 | +| `--input-directory <path>` | The directory containing the ZIP archive produced by `lambda-build`. (default: `.build/plugins/AWSLambdaBuilder/outputs/...`) | |
| 149 | +| `--architecture <arch>` | The function architecture, `x64` or `arm64`. (default: host architecture) | |
| 150 | +| `--products <list>` | The list of executable targets to deploy. (default: taken from `Package.swift`) | |
| 151 | +| `--verbose` | Produce verbose output for debugging. | |
| 152 | +| `--help` | Show help information. | |
| 153 | + |
| 154 | +## A typical workflow |
| 155 | + |
| 156 | +Putting the three plugins together, a typical workflow looks like this: |
| 157 | + |
| 158 | +```sh |
| 159 | +# 1. Scaffold a new function |
| 160 | +swift package lambda-init --allow-writing-to-package-directory |
| 161 | + |
| 162 | +# 2. Build and package it for Amazon Linux 2023 |
| 163 | +swift package --allow-network-connections docker lambda-build |
| 164 | + |
| 165 | +# 3. Deploy it to AWS |
| 166 | +swift package --allow-network-connections all:443 lambda-deploy |
| 167 | +``` |
| 168 | + |
| 169 | +> Note: The legacy `archive` command remains available as a deprecated alias for |
| 170 | +> `lambda-build`. |
0 commit comments