Skip to content

Commit a8d0fd8

Browse files
committed
Update deployment instructions to make them work with simple copy / paste
1 parent 2c177eb commit a8d0fd8

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

README.md

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ swift package init --name quoteapi --type executable
111111
2. Write or import an OpenAI API definition in YAML or JSON
112112

113113
```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+
114120
cat << EOF > Sources/openapi.yaml
115121
openapi: 3.1.0
116122
info:
@@ -155,12 +161,12 @@ paths:
155161
content:
156162
application/json:
157163
schema:
158-
$ref: '#/components/schemas/quote'
164+
\$ref: '#/components/schemas/quote'
159165
400:
160166
description: Bad Request
161167
404:
162168
description: Not Found
163-
EOF
169+
EOF
164170
```
165171

166172
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
170176
generate:
171177
- types
172178
- server
173-
EOF
179+
EOF
174180
```
175181

176182
4. Use this `Package.swift` file to define targets and their dependencies
@@ -191,7 +197,7 @@ let package = Package(
191197
.executable(name: "QuoteService", targets: ["QuoteService"]),
192198
],
193199
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")),
195201
.package(url: "https://github.com/apple/swift-openapi-runtime.git", .upToNextMinor(from: "1.0.0")),
196202
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "1.0.0-alpha.1"),
197203
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main"),
@@ -274,6 +280,12 @@ swift build
274280
1. Add the Lambda build instructions as a Docker file and a Makefile. We build for Swift 5.9 on Amazon Linux 2
275281

276282
```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+
277289
cat << EOF > Dockerfile
278290
# image used to compile your Swift code
279291
FROM public.ecr.aws/docker/library/swift:5.9.1-amazonlinux2
@@ -298,34 +310,41 @@ tail:
298310
###################### No Change required below this line ##########################
299311

300312
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))
305317

306318
# build docker image to compile Swift for Linux
307319
docker build -f Dockerfile . -t swift-builder
308320

309321
# prep directories
310-
mkdir -p $($@BUILD_DIR)/lambda $($@ARTIFACTS_DIR)
322+
mkdir -p \$(\$@BUILD_DIR)/lambda \$(\$@ARTIFACTS_DIR)
311323

312324
# 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"
314326

315327
# 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"
317329

318330
# copy binary to stage
319-
cp $($@BUILD_DIR)/release/$($@PRODUCT) $($@STAGE)/bootstrap
331+
cp \$(\$@BUILD_DIR)/release/\$(\$@PRODUCT) \$(\$@STAGE)/bootstrap
320332

321333
# copy app from stage to artifacts dir
322-
cp $($@STAGE)/* $($@ARTIFACTS_DIR)
334+
cp \$(\$@STAGE)/* \$(\$@ARTIFACTS_DIR)
335+
323336
EOF
324337
```
325338
326339
2. Add a SAM template to deploy the Lambda function and the API Gateway
327340
328341
```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+
329348
cat << EOF > template.yml
330349
AWSTemplateFormatVersion: '2010-09-09'
331350
Transform: AWS::Serverless-2016-10-31
@@ -357,7 +376,7 @@ Resources:
357376
Outputs:
358377
SwiftAPIEndpoint:
359378
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"
361380
EOF
362381
```
363382
@@ -370,8 +389,16 @@ sam build
370389
4. Deploy the Lambda function and create an API Gateway in front of it
371390
372391
```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
375402
```
376403
377404
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
388415
5. Test your setup
389416
390417
```sh
391-
curl https://747ukfmah7.execute-api.us-east-1.amazonaws.com/stocks/AAPL
418+
curl [[ Replace with SWIFTAPIEndpoint value ]]/stocks/AAPL
392419
{
393420
"change" : -4,
394421
"changePercent" : -0.030052760210257923,
@@ -399,6 +426,10 @@ curl https://747ukfmah7.execute-api.us-east-1.amazonaws.com/stocks/AAPL
399426
}
400427
```
401428
429+
6. Delete the infrastructure
430+
431+
There is no cost
432+
402433
## Local Testing
403434
404435
```

0 commit comments

Comments
 (0)