KEMBAR78
Server(less) Swift at SwiftCloudWorkshop 3 | PDF
Server(less) Swift
Joshua B. Smith kognate@gmail.com
Swift Cloud Workshop 3 (February 23, 2018)
MartianCraft, LLC
Server
There is no Cloud
The “cloud” is just someone elses computer.
(less)
Manifestos For Everybody
• Function are the unit of deployment and scaling.
• No machines, VMs, or containers visible in the programming
model.
• Permanent storage lives elsewhere.
• Scales per request; Users cannot over- or under-provision
capacity.
• Never pay for idle (no cold servers/containers or their costs).
• Implicitly fault-tolerant because functions can run anywhere.
• BYOC - Bring Your Own Code.
• Metrics and logging are a universal right.
http://blog.rowanudell.com/the-serverless-compute-manifesto/
There are LOTS of function providers
• Amazon Lambda
• Google Cloud Functions
• Azure has some, too
• and these are just the big name-brand providers
Few support Swift
None of the Big 3 do.
Two Systems that fit the bill
• OpenWhisk
• Fn
OpenWhisk
• Open Source
• Event Driven
• Containerized
• Chainable
Open Source
Currently an Apache Project (incubator)
Event Driven
Events (called Triggers) can invoke actions via rules.
This is a SUPERPOWER
Containerized
But you don’t have to manage them. You can even run “bare”
Swift functions. Supports a large number of languages.
Chainable
Sequences of actions can be built. Scatter/Gather easily.
Hard to deploy
The Bad News: OW is hard to deploy and needs a fair amount of
infrastructure to host yourself.
You can use Bluemix
Easiest way to start is to use Bluemix, which hosts OW and has
very attractive pricing for it.
Get the CLI
https://console.bluemix.net/openwhisk/learn/cli
You can use the web
You can use the web console, but it’s better to use the CLI
Simple Function
1 func main(args: [String:Any]) -> [String:Any] {
2 var res: [String: Any] = [:]
3 for (k, v) in args {
4 res[k] = v
5 }
6 return res
7 }
Deploy it
(assuming you have your bluemix account all setup)
bx wsk action create simpleow 
Sources/simple_ow/main.swift 
--kind swift:3.1.1
Update it
bx wsk action update simpleow 
Sources/simple_ow/main.swift 
--kind swift:3.1.1
Run it
bx wsk action invoke --result 
--blocking simpleow 
--param one 10 --param two 20
1 {
2 "one": 10,
3 "two": 20
4 }
RESTful Endpoint (requires auth)
(use -v to see all the stuff)
https://openwhisk.ng.bluemix.net/api/v1/namespaces/
_/actions/simpleow?blocking=true&result=true
Logging
bx wsk activation ls
bx wsk activation get <ACTIVATIONID>
Docker
Everybody Loves Docker
Containerization is lighter weight than Virtual Machines
Containers are not the same as Virtual Machines
• Containers run inside Machines (either real or virtual)
• Containers can’t see each other unless you let them
• Isolation is the word of the day
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
Base Image
This is the image used to build on top of
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
RUN a command
executes a command in the new docker container. In this case,
create a directory for our code.
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
copy files into our new container
copies a single file from the currenct directory to the destination
path in the container.
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
copy directory into our new container
copies (with recursion) a directory file from the currenct directory
to the destination path in the container.
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
Run a command
executes a command in the new docker container. change
directory and build the project.
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
register a port that can be opened
This registers a port and lets the docker environment know this
port can be opened.
Docker file
1 FROM swiftdocker/swift:latest
2 RUN mkdir /var/run/app
3 ADD Package.swift /var/run/app
4 ADD Sources /var/run/app/Sources
5 RUN cd /var/run/app && swift build
6 EXPOSE 8080
7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
Entrypoint
This is the entrypoint of the container, and this is run during
‘docker run‘
fn project
The container native, cloud agnostic serverless platform.
Container native?
It deploys as a container
Very Young Project
That doesn’t mean it’s not good, it’s just not as mature as other
systems.
• Open Source
• Containerized
Open Source
Apache 2.0 License
Containerized
Functions can be docker containers. STDIN and STDOUT
Also has a nice specification for formats
Simple Function
1 import Foundation
2
3 FileHandle.standardError.write("this is a log message".data
4
5 print("Hello, world!")
6
7 let dic = ProcessInfo.processInfo.environment
8 for (k,v) in dic {
9 print("(k) (v)")
10 }
Deploy it
fn deploy --app simple_fn --local
Same command to update
Run It
fn call simple_fn /simple_fn
The Output
1 Hello, world!
2 FN_PATH /simple_fn
3 SWIFT_BRANCH swift-4.0.3-release
4 FN_CALL_ID 01C6ZWWCWE47WGY00000000000
5 SWIFT_PLATFORM ubuntu16.04
6 FN_APP_NAME simple_fn
7 FN_TYPE sync
8 HOME /root
9 FN_HEADER_Accept-Encoding gzip
10 PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
11 HOSTNAME b8e2c8b47ad3
12 FN_HEADER_Content-Type application/json
13 FN_HEADER_User-Agent Go-http-client/1.1
14 FN_DEADLINE 2018-02-22T23:17:27.358Z
15 FN_FORMAT default
16 SWIFT_VERSION swift-4.0.3-RELEASE
17 FN_METHOD GET
18 FN_REQUEST_URL http://localhost:8080/r/simple_fn/simple_fn
RESTful Endpoint
/r/simple_fn/simple_fn
You can get the list using
fn routes l <APPNAME>
Chainable?
Not yet, but it’s in beta.
Logs
fn calls l <APPNAME>
fn logs g <APPNAME> <ID>
Should I use this?
If you want a cost effective way to deploy microservices using the
Swift language.
Good Use Cases
• Chat bots/Slack APIS
• Data/ETL pipelines
• IOT
• Mobile applications
Which one should I use?
Thank you
Questions?

Server(less) Swift at SwiftCloudWorkshop 3

  • 1.
    Server(less) Swift Joshua B.Smith kognate@gmail.com Swift Cloud Workshop 3 (February 23, 2018) MartianCraft, LLC
  • 2.
  • 3.
    There is noCloud The “cloud” is just someone elses computer.
  • 4.
  • 5.
    Manifestos For Everybody •Function are the unit of deployment and scaling. • No machines, VMs, or containers visible in the programming model. • Permanent storage lives elsewhere. • Scales per request; Users cannot over- or under-provision capacity. • Never pay for idle (no cold servers/containers or their costs). • Implicitly fault-tolerant because functions can run anywhere. • BYOC - Bring Your Own Code. • Metrics and logging are a universal right. http://blog.rowanudell.com/the-serverless-compute-manifesto/
  • 6.
    There are LOTSof function providers • Amazon Lambda • Google Cloud Functions • Azure has some, too • and these are just the big name-brand providers
  • 7.
    Few support Swift Noneof the Big 3 do.
  • 8.
    Two Systems thatfit the bill • OpenWhisk • Fn
  • 9.
    OpenWhisk • Open Source •Event Driven • Containerized • Chainable
  • 10.
    Open Source Currently anApache Project (incubator)
  • 11.
    Event Driven Events (calledTriggers) can invoke actions via rules. This is a SUPERPOWER
  • 12.
    Containerized But you don’thave to manage them. You can even run “bare” Swift functions. Supports a large number of languages.
  • 13.
    Chainable Sequences of actionscan be built. Scatter/Gather easily.
  • 14.
    Hard to deploy TheBad News: OW is hard to deploy and needs a fair amount of infrastructure to host yourself.
  • 15.
    You can useBluemix Easiest way to start is to use Bluemix, which hosts OW and has very attractive pricing for it.
  • 16.
  • 17.
    You can usethe web You can use the web console, but it’s better to use the CLI
  • 18.
    Simple Function 1 funcmain(args: [String:Any]) -> [String:Any] { 2 var res: [String: Any] = [:] 3 for (k, v) in args { 4 res[k] = v 5 } 6 return res 7 }
  • 19.
    Deploy it (assuming youhave your bluemix account all setup) bx wsk action create simpleow Sources/simple_ow/main.swift --kind swift:3.1.1
  • 20.
    Update it bx wskaction update simpleow Sources/simple_ow/main.swift --kind swift:3.1.1
  • 21.
    Run it bx wskaction invoke --result --blocking simpleow --param one 10 --param two 20 1 { 2 "one": 10, 3 "two": 20 4 }
  • 22.
    RESTful Endpoint (requiresauth) (use -v to see all the stuff) https://openwhisk.ng.bluemix.net/api/v1/namespaces/ _/actions/simpleow?blocking=true&result=true
  • 23.
    Logging bx wsk activationls bx wsk activation get <ACTIVATIONID>
  • 24.
  • 25.
    Everybody Loves Docker Containerizationis lighter weight than Virtual Machines
  • 26.
    Containers are notthe same as Virtual Machines • Containers run inside Machines (either real or virtual) • Containers can’t see each other unless you let them • Isolation is the word of the day
  • 27.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ]
  • 28.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] Base Image This is the image used to build on top of
  • 29.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] RUN a command executes a command in the new docker container. In this case, create a directory for our code.
  • 30.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] copy files into our new container copies a single file from the currenct directory to the destination path in the container.
  • 31.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] copy directory into our new container copies (with recursion) a directory file from the currenct directory to the destination path in the container.
  • 32.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] Run a command executes a command in the new docker container. change directory and build the project.
  • 33.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] register a port that can be opened This registers a port and lets the docker environment know this port can be opened.
  • 34.
    Docker file 1 FROMswiftdocker/swift:latest 2 RUN mkdir /var/run/app 3 ADD Package.swift /var/run/app 4 ADD Sources /var/run/app/Sources 5 RUN cd /var/run/app && swift build 6 EXPOSE 8080 7 CMD [ "/var/run/app/.build/debug/<YOURAPPNAME>" ] Entrypoint This is the entrypoint of the container, and this is run during ‘docker run‘
  • 35.
    fn project The containernative, cloud agnostic serverless platform.
  • 36.
  • 37.
    Very Young Project Thatdoesn’t mean it’s not good, it’s just not as mature as other systems. • Open Source • Containerized
  • 38.
  • 39.
    Containerized Functions can bedocker containers. STDIN and STDOUT Also has a nice specification for formats
  • 40.
    Simple Function 1 importFoundation 2 3 FileHandle.standardError.write("this is a log message".data 4 5 print("Hello, world!") 6 7 let dic = ProcessInfo.processInfo.environment 8 for (k,v) in dic { 9 print("(k) (v)") 10 }
  • 41.
    Deploy it fn deploy--app simple_fn --local Same command to update
  • 42.
    Run It fn callsimple_fn /simple_fn
  • 43.
    The Output 1 Hello,world! 2 FN_PATH /simple_fn 3 SWIFT_BRANCH swift-4.0.3-release 4 FN_CALL_ID 01C6ZWWCWE47WGY00000000000 5 SWIFT_PLATFORM ubuntu16.04 6 FN_APP_NAME simple_fn 7 FN_TYPE sync 8 HOME /root 9 FN_HEADER_Accept-Encoding gzip 10 PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 11 HOSTNAME b8e2c8b47ad3 12 FN_HEADER_Content-Type application/json 13 FN_HEADER_User-Agent Go-http-client/1.1 14 FN_DEADLINE 2018-02-22T23:17:27.358Z 15 FN_FORMAT default 16 SWIFT_VERSION swift-4.0.3-RELEASE 17 FN_METHOD GET 18 FN_REQUEST_URL http://localhost:8080/r/simple_fn/simple_fn
  • 44.
    RESTful Endpoint /r/simple_fn/simple_fn You canget the list using fn routes l <APPNAME>
  • 45.
    Chainable? Not yet, butit’s in beta.
  • 46.
    Logs fn calls l<APPNAME> fn logs g <APPNAME> <ID>
  • 47.
    Should I usethis? If you want a cost effective way to deploy microservices using the Swift language.
  • 48.
    Good Use Cases •Chat bots/Slack APIS • Data/ETL pipelines • IOT • Mobile applications
  • 49.
  • 50.