Skip to content

Commit 7a76c32

Browse files
committed
Update Examples for Tencent Cloud and SCF
1 parent 13e6b2f commit 7a76c32

29 files changed

+210
-425
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
xcuserdata
99
Package.resolved
1010
.serverless
11-
docker
11+
docker
12+
.env

Examples/CloudFunctions/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
FROM swift:5.2-amazonlinux2
2-
3-
RUN yum -y install zip
1+
FROM stevapple/swift-scf:5.2.5

Examples/CloudFunctions/README.md

Lines changed: 57 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -13,165 +13,93 @@ cd swift-tencent-scf-runtime/Examples/CloudFunctions
1313

1414
Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.
1515

16-
## Deployment instructions using AWS CLI
16+
## Deployment instructions using TCCLI
1717

18-
Steps to deploy this sample to AWS Lambda using the AWS CLI:
18+
Steps to deploy this sample to Tencent SCF Platform using TCCLI:
1919

20-
1. Login to AWS Console and create an AWS Lambda with the following settings:
21-
* Runtime: Custom runtime
22-
* Handler: Can be any string, does not matter in this case
20+
1. Login to SCF Console and create a cloud function
21+
2. Build, package and deploy the function
2322

24-
2. Build, package and deploy the Lambda
25-
26-
```
27-
./scripts/deploy.sh
28-
```
29-
30-
Notes:
31-
- This script assumes you have AWS CLI installed and credentials setup in `~/.aws/credentials`.
32-
- The default lambda function name is `SwiftSample`. You can specify a different one updating `lambda_name` in `deploy.sh`
33-
- Update `s3_bucket=swift-lambda-test` in `deploy.sh` before running (AWS S3 buckets require a unique global name)
34-
- Both lambda function and S3 bucket must exist before deploying for the first time.
35-
36-
### Deployment instructions using AWS SAM (Serverless Application Model)
37-
38-
AWS [Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM) is an open-source framework for building serverless applications. This framework allows you to easily deploy other AWS resources and more complex deployment mechanisms such a CI pipelines.
39-
40-
***Note:*** Deploying using SAM will automatically create resources within your AWS account. Charges may apply for these resources.
41-
42-
To use SAM to deploy this sample to AWS:
43-
44-
1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
45-
46-
2. Install SAM CLI by following the [instructions](https://aws.amazon.com/serverless/sam/).
47-
48-
3. Build, package and deploy the Lambda
49-
50-
```
51-
./scripts/sam-deploy.sh --guided
52-
```
53-
54-
The script will ask you which sample Lambda you wish to deploy. It will then guide you through the SAM setup process.
55-
56-
```
57-
Setting default arguments for 'sam deploy'
58-
=========================================
59-
Stack Name [sam-app]: tencent-scf-runtime-sample
60-
AWS Region [us-east-1]: <your-favourite-region>
61-
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
62-
Confirm changes before deploy [y/N]: Y
63-
#SAM needs permission to be able to create roles to connect to the resources in your template
64-
Allow SAM CLI IAM role creation [Y/n]: Y
65-
Save arguments to samconfig.toml [Y/n]: Y
66-
```
67-
68-
If you said yes to confirm changes, SAM will ask you to accept changes to the infrastructure you are setting up. For more on this, see [Cloud Formation](https://aws.amazon.com/cloudformation/).
69-
70-
The `sam-deploy` script passes through any parameters to the SAM deploy command.
71-
72-
4. Subsequent deploys can just use the command minus the `guided` parameter:
73-
74-
```
75-
./scripts/sam-deploy.sh
76-
```
77-
78-
The script will ask you which sample Lambda you wish to deploy. If you are deploying a different sample lambda, the deploy process will pull down the previous Lambda.
79-
80-
SAM will still ask you to confirm changes if you said yes to that initially.
81-
82-
5. Testing
83-
84-
For the API Gateway sample:
85-
86-
The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` which you can use to test the Lambda. For example:
87-
88-
```
89-
curl <<LambdaApiGatewayEndpoint>>
90-
```
91-
92-
***Warning:*** This SAM template is only intended as a sample and creates a publicly accessible HTTP endpoint.
93-
94-
For all other samples use the AWS Lambda console.
23+
```
24+
./scripts/deploy.sh
25+
```
9526

96-
### Deployment instructions using Serverless Framework (serverless.com)
27+
Notes:
28+
- This script assumes you have TCCLI installed and user configured (See https://cloud.tencent.com/document/product/440/34012).
29+
- You'll be prompted to provide the COS bucket ID and region, and the SCF function name and region.
30+
- Both the cloud function and COS bucket must exist before deploying for the first time.
9731

98-
[Serverless framework](https://www.serverless.com/open-source/) (Serverless) is a provider agnostic, open-source framework for building serverless applications. This framework allows you to easily deploy other AWS resources and more complex deployment mechanisms such a CI pipelines. Serverless Framework offers solutions for not only deploying but also testing, monitoring, alerting, and security and is widely adopted by the industry and offers along the open-source version a paid one.
32+
### Deployment instructions using Serverless Framework for Tencent (serverless.com/cn)
9933

100-
***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources.
34+
[Serverless framework](https://www.serverless.com/open-source/) (Serverless) is a provider agnostic, open-source framework for building serverless applications. [Serverless framework for Tencent](https://www.serverless.com/cn/) is a highly customized version that allows you to easily deploy other Tencent Cloud resources and more complex deployment mechanisms such a CI pipelines. Serverless Framework offers solutions for not only deploying but also testing, monitoring, alerting, and security and is widely adopted by the industry, and the Tencent Cloud version is totally free.
10135

102-
To use Serverless to deploy this sample to AWS:
36+
***Note:*** Deploying using Serverless will automatically create resources within your Tencent Cloud account. Charges may apply for these resources.
10337

104-
1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
38+
To use Serverless to deploy this sample to Tencent Cloud:
10539

106-
2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
107-
If you already have installed be sure you have the latest version.
108-
The examples have been tested with the version 1.72.0.
40+
1. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
41+
If you already have installed, be sure you have the latest version.
42+
The examples have been tested with the version 1.78.1.
10943

11044
```
111-
Serverless --version
112-
Framework Core: 1.72.0 (standalone)
113-
Plugin: 3.6.13
45+
$ serverless --version
46+
Framework Core: 1.78.1
47+
Plugin: 3.7.0
11448
SDK: 2.3.1
115-
Components: 2.30.12
49+
Components: 2.34.1
11650
```
11751

118-
3. Build, package and deploy the Lambda
52+
2. Build, package and deploy the cloud function
11953

120-
```
121-
./scripts/serverless-deploy.sh
122-
```
123-
124-
The script will ask you which sample Lambda you wish to deploy.
125-
126-
The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command.
54+
```
55+
./scripts/serverless-deploy.sh
56+
```
12757

128-
4. Testing
58+
The script will ask you which sample function you wish to deploy.
12959

130-
For the APIGateway sample:
60+
3. Test
13161

132-
The Serverless template will provide an endpoint which you can use to test the Lambda.
62+
For the APIGateway sample, the Serverless template provides an endpoint with API Gateway which you can use to test the cloud function.
13363

13464
Outuput example:
13565

13666
```
137-
...
138-
...
139-
Serverless: Stack update finished...
140-
Service Information
141-
service: apigateway-swift-aws
142-
stage: dev
143-
region: us-east-1
144-
stack: apigateway-swift-aws-dev
145-
resources: 12
146-
api keys:
147-
None
148-
endpoints:
149-
GET - https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
150-
functions:
151-
httpGet: apigateway-swift-aws-dev-httpGet
152-
layers:
153-
None
154-
155-
Stack Outputs
156-
HttpGetLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:XXXXXXXXX:function:apigateway-swift-aws-dev-httpGet:1
157-
ServerlessDeploymentBucketName: apigateway-swift-aws-dev-serverlessdeploymentbuck-ud51msgcrj1e
158-
HttpApiUrl: https://r39lvhfng3.execute-api.us-east-1.amazonaws.com
67+
$ sls deploy
68+
69+
serverless ⚡ framework
70+
Action: "deploy" - Stage: "dev" - App: "SwiftAPIGatewayDemo" - Instance: "SwiftAPIGatewayDemo"
71+
72+
functionName: apigateway-swift-scf
73+
description: Swift SCF demo for APIGateway
74+
namespace: default
75+
runtime: CustomRuntime
76+
handler: swift.main
77+
memorySize: 64
78+
lastVersion: $LATEST
79+
traffic: 1
80+
triggers:
81+
apigw:
82+
- http://service-jyl9i6mc-1258834142.gz.apigw.tencentcs.com/release/api
83+
84+
Full details: https://serverless.cloud.tencent.com/apps/SwiftAPIGatewayDemo/SwiftAPIGatewayDemo/dev
85+
86+
31s › SwiftAPIGatewayDemo › Success
15987
```
16088

16189
For example:
16290

163-
```
164-
curl https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
165-
```
91+
```
92+
curl http://service-jyl9i6mc-1258834142.gz.apigw.tencentcs.com/release/api
93+
```
16694

16795
***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint.
16896

169-
For all other samples use the AWS Lambda console.
97+
For extensive usage, you need to customize `serverless.yml` yourself.
17098

17199
4. Remove
172100

173-
```
174-
./scripts/serverless-remove.sh
175-
```
101+
```
102+
./scripts/serverless-remove.sh
103+
```
176104

177-
The script will ask you which sample Lambda you wish to remove from the previous depolyment.
105+
The script will ask you which sample function you wish to remove from the previous deployment.

Examples/CloudFunctions/scripts/SAM/APIGateway-template.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

Examples/CloudFunctions/scripts/SAM/Benchmark-template.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

Examples/CloudFunctions/scripts/SAM/CurrencyExchange-template.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

Examples/CloudFunctions/scripts/SAM/ErrorHandling-template.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

Examples/CloudFunctions/scripts/SAM/HelloWorld-template.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

Examples/CloudFunctions/scripts/build-and-package.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ executable=$1
3232
workspace="$(pwd)/../.."
3333

3434
echo "-------------------------------------------------------------------------"
35-
echo "preparing docker build image"
35+
echo "Preparing docker build image"
3636
echo "-------------------------------------------------------------------------"
3737
docker build . -t builder
3838
echo "done"
3939

4040
echo "-------------------------------------------------------------------------"
41-
echo "building \"$executable\" lambda"
41+
echo "Building \"$executable\" SCF"
4242
echo "-------------------------------------------------------------------------"
43-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
43+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/CloudFunctions builder \
4444
bash -cl "swift build --product $executable -c release"
4545
echo "done"
4646

4747
echo "-------------------------------------------------------------------------"
48-
echo "packaging \"$executable\" lambda"
48+
echo "Packaging \"$executable\" SCF"
4949
echo "-------------------------------------------------------------------------"
50-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
50+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/CloudFunctions builder \
5151
bash -cl "./scripts/package.sh $executable"
5252
echo "done"

Examples/CloudFunctions/scripts/config.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ DIR="$(cd "$(dirname "$0")" && pwd)"
3030
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
3131

3232
if [[ ${#executables[@]} = 0 ]]; then
33-
echo "no executables found"
33+
echo "No executables found"
3434
exit 1
3535
elif [[ ${#executables[@]} = 1 ]]; then
3636
executable=${executables[0]}
3737
elif [[ ${#executables[@]} > 1 ]]; then
38-
echo "multiple executables found:"
38+
echo "Multiple executables found:"
3939
for executable in ${executables[@]}; do
4040
echo " * $executable"
4141
done
4242
echo ""
43-
read -p "select which executables to deploy: " executable
43+
read -p "Select which executable to deploy: " executable
4444
fi
4545

4646
echo "-------------------------------------------------------------------------"
47-
echo "configuration"
47+
echo "Configuration"
4848
echo "-------------------------------------------------------------------------"
4949
echo "current dir: $DIR"
5050
echo "executable: $executable"

0 commit comments

Comments
 (0)