No, the SDK does not currently support that, and how to actually use the endpoint resolver for other uses is extremely poorly documented - resolving both of those would take some headaches away for me using Swift on AWS.
Hi all, thanks for your comments so far, they have definitely provided a number of topics to discuss.
I had a question about how people would like to be able to define server side Swift applications. Would people prefer-
- Primarily a CLI tool (maybe something like AWS Copilot CLI - AWS Copilot CLI) with documentation on how to use the tool
- Primarily a macOS application (essentially the features of a CLI tool but in graphical form)
- Bootstrapping and status provided by a CLI tool or macOS application but the ability to declare what your application is (at a high level) defined in Swift
For (3), being able to define your application something like this-
let app = App()
GithubSourcedPipelineStack(
app: app,
stackName: "AwsSwiftLambdaExampleStack",
pipelineApplications: [
SwiftLambda(executableName: "AwsSwiftLambdaExample",
functionUri: .enabled)
],
infrastructureExecutableName: "Infrastructure",
swiftVersion: "5.6.1",
awsRegion: "us-west-2",
repositoryOwner: "me-at-github",
repositoryName: "my-github-repository",
repositoryBranch: "branch",
sourceConnectionArn: "arn:aws:codestar-connections:region:XXXXXXXXXXXX:connection/uuid")
try app.synth()
Swift support for the CDK would be great
Sorry for the drive-by comment, but this caught my eye.
macOS application
Why limit this theoretical application to macOS? I know I would be interested in such a tool ... but not on MacOS . Those machines exist solely to give me access to the SDKs
We already run Swift on AWS. Not any services which interface directly with HTTP (yet!), but some services as part of our content publishing pipeline. So I might not be the target for this post.
I would love to see it become a little more straightforward to deploy AWS lambdas. All the pieces are there with GitHub - swift-server/swift-aws-lambda-runtime: Swift implementation of AWS Lambda Runtime and the new ability to do Lambdas from docker images. But… connecting all the pieces feels complicated. And all the documentation for swift-aws-lambda-runtime suggests building the executable using a dockerfile locally, copying it out, then zipping it up & plopping it in a lambda. A nicer process would be a container image-based lambda with a single dockerfile. Even nicer would be a CLI (maybe a Swift PM plugin?) to wrap it all up.
That's my answer to this question, by the way. I think a CLI tool is superior to the other options (it's not limited to macOS for development + it'll be easier to use in CI). I personally think it would be a great use for a SwiftPM plugin: swift-package-manager/Documentation/Plugins.md at main · swiftlang/swift-package-manager · GitHub
I second this. While the CDK is great it is frustrating that it is required to use another language ecosystem to deploy apps.
Does not do everything (yet) but getting close
This is awesome! I hadn’t seen that. Thank you for the tip. Is there somewhere with documentation on this? I think a mention in the README at least would be worthwhile
I made a long time ago a makefile + cloudformation to help in this process... maybe helps somehow: GitHub - robipresotto/swiftLambdaTemplate: ⚠️ [WIP] Swift package template for AWS Lambda
Also running for about 3y multiple microservices written in swift and deployed by fargate (ECS+ECR) and AWS code pipeline to help in this process. It's extremely straightforward and easy to achieve.
What is missing the most is support for XRay - got some issues implementing AppMesh sometime ago.
Me and @slashmo implemented this in a client project a while ago. There were lots of hoops to jump through, a Vapor fork and some custom packages but we made it work. Once tracing lands a 1.0 release we can work out how to integrate it all properly
Definitely something that works also for other server-side platforms - Linux specifically - so I think 2 is out.
Option 3, please. To see Swift added as a supported language for the CDK would be incredible.
If I were to define a goal for this sort of thing, it would be: make it possible to define a system of interconnected AWS services in a single SwiftPM package. What follows is a little pie-in-the-sky, but I figured I'd share.
So each lambda/ECS service/ECS task is its own SwiftPM target and maybe there's an associated JSON/YAML (or Swift - whatever; that has never really spoken to me) file defining a mapping, non-compute resources, etc. Then swift package deploy
would just make everything go (extra points for enabling separate environments, etc).
// Package.swift
import PackageDescription
let package = Package(
name: "…",
products: [ /* ... */ ],
dependencies: [ /* … */ ],
targets: [
.target(name: "MyLambda", dependencies: [ /* … */ ]),
]
)
/* AWS-services.json */
{
"services" : [
{
"type" : "lambda",
"name" : "my-lambda",
"swift-product" : "MyLambda",
"events" : [ {"type" : "s3", "bucket" : "my-lambdas-bucket"} ]
},
{
"type" : "s3-bucket",
"name" : "my-lambdas-bucket"
},
{
"type" : "sqs-queue",
"name" : "my-lambdas-queue"
}
]
}
Furthermore, I imagine we could write nice tests by synthesizing some test code:
import MyLambda
import AWSTestingInfrastructure
import XCTest
class SystemTests: XCTestCase {
func testS3Notification() throws {
let system = SynthesizedAWSTestingSystem()
let notif = system.my_lambdas_bucket.objectCreatedNotification(key: "foo")
system.my_lambda.receive(notif)
let messages = system.my_lambdas_queue.drain()
XCTAssertEqual(messages, [ /*...*/])
}
}
Obviously services which aren't S3 and SQS can become more & more complicated to mock out, but even just the basics would be enormously helpful.
A question to everyone who is interested in Swift support for the CDK - which is being able to define infrastructure as Swift code (similar to what I have above).
Given the work to fully support Swift within the CDK is quite significant and will take some time, what would people prefer-
- Wait until Swift is fully supported by the CDK, giving full configurability of defining arbitrary infrastructure in Swift code plus the ability to combine individual infrastructure components into higher level constructs.
- Provide an interim solution that provides higher level components for a couple of well defined use cases (a couple of Lambda use cases, a basic AppRunner or Fargate use case). This interim solution would be limited to these use cases and initially wouldn't be configurable beyond them. This interim solution would then evolve into being built on Swift CDK components when they become available, allowing for these constructs to be extended and combined with other infrastructure components.
I'd choose option 2, it will help people get started and show if/how big a demand for a Swift CDK is there
An interim solution (2) would of course be preferable to nothing at all, but in that case I'd hope it would include an escape hatch for working with the CDK in other languages.
With AWS, it very quickly becomes necessary to provision some kind of storage via s3 or DynamoDB, set up Cloudfront for hosting static assets, the IAM roles that go with it... It would be a shame to get so far with a Swift solution and then have to port it over to another language to gain the necessary flexibility.
In terms of constructs to start with, it would be great to see a basic static website generator template, (that can be used with Sundell's Plot and Ink for example), that can be hosted on GitHub and triggered by a GitHub action. (s3, Cloudfront, etc.)
And with that, of course it would be great to see a construct that can setup some Lambdas, API gateway, and DynamoDB for the backend.
I think the GitHub part is important. So, perhaps like the CDK, the tool could setup the necessary roles and policies in an AWS account so that a GitHub Action can then authenticate with 'aws-actions/configure-aws-credentials@v1' and deploy.
The Swift community is heavily invested in GitHub with the Swift project itself and the majority of SPM packages already hosted there. Removing the cognitive load of learning an additional CICD solution, such as AWS CodeCommit/CodeBuild/CodeDeploy/CodePipeline would, I think, make the proposition far more attractive and easier for the community to demonstrate.
With that I think you would quickly see the Swift community taking the baton and producing tutorials demonstrating the ease with which it's possible to set-up a modern website and backend for their mobile apps, plus the economy of serverless.
I would choose option 2 as well. Providing support for standard patterns like upload a lambda, setup a ECS container behind a load balancer, upload a file to S3 would be a great start. It would also provide a great starting off point for individuals who might want to contribute to the project. If there is a framework already in place it makes it easier to consider how to provide additional functionality.
It would be great to build a swift command plugin, to deploy my app using the CDK and then be able to deploy using swift package deploy
. Although I don't think the CDK needs to know about this as I assume it'll be a bunch of libraries, but something to keep in mind.
Out of interest is any of the CDK generated code, given all the SDKs are?
I currently wrote and maintain 3 swift AWS lambda projects, i had been a long time php and web developer who jumped into objective-c with ios2, my biggest pain point is creating a amazonlinux2 amd64 executable on my M1 Pro. Everything in docker to emulate x86 has failed for me, and i can't seem to deploy a graviton2 arm64 executable(but i can build it). This leaves me running deployments from my intel mac, until xcode becomes unsupported for that aging computer....
I use docker to build and package and serverless to deploy the executable.
I really enjoy Soto AWS libraries, extendability and flexibility are key.
So 1 & 2 end to end deployments for aws lambda on an apple silicon.
3 I think a few vetted extensions to SwiftNIO would be very helpful, like cleanly capturing a byteBuffer to Data or a String.
4 AWS Xray seems hard to implement and i wasn't able to leverage that, instead i used cloudwatch and made my own breadcrumb system to track inefficiencies.The tighter the core AWSRunTime can be to AWS the more adoption I'd expect to see.
Nick
Just going through this right now and I've had an incredibly frustrating time. Let me explain my frustrations.
- Lack of documentation
- Not really clear what is the best AWS solution. EC2, EB, ECS? Something that explains the pros and cons of everything would be great.
- A few tutorials I find on Medium, etc., ended up using CDK1 and I had to learn how to migrated it (after having tons of errors)
- This is a big one: Docker just sucks on M1. I have to keep my old Intel Mac on my desk and run
cdk deploy
from there. Not only is it 100x faster on Intel, but it also doesn't crash every 10 seconds. - Some documentation that lets you know what works and doesn't work on Linux. Just right now, I'm trying to figure out why JSONDecoder doesn't decode a json file I have that does work on my Mac dev environment.
I share your pain. I have a feeling that all swift-based books and tutorials count that you have some background on a server/deploying/AWS using other technologies like NodeJs or PHP, and you know many tiny things by default. I guess people forget that most Swift developers (including myself) are from iOS pink bubble, and a lot of apparent things for tutorial authors are not so evident for a person who always counted on "best practices" from Apple during WWDC or documentation. I found it straightforward to write code with Vapor and share it with my iOS client, but I have difficulty understanding the best way to deploy it. What are the best options? So far, I have read two books Practical Server Side Swift
and Server-Side Swift with Vapor
and while both cover different ways of deploying, including AWS, DigitaOcen, Docker, etc., they are somewhat not clear and, at a high level, say "deploying/hosting server apps is a hard topic". It wasn't negative feedback overall, the books themselves were beneficial for me. Back to the original question - "What would help you use Server Side Swift with AWS?"
- Showcases where someone can share a success story from "A-Z" on how to deploy and what tools they used for server-side swift on AWS. (small/medium/large apps).