官术网_书友最值得收藏!

The serverless toolkit

Before we finish this chapter, we are going to take a look at the serverless toolkit. This is an application that aims to provide a consistent experience when it comes to deploying your serverless functions across different cloud providers. You can find the service's homepage at https://serverless.com/.

As you can see from the home page, it supports both AWS and Microsoft Azure, as well as the Google Cloud Platforms and IBM OpenWhisk. You will also notice that there is a Sign Up button; click on this and follow the onscreen prompts to create your account.

Once signed up, you will receive some very simple instructions on how to install the tool and also deploy your first application; let's follow these now. First of all, we need to install the command-line tool by running:

$ npm install serverless -g

The installation will take a few minutes, and once it is installed you should be able to run:

$ serverless version

This will confirm the version that was installed by the previous command:

Now that the command-line tool is installed and we have confirmed that we can get the version number without any errors, we need to log in. To do this, run:

$ serverless login

This command will open a browser window and take you to a login page where you will need to select which account you wish to use:

As you can see in the preceding screenshot, it knows I last logged into serverless using my GitHub account, so clicking this will generate a Verification Code:

Pasting the code into your Terminal at the prompt and pressing Enter on your keyboard will then log you in:

Now that we are logged in, we can create our first project, which is going to be another hello-world application.

To launch our hello-world function in AWS, we must first create a folder to hold the artifacts created by the serverless toolkit and change to it; I created mine on my Desktop using:

$ mkdir ~/Desktop/Serverless
$ cd ~/Desktop/Serverless

To generate the files needed to launch our hello-world application, we need to run:

$ serverless create --template hello-world

This will return the following message:

Opening serverless.yml in my editor, I can see the following (I have removed the comments):

service: serverless-hello-world
provider:
name: aws
runtime: nodejs6.10
functions:
helloWorld:
handler: handler.helloWorld
# The `events` block defines how to trigger the handler.helloWorld code
events:
- http:
path: hello-world
method: get
cors: true

I updated the service to be russ-test-serverless-hello-world; you should choose something unique as well. Once I had saved my updated serverless.yml file, I ran:

$ serverless deploy

This, as you may have already guessed, deployed the hello-world application to AWS:

Access the endpoint URL using HTTPie:

$ http --body "https://5rwwylyo4k.execute-api.us-east-1.amazonaws.com/dev/hello-world"

This returns the following JSON:

{
"input": {
"body": null,
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "GB",
"Host": "5rwwylyo4k.execute-api.us-east-1.amazonaws.com",
"User-Agent": "HTTPie/0.9.9",
"Via": "1.1 dd12e7e803f596deb3908675a4e017be.cloudfront.net
(CloudFront)",
"X-Amz-Cf-Id": "bBd_ChGfOA2lEBz2YQDPPawOYlHQKYpA-
XSsYvVonXzYAypQFuuBJw==",
"X-Amzn-Trace-Id": "Root=1-59b417ff-5139be7f77b5b7a152750cc3",
"X-Forwarded-For": "109.154.205.250, 54.240.147.50",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"httpMethod": "GET",
"isBase64Encoded": false,
"path": "/hello-world",
"pathParameters": null,
"queryStringParameters": null,
"requestContext": {
"accountId": "687011238589",
"apiId": "5rwwylyo4k",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"apiKey": "",
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"sourceIp": "109.154.205.250",
"user": null,
"userAgent": "HTTPie/0.9.9",
"userArn": null
},
"path": "/dev/hello-world",
"requestId": "b3248e19-957c-11e7-b373-8baee2f1651c",
"resourceId": "zusllt",
"resourcePath": "/hello-world",
"stage": "dev"
},
"resource": "/hello-world",
"stageVariables": null
},
"message": "Go Serverless v1.0! Your function executed successfully!"
}

Entering the endpoint URL in your browser, (in my case as I am using Safari) shows you the RAW output:

Going to the URL mentioned at the very end of the serverless deploy command gives you an overview of the function you have deployed to Lambda using serverless:

Open the AWS Console by going to https://console.aws.amazon.com/, select Lambda from the Services menu, and then change to the region your function was launching in; this should show you your function:

At this point, you might scratch your head thinking, How was it launched in my account? I didn't provide any credentials! The serverless tool is designed to use the same credentials as the AWS CLI we installed before we launched our first Lambda function—these can be found at ~/.aws/credentials on your machine.

To remove the function, simply run:

$ serverless remove

And this will remove everything in your AWS account that the serverless toolkit has created.

For more information on how to use the serverless toolkit to launch an Azure Function, please see the quick-start guide which can be found at https://serverless.com/framework/docs/providers/azure/guide/quick-start/.

主站蜘蛛池模板: 将乐县| 东阿县| 陇西县| 陇南市| 肇东市| 兴山县| 金秀| 兴城市| 双峰县| 都安| 甘谷县| 阿克苏市| 瓦房店市| 霍邱县| 肇州县| 望都县| 阳春市| 天峨县| 罗源县| 虹口区| 长岭县| 宝应县| 通河县| 叶城县| 南澳县| 台湾省| 普兰店市| 安岳县| 蚌埠市| 行唐县| 漠河县| 延寿县| 瑞丽市| 正阳县| 屏东市| 泰宁县| 宜川县| 伊川县| 武威市| 凉城县| 泸州市|