Deploy HTTP Services Written in Swift to AWS in Seconds
Tags: swift, aws, tutorial, beginners
Published: 2020-07-29 13:24:28

---
title: Deploy HTTP Services Written in Swift to AWS in Seconds
published: true
description: Use this Open Source project to deploy a Swift service to AWS in seconds.
tags: swift, aws, tutorial, beginners
//cover_image: https://direct_url_to_image.jpg
canonical_url: https://getstream.io/blog/swift-lambda-aws-serverless
---

Recently, Apple announced the [Swift AWS Lambda Runtime](https://swift.org/blog/aws-lambda-runtime/). It's now possible to write self-contained functions that run on AWS using the same Swift you use for iOS development. This lets you not only reuse the knowledge you already have of Swift, but also share code between the server and client. However, everything is still a little bit complicated to set up. To help you get started faster, we've built a [starting point for writing an HTTP service in Swift](https://github.com/GetStream/swift-lambda), which does everything you need by running a single script. 

![Image shows a terminal window with the deployment process of an AWS Lambda written in Swift finished](https://i.imgur.com/dSz1pYT.png)

Swift Lambda contains configuration files and scripts to fully automate deploying to AWS in a matter of seconds using the [Serverless Framework](https://www.serverless.com/). It is based on samples and documentation from [swift-server/swift-aws-lambda-runtime](https://github.com/swift-server/swift-aws-lambda-runtime).

## Getting started

1. Clone the repository: https://github.com/GetStream/swift-lambda

2. [Install Docker](https://docs.docker.com/docker-for-mac/install/)

3. [Install the Serverless Framework](https://www.serverless.com/framework/docs/getting-started/)

4. [Configure the AWS Credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials/)

5. Write your code in `Sources/Lambda/main.swift`

6. Deploy by running `./Scripts/deploy.sh`

7. Open the output URL in your browser.

![Image shows a browser on an AWS Lambda URL showing the words "Hello, world!" output from the swift code](https://i.imgur.com/FocTSMk.png)

## Writing code

There is some code already present in the `Sources/Lambda/main.swift` file. It simply outputs "Hello, world!" in plain text.

{% gist https://gist.github.com/cardoso/e6324000861379344bd12b21a47d91de %}

If you want to output some HTML, just set the "Content-Type" header to "text/html; charset=UTF-8"

{% gist https://gist.github.com/cardoso/2134399fa17d96d4b44bcbebf70ac7de %}

![Image shows a browser on an AWS Lambda URL showing the words "Hello, world! From Swift 5.2 💘 and a User Agent string" output from the swift code](https://i.imgur.com/CBWG1vG.png) 

For more information on the available settings and methods, refer to the [Swift AWS Lambda Runtime README](https://github.com/swift-server/swift-aws-lambda-runtime)

## Configuring the endpoint

To change some characteristics of your HTTP endpoint, such as the method expected, you should modify the [`serverless.yml`](https://github.com/GetStream/swift-lambda/blob/main/serverless.yml#L70-L72) file. For more information on the available parameters, refer to the [Serverless.yml Reference](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/)

## Contributing

If you have a suggestion or bug report, please [file an issue in the Swift Lambda repository](https://github.com/GetStream/swift-lambda/issues/new). If you want to take a stab at contributing code, don't hesitate in submitting a PR.  Don't forget to leave a star if you liked it :)