Creating a Function by Running Terraform Commands
Terraform is an open-source tool that lets you safely build, change, and version infrastructure. Just declare the final state of the infrastructure you want in the configuration files, without specifying how to implement the state.
Terraform advantages:
- More consistent architecture: Manual configuration errors and drift are reduced.
- Lower O&M costs: VMs are managed programmatically, reducing the need for manual hardware configurations and updates.
- Improved operational efficiency: System configuration, maintenance, and management are simplified, and software development and deployment are accelerated.
- Faster deployment: Complex configuration tasks are simplified using scripts, which speeds up application release.
- Fewer operation risks: Version control is supported, reducing configuration errors.
This section describes how to create a function using Terraform.
Prerequisites
You have obtained an access key.
For details about how to obtain an access key, see Access Keys. Use temporary access keys, which are more secure. For details, see Temporary Access Keys (Federated Users).
An access key comprises an access key ID (AK) and secret access key (SK), and is your long-term credential for accessing Huawei Cloud APIs.
After the access key is created, view the access key ID (AK) in the access key list in the access key list and view the secret access key (SK) in the downloaded CSV file.
Preparing a Terraform Environment
- Install Terraform.
Terraform provides installation packages for different environments. For details, see the Terraform official website.
The following uses Linux CentOS (public access required) as an example to describe how to install Terraform.
Log in to the system as the root user, create the /home/Terraform directory, run the cd command to go to this directory, and then run the following commands:
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo sudo yum -y install terraform
- View basic Terraform commands.
After Terraform commands are executed, command details are displayed. For details, see Terraform CLI.
- View basic Terraform syntax.
The Terraform configuration language is based on the HCL syntax. It is easy to configure and readable, and is compatible with the JSON syntax. For details, see Terraform official website.
Writing a Function Resource Script
Huawei Cloud has registered with Terraform as a provider. You can mount your functions to the provider as resources. For details, see huaweicloud_fgs_function.
The following is an example.
Create a main.tf file on the server, copy the following script to the file, and save it.
terraform { required_providers { huaweicloud = { source = "huaweicloud/huaweicloud" version = ">= 1.40.0" } } } provider "huaweicloud" { region = "cn-east-3" # Actual region access_key = "*******" # Replace it with the key generated in Prerequisites. secret_key = "*******" #Replace it with the key generated in Prerequisites. } resource "huaweicloud_fgs_function" "fgs_function" { name = "test_func_rf" app = "default" agency = "function-admin" description = "function test" handler = "index.handler" memory_size = 128 timeout = 3 runtime = "Python3.6" code_type = "inline" func_code = "aW1wb3J0IGpzb24KZGVmIGhhbmRsZXIgKGV2ZW50LCBjb250ZXh0KToKICAgIG91dHB1dCA9ICdIZWxsbyBtZXNzYWdlOiAnICsganNvbi5kdW1wcyhldmVudCkKICAgIHJldHVybiBvdXRwdXQ=" }
Creating a Function by Running Terraform Commands
- Go to the file path and run the following command to initialize a working directory that contains Terraform code.
terraform init
- Run the following command to apply the infrastructure configuration to the function and enter yes after Enter a value:.
terraform apply
- If the execution is successful, the function has been created.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot