This PR has been reworked. Instead of silently switching the default base image based on Swift version, we now: 1. **Keep Amazon Linux 2 as the default** base Docker image for the packager plugin 2. **Add a prominent deprecation warning** when AL2 is used (either via Docker or natively), informing developers that AL2 reaches End of Life on June 30, 2026 3. **Migrate all examples** (READMEs, SAM templates, scripts) to build and deploy on Amazon Linux 2023 (`provided.al2023` runtime + `--base-docker-image swift:amazonlinux2023`) 4. **Update documentation** (readme, quick-setup) with migration notes The warning includes the `--base-docker-image swift:6.3-amazonlinux2023` flag and reminds developers to use the `provided.al2023` runtime when deploying. After June 30, 2026, the default will switch to AL2023. --- <details> <summary>Original PR description (superseded)</summary> ~~Now that Docker Hub has official Swift images based on Amazon Linux 2023 (starting with 6.3), the packager plugin picks the right base image automatically depending on the Swift version:~~ ~~- Swift 6.3 and later: `swift:<version>-amazonlinux2023`~~ ~~- Earlier versions: `swift:<version>-amazonlinux2` (unchanged behavior)~~ ~~- No version specified (latest): defaults to `amazonlinux2023`~~ ~~When only a major version is provided (e.g. `--swift-version 6` without a minor), we conservatively treat it as 6.0 and use Amazon Linux 2, since we can't be sure it's 6.3+.~~ ~~Also added a verbose log line showing the resolved Swift version, Amazon Linux version, and final base image to help with debugging.~~ ~~The `--base-docker-image` flag still overrides everything as before.~~ </details> --------- Co-authored-by: Sébastien Stormacq <stormacq@amazon.lu>
API Gateway and Cloud Development Kit
This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway and deployed with the Cloud Development Kit (CDK).
Code
The Lambda function takes all HTTP headers it receives as input and returns them as output. See the API Gateway example for a complete description of the code.
Build & Package
To build the package, type the following commands.
swift build
swift package archive --allow-network-connections docker --base-docker-image swift:amazonlinux2023
If there is no error, there is a ZIP file ready to deploy.
The ZIP file is located at .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip
Deploy
[NOTE] Before deploying the infrastructure, you need to have NodeJS and the AWS CDK installed and configured. For more information, see the AWS CDK documentation.
To deploy the infrastructure, type the following commands.
# Change to the infra directory
cd infra
# Install the dependencies (only before the first deployment)
npm install
cdk deploy
✨ Synthesis time: 2.88s
... redacted for brevity ...
Do you wish to deploy these changes (y/n)? y
... redacted for brevity ...
✅ LambdaApiStack
✨ Deployment time: 42.96s
Outputs:
LambdaApiStack.ApiUrl = https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com/
Stack ARN:
arn:aws:cloudformation:eu-central-1:401955065246:stack/LambdaApiStack/e0054390-be05-11ef-9504-065628de4b89
✨ Total time: 45.84s
Invoke your Lambda function
To invoke the Lambda function, use this curl command line.
curl https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com
Be sure to replace the URL with the API Gateway endpoint returned in the previous step.
This should print a JSON similar to
{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"}
If you have jq installed, you can use it to pretty print the output.
curl -s https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com | jq
{
"version": "2.0",
"rawPath": "/",
"requestContext": {
"domainPrefix": "a5q74es3k2",
"stage": "$default",
"timeEpoch": 1727726558220,
"http": {
"protocol": "HTTP/1.1",
"method": "GET",
"userAgent": "curl/8.7.1",
"path": "/",
"sourceIp": "81.0.0.43"
},
"apiId": "a5q74es3k2",
"accountId": "012345678901",
"requestId": "e72KxgsRoAMEMSA=",
"domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
"time": "30/Sep/2024:20:02:38 +0000"
},
"rawQueryString": "",
"routeKey": "$default",
"headers": {
"x-forwarded-for": "81.0.0.43",
"user-agent": "curl/8.7.1",
"host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
"accept": "*/*",
"x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb",
"content-length": "0",
"x-forwarded-proto": "https",
"x-forwarded-port": "443"
},
"isBase64Encoded": false
}
Undeploy
When done testing, you can delete the infrastructure with this command.
cdk destroy
Are you sure you want to delete: LambdaApiStack (y/n)? y
LambdaApiStack: destroying... [1/1]
... redacted for brevity ...
✅ LambdaApiStack: destroyed
⚠️ Security and Reliability Notice
These are example applications for demonstration purposes. When deploying such infrastructure in production environments, we strongly encourage you to follow these best practices for improved security and resiliency:
- Enable access logging on API Gateway (documentation)
- Ensure that AWS Lambda function is configured for function-level concurrent execution limit (concurrency documentation, configuration guide)
- Check encryption settings for Lambda environment variables (documentation)
- Ensure that AWS Lambda function is configured for a Dead Letter Queue (DLQ) (documentation)
- Ensure that AWS Lambda function is configured inside a VPC when it needs to access private resources (documentation, code example)