CLI integration example
Alibaba Cloud CLI is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management and maintenance of Web Application Firewall 2.0. If you are not familiar with Alibaba Cloud CLI, see What is Alibaba Cloud CLI?. This topic describes the steps to use Alibaba Cloud CLI to call Web Application Firewall 2.0 by using an example of calling the DescribeInstanceSpecInfo API to query the specifications of a WAF instance.
Install Alibaba Cloud CLI
Before you can use Alibaba Cloud CLI, you must install it. Alibaba Cloud CLI is available for Windows, Linux, and macOS. Choose the installation instructions that match your operating system.
-
Windows: Install Alibaba Cloud CLI on Windows.
You can also use Cloud Shell to run and test Alibaba Cloud CLI commands. For more information about Cloud Shell, see What is Cloud Shell.
Configure Alibaba Cloud CLI
An Alibaba Cloud account has full permissions to access and manage all product APIs, which poses a significant security risk. We strongly recommend that you create a RAM identity, grant permissions based on the principle of least privilege, and use that RAM identity to access APIs.
Before you use Alibaba Cloud CLI, you must configure your identity credentials and a default region ID. Alibaba Cloud CLI supports multiple types of identity credentials. For more information, see Configure and manage credentials. This example uses an AccessKey. Follow these steps:
-
Create a RAM user and an AccessKey for that user to configure your identity credentials. For instructions, see Create a RAM user and Create an AccessKey.
-
Grant the necessary permissions to the RAM user. For this example, attach the
AliyunYundunWAFReadOnlyAccesspolicy, which provides read-only access to Web Application Firewall 2.0. For more information about system policies, see System policies for WAF. -
Obtain and record the ID of an available region. Alibaba Cloud CLI sends API requests to the region that you specify. For a list of available regions, see service endpoint.
NoteYou can use the
--regioncommand-line option to specify a region for a single command. This option overrides the default region configured in your credentials file and environment variables. For more information, see Command-line options. -
Use the RAM user's AccessKey to configure the credentials. In this example, the configuration profile is named
AkProfile. For detailed instructions, see Configure and manage credentials.
Generate a CLI command example
-
Click DescribeInstanceSpecInfo to go to OpenAPI Explorer.
On the Parameters tab, enter the request parameters. Then, click the CLI Example tab to view the generated CLI example.
Copy the CLI example or run it quickly in Cloud Shell:
Click the Run Command
button to open Cloud Shell and quickly debug the command.Click the Copy
button to copy the CLI example to the clipboard. You can then paste it into your local shell to run it.NoteWhen you run the CLI example in your local shell, check the parameter format. For more information about Alibaba Cloud CLI command parameter formats, see Understand command parameters.
The example generated by OpenAPI Explorer includes the
--regionoption by default. When you run the command locally, Alibaba Cloud CLI uses the specified region and ignores the default region in your configuration. You can keep or remove this option as needed.
Call an API
Example 1: List Web Application Firewall 2.0 OpenAPI operations
The following example shows how to use the --help option to list the Web Application Firewall 2.0 OpenAPI operations that you can call with Alibaba Cloud CLI. You can also view the list of supported APIs in the API overview.
Run the command.
aliyun waf-openapi helpThe command returns the following output.
Product: waf-openapi (Web Application Firewall) Version: 2019-09-10 Available Api List: CreateCertificate Uploads a certificate and private key for a domain. CreateCertificateByCertificateId Creates a certificate for a domain based on a certificate ID. CreateDomain Adds a domain to a WAF instance for protection. CreateProtectionModuleRule Creates a rule in a specified protection module, such as Web Intrusion Prevention, Data Security, Advanced Protection, Bot Management, Access Control, or Rate Limiting. DeleteDomain Deletes the configuration of a specified domain. DeleteInstance Releases a pay-as-you-go or an expired subscription WAF instance. DeleteProtectionModuleRule Deletes a rule from a specified protection module. DescribeCertMatchStatus Checks whether the uploaded certificate and private key for a domain match. DescribeCertificates Queries existing certificates that are available for a specified domain. These are certificates managed in the SSL Certificates service. DescribeDomain Queries the configuration information of a domain that has been added to WAF. DescribeDomainAdvanceConfigs Queries the advanced configurations of a domain that has been added to WAF. DescribeDomainBasicConfigs Queries the basic configurations of a domain that has been added to WAF. DescribeDomainList Queries the list of protected domains in a paginated manner.
Example 2: Query WAF instance specifications
The following code example shows you how to use the Alibaba Cloud CLI to call the DescribeInstanceSpecInfo command in Web Application Firewall 2.0 to query the specifications of a WAF instance.
Run the command.
aliyun waf-openapi DescribeInstanceSpecInfo \ --region cn-hangzhou \ --RegionId 'cn-hangzhou' \ --InstanceId 'waf-cn-st2225l****'The command returns the following output.
{ "RequestId": "E906513E-F6B5-495E-98DC-7BA8****1D76", "InstanceId": "waf-cn-st2225l****", "ExpireTime": 1677168000000, "Version": "version_3", "InstanceSpecInfos": [ { "Code": "103", "Value": "640" } ] }NoteIf a call to a Web Application Firewall 2.0 API operation returns an error, use the returned error code to check your request parameters and their values.
You can also record the request ID or the SDK error message from the response and use the Alibaba Cloud OpenAPI Diagnostics tool for self-service troubleshooting.