@@ -111,6 +111,12 @@ swift package init --name quoteapi --type executable
111
111
2 . Write or import an OpenAI API definition in YAML or JSON
112
112
113
113
```yaml
114
+ #
115
+ # the $ signs are escaped (\$) to work with the cat << EOF command
116
+ # if you choose to copy the content directly to a text editor,
117
+ # be sure to remove the \ (that means \$ becomes $)
118
+ #
119
+
114
120
cat << EOF > Sources/ openapi.yaml
115
121
openapi: 3.1.0
116
122
info:
@@ -155,12 +161,12 @@ paths:
155
161
content:
156
162
application/ json:
157
163
schema:
158
- $ref: '#/components/schemas/quote'
164
+ \ $ref: '#/components/schemas/quote'
159
165
400 :
160
166
description: Bad Request
161
167
404 :
162
168
description: Not Found
163
- EOF
169
+ EOF
164
170
```
165
171
166
172
3 . Add a Swift OpenAPI generator configuration file to generate only the server side
@@ -170,7 +176,7 @@ cat << EOF > Sources/openapi-generator-config.yaml
170
176
generate:
171
177
- types
172
178
- server
173
- EOF
179
+ EOF
174
180
```
175
181
176
182
4 . Use this `Package.swift` file to define targets and their dependencies
@@ -191,7 +197,7 @@ let package = Package(
191
197
.executable (name : " QuoteService" , targets : [" QuoteService" ]),
192
198
],
193
199
dependencies : [
194
- .package (url : " https://github.com/apple/swift-openapi-generator.git" , .upToNextMinor (from : " 1.0.0-alpha.1 " )),
200
+ .package (url : " https://github.com/apple/swift-openapi-generator.git" , .upToNextMinor (from : " 1.0.0" )),
195
201
.package (url : " https://github.com/apple/swift-openapi-runtime.git" , .upToNextMinor (from : " 1.0.0" )),
196
202
.package (url : " https://github.com/swift-server/swift-aws-lambda-runtime.git" , branch : " 1.0.0-alpha.1" ),
197
203
.package (url : " https://github.com/swift-server/swift-aws-lambda-events.git" , branch : " main" ),
@@ -274,6 +280,12 @@ swift build
274
280
1 . Add the Lambda build instructions as a Docker file and a Makefile. We build for Swift 5.9 on Amazon Linux 2
275
281
276
282
```sh
283
+ #
284
+ # the $ signs are escaped (\$) to work with the cat << EOF command
285
+ # if you choose to copy the content directly to a text editor,
286
+ # be sure to remove the \ (that means \$ becomes $)
287
+ #
288
+
277
289
cat << EOF > Dockerfile
278
290
# image used to compile your Swift code
279
291
FROM public .ecr.aws/ docker/ library/ swift: 5.9.1 - amazonlinux2
@@ -298,34 +310,41 @@ tail:
298
310
###################### No Change required below this line ##########################
299
311
300
312
builder- bot:
301
- $(eval $@PRODUCT = $(subst build- ,,$(MAKECMDGOALS)))
302
- $(eval $@BUILD_DIR = $(PWD)/ . aws-sam/ build- swift)
303
- $(eval $@STAGE = $( $@BUILD_DIR )/ lambda)
304
- $(eval $@ARTIFACTS_DIR = $(PWD)/ . aws-sam/ build/ $( $@PRODUCT ))
313
+ \ $(eval \ $@PRODUCT = \ $(subst build- ,,\ $(MAKECMDGOALS)))
314
+ \ $(eval \ $@BUILD_DIR = \ $(PWD)/ . aws-sam/ build- swift)
315
+ \ $(eval \ $@STAGE = \$(\ $@BUILD_DIR )/ lambda)
316
+ \ $(eval \ $@ARTIFACTS_DIR = \ $(PWD)/ . aws-sam/ build/ \$(\ $@PRODUCT ))
305
317
306
318
# build docker image to compile Swift for Linux
307
319
docker build - f Dockerfile . - t swift- builder
308
320
309
321
# prep directories
310
- mkdir - p $( $@BUILD_DIR )/ lambda $( $@ARTIFACTS_DIR )
322
+ mkdir - p \$(\ $@BUILD_DIR )/ lambda \$(\ $@ARTIFACTS_DIR )
311
323
312
324
# compile application inside Docker image using source code from local project folder
313
- docker run -- rm - v $( $@BUILD_DIR ): / build-target -v `pwd`:/ build- src - w / build- src swift- builder bash - cl " swift build --static-swift-stdlib --product $( $@PRODUCT) -c release --build-path /build-target"
325
+ docker run -- rm - v \$(\ $@BUILD_DIR ): / build-target -v \ ` pwd\ ` :/ build- src - w / build- src swift- builder bash - cl " swift build --static-swift-stdlib --product \$ ( \ $ @PRODUCT) -c release --build-path /build-target"
314
326
315
327
# create lambda bootstrap file
316
- docker run -- rm - v $( $@BUILD_DIR ): / build-target -v `pwd`:/ build- src - w / build- src swift- builder bash - cl " cd /build-target/lambda && ln -s $( $@PRODUCT) /bootstrap"
328
+ docker run -- rm - v \$(\ $@BUILD_DIR ): / build-target -v \ ` pwd\ ` :/ build- src - w / build- src swift- builder bash - cl " cd /build-target/lambda && ln -s \$ ( \ $ @PRODUCT) /bootstrap"
317
329
318
330
# copy binary to stage
319
- cp $( $@BUILD_DIR )/ release/ $( $@PRODUCT ) $( $@STAGE )/ bootstrap
331
+ cp \$(\ $@BUILD_DIR )/ release/ \$(\ $@PRODUCT ) \$(\ $@STAGE )/ bootstrap
320
332
321
333
# copy app from stage to artifacts dir
322
- cp $($@STAGE )/* $($@ARTIFACTS_DIR)
334
+ cp \$(\$@STAGE )/* \$(\$@ARTIFACTS_DIR)
335
+
323
336
EOF
324
337
```
325
338
326
339
2. Add a SAM template to deploy the Lambda function and the API Gateway
327
340
328
341
```sh
342
+ #
343
+ # the $ signs are escaped (\$) to work with the cat << EOF command
344
+ # if you choose to copy the content directly to a text editor,
345
+ # be sure to remove the \ (that means \$ becomes $)
346
+ #
347
+
329
348
cat << EOF > template.yml
330
349
AWSTemplateFormatVersion: '2010-09-09'
331
350
Transform: AWS::Serverless-2016-10-31
@@ -357,7 +376,7 @@ Resources:
357
376
Outputs:
358
377
SwiftAPIEndpoint:
359
378
Description: "API Gateway endpoint URL for your application"
360
- Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"
379
+ Value: !Sub "https://\ ${ServerlessHttpApi}.execute-api.\ ${AWS::Region}.amazonaws.com"
361
380
EOF
362
381
```
363
382
@@ -370,8 +389,16 @@ sam build
370
389
4. Deploy the Lambda function and create an API Gateway in front of it
371
390
372
391
```sh
373
- # use --guided for the first deployment only. SAM cli collects a few parameters and store them in `samconfig.toml`
374
- sam deploy --guided
392
+ # use --guided for the first deployment only.
393
+ # SAM cli collects a few parameters and store them in `samconfig.toml`
394
+
395
+ sam deploy --guided --stack-name QuoteService
396
+ ```
397
+
398
+ Accept all the default values, except:
399
+
400
+ ```sh
401
+ QuoteService has no authentication. Is this okay? [y/N]: <-- answer Y here
375
402
```
376
403
377
404
This command outputs the URL of the API GAteway, for example:
@@ -388,7 +415,7 @@ Value https://747ukfmah7.execute-api.us-east-1.amazonaws.com
388
415
5. Test your setup
389
416
390
417
```sh
391
- curl https://747ukfmah7.execute-api.us-east-1.amazonaws.com /stocks/AAPL
418
+ curl [[ Replace with SWIFTAPIEndpoint value ]] /stocks/AAPL
392
419
{
393
420
"change" : -4,
394
421
"changePercent" : -0.030052760210257923,
@@ -399,6 +426,10 @@ curl https://747ukfmah7.execute-api.us-east-1.amazonaws.com/stocks/AAPL
399
426
}
400
427
```
401
428
429
+ 6. Delete the infrastructure
430
+
431
+ There is no cost
432
+
402
433
## Local Testing
403
434
404
435
```
0 commit comments