Author | Liu Yu (flower name: Jiang Yu)
In the last article "Learn to Learn Serverless | How to Solve the Problem of Serverless Application Development and Deployment" , we explained the importance of the tool chain, so this article will lead you to quickly realize the introduction of Serverless Devs.
installation tool
- Step 1: Please install Node.js(>=10.8.0) and NPM package management tool first;
- Step 2: Install the Serverless Devs developer tools; refer to the documentation for the specific installation method:
https://help.aliyun.com/document_detail/195474.html
$ npminstall @serverless-devs/s -g
- Step 3: You can judge whether the tool is installed successfully through s -v. If the installation is successful, you can see the corresponding version information, for example:
@serverless-devs/s:2.0.89, @serverless-devs/core: 0.1.7, darwinarwin-x64, node-v12.15.0
configure key
get key
To configure the Alibaba Cloud key of Serverless Devs, the key information is generally required. The obtaining page is:
https://usercenter.console.aliyun.com/#/manage/ak_
- AccessKeyID: User's AK information
- AccessKeySecret: User's SK information
The process of obtaining key information is as follows: Open the page for obtaining keys; _ https://usercenter.console.aliyun.com/#/manage/ak_
and get the key info:
Guided key configuration
Guided key configuration: Guided creation can be done with the s config add command:
Execute s config add and select Alibaba Cloud (alibaba):
$ s config add? Please select a template: Alibaba Cloud(alibaba)🧭Refer to the document for alibaba key: http://config.devsapp.net/account/alibaba? AccountID ()
At this point, you can follow the instructions to configure the key:
? Please select a template: Alibaba Cloud(alibaba)
🧭Refer to the document for alibaba key: http://config.devsapp.net/account/alibaba
? AccessKeyID 此处填写AccessKeyID
? AccessKeySecret 此处填写AccessKeySecret
? Please create alias for key pair. Ifnot, please enter to skip alibaba-access
Alias: alibaba-access AccountID:此处填写AccountID
AccessKeyID: 此处填写AccessKeyID
AccessKeySecret: 此处填写AccessKeySecret
Configuration successful
So, through guided, we have completed the key configuration information.
Imperative key configuration
In order to better use Serverless Devs in some automated processes, in addition to guided key configuration, Serverless Devs also supports non-interactive key configuration through the command line.
Also taking Alibaba Cloud key configuration as an example, you can directly pass in the key information through parameters:
$ s configadd --AccessKeyID myAccessKeyID --AccessKeySecret myAccessKeySecret -a demoAlias: demoAccountID: myAccountIDAccessKeyID: myAccessKeyIDAccessKeySecret: myAccessKeySecretConfigurationsuccessful
If you need to configure key information in more formats, you can implement it by customizing Key-Value, for example:
$ s config add--AccessKeyID ****** -kl key1,key2,key3 -il info1,info2,info3 -a demo-2Alias: demo-2key1: info1key2: info2key3: info3AccessKeyID: codeConfigurationsuccessful
Configured via environment variables
A considerable number of developers will put the key information in the environment variable, so when using the tool, it is necessary to read the key information from the environment variable. At this time, the method of configuring the key through the environment variable is particularly important. Important, for this, Serverless Devs provides two ways to configure keys via environment variables:
Method 1: Configure directly through config add
This method is very simple. It is basically similar to the imperative key configuration described above, except that instead of a fixed value, an environment variable is passed in. For example, in the environment variable:
ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET and other related content;
At this point, it can be added through the s configadd command:
$ s config add -adefault-aliyun -kl AccessKeyID,AccessKeySecret -il${ALIBABA_CLOUD_ACCESS_KEY_ID},${ALIBABA_CLOUD_ACCESS_KEY_SECRET}
Method 2: Use environment variable key by specifying name
Configure by specifying the name of the environment variable: For example, there is currently an Alibaba Cloud key pair:
AccountID:temp_accountid
AccessKeyID:temp_accesskeyid
AccessKeySecret:temp_accesskeysecret
At this point, the key can be named in the environment variable as *_serverless_devs_access,
For example default_serverless_devs_access, the value is a JSON string, such as:
Key:default_serverless_devs_access
Value:{\"AccessKeyID\":\"temp_accesskeyid\",\"AccessKeySecret\":\"temp_accesskeysecret\"}
Notes on key usage
safety related
The cloud account AccessKey is your key to access the Alibaba Cloud API. It has full permissions for the account. Please keep it safe! Do not expose the AccessKey to external channels in any way (eg Github) to avoid being used by others to cause security threats. It is strongly recommended that developers follow Alibaba Cloud security best practices and use the RAM sub-user AccessKey to make API calls.
Practical Thoughts on Alias in Key Configuration**
In Serverless Devs, in addition to configuring the key information provided by the cloud vendor, additional Alias settings are required. The so-called Alias here refers to the alias settings for keys. Since Serverless Devs supports the configuration and management of multiple keys, in general, one alias corresponds to one key pair. Relevant best practices can be:
- There are two accounts, namely Alibaba Cloud account and Tencent Cloud account. When configuring the key, you can set aliases alibaba and tencent. When using it, use different keys by referencing different aliases to prevent each time the password is switched. When the key is used, the key is reconfigured;
- It owns two environment keys, one is the test environment key test, and the other is the online environment key release. When the development is completed, the business needs to be deployed to different environments, which can be performed directly by specifying the key. Deployment without repeated key reconfiguration due to key switching;
How to use the key
In Serverless Devs, keys are used at two levels:
- Command line level: When used in the command line, you can use it directly through the -a/--access parameter. For example, when deploying a business, you can specify the demo key pair through s deploy-a demo;
- Yaml configuration file level: You can specify key pairs in Yaml, for example:
Specifying access directly in Yaml means that the entire application is deployed through the current key pair, or you can specify the current module/service under a module/service to deploy using a specified key pair.
The order of key usage is related
The key supports multiple forms of use, and the order of use of the key will also arise:
- Key information specified by the -a/--access parameter
- Use the configured default key information
- Use
`default_serverless_devs_access
key information configured through environment variables - Configure boot without key info/enter key info
The specific flow chart is:
Other operations related to keys
In Serverless Devs, in addition to configuring keys, it also includes modifying, deleting and viewing keys. At this time, you can view related functions through s config -h:
- To view the key, you can view the help through s config get -h;
- To modify the key, you can re-create the key of the specified alias, and forcibly overwrite it through the -f parameter;
For the deletion of the key, you can view the help through s config delete-h;
Epilogue
Serverless Devs can describe serverless applications through Yaml in a specified format. In the provisions of Serverless Devs, a Yaml can be considered as a serverless application. In the next article, I will introduce the usage specifications of Yaml in detail.
For more content, pay attention to the Serverless WeChat official account (ID: serverlessdevs), which brings together the most comprehensive content of serverless technology, regularly holds serverless events, live broadcasts, and user best practices.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。