DevOps for Serverless Applications
上QQ阅读APP看书,第一时间看更新

Deploying and invoking locally 

As we are now able to successfully invoke and test the function locally, we should be fine to deploy and test it remotely. First, we need to make sure that we have retrieved and exported the access and secret access keys as environment variables, as shown in the following code: 

$ export AWS_ACCESS_KEY_ID=<access-key-id>
$ export AWS_SECRET_ACCESS_KEY=<secret-key-id>

Now I am deploying the function and API to the AWS Cloud through a simple deploy command. Behind the scenes, the deployment process will create a .serverless folder with CloudFormation JSON templates. The serverless code is packaged into a .zip file and a serverless state JSON file. If we look into the create-stack JSON template, Serverless Framework will create an S3 bucket on the AWS Cloud and deploy the function and API package on to the bucket with the CloudFormation template JSON file. It will also keep the state of the deployment in the form of a JSON file. The successful deployment will provision an API endpoint that is tied to an AWS Lambda function and creates a service as mentioned in the provider: 

$ serverless deploy

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (417 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............................
Serverless: Stack update finished...
Service Information
service: my-serverless-service
stage: dev
region: us-east-1
stack: my-serverless-service-dev
api keys:
None
endpoints:
GET - https://kn6esoolgi.execute-api.us-east-1.amazonaws.com/dev/handler
 functions:
hello: my-serverless-service-dev-hello

Postdeployment, we will go to the AWS portal and see whether the function is deployed. Then we will invoke it through the portal and then again invoke the remote function through the serverless CLI, using the following steps: 

  1. Log in to the AWS portal, then select the correct region where the function and API was deployed. 
  2. Select the service as Lambda. The Lambda portal page will show the function name deployed. Select the function that you test using the radio button, then go to the drop-down marked Actions and select the option marked Test. The window will pop up to configure the event. You can add your own event or keep the default, and then click on Save. The event will be saved and the page will be redirected to the Function page. Now click on the Test button. The function will be executed, and the execution status and results will be displayed.

As I mentioned earlier, we can invoke the remote function locally as well. We will now look at the various commands for the remote Lambda function: 

To invoke the function and get the logs, use the following command: 

 $ serverless invoke -f hello -l
{
"statusCode": 200,
"body": "{\"message\":\"My Serverless World\",\"input\":{}}"
}
--------------------------------------------------------------------
START RequestId: c4bdc7f8-60e1-11e8-9846-f5267af11144 Version: $LATEST
END RequestId: c4bdc7f8-60e1-11e8-9846-f5267af11144
REPORT RequestId: c4bdc7f8-60e1-11e8-9846-f5267af11144 Duration: 44.37 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB

To just get the logs for the previous invocation, enter the following code. We can do this in a separate console to test the working:

$ serverless logs -f hello -t
START RequestId: 75139847-60dc-11e8-8c0b-4f4996ceffb3 Version: $LATEST
END RequestId: 75139847-60dc-11e8-8c0b-4f4996ceffb3
REPORT RequestId: 75139847-60dc-11e8-8c0b-4f4996ceffb3 Duration: 9.04 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB
START RequestId: 675574ec-60de-11e8-bbd5-8bf1c1e7f05c Version: $LATEST
END RequestId: 675574ec-60de-11e8-bbd5-8bf1c1e7f05c
REPORT RequestId: 675574ec-60de-11e8-bbd5-8bf1c1e7f05c Duration: 18.15 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB

The command will undeploy the function from the Lambda and also remove the package details from the S3 bucket. Once serverless remove runs successfully (as shown in the following code), you can log in to the AWS portal and check the S3 bucket and the Lambda function for the specific region—it should be removed:  

$ serverless remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
.........
Serverless: Stack removal finished...