☁️CodeArts Setup with Terraform

Ertugrul Basar
Huawei Developers
Published in
6 min readMay 14, 2024
CodeArts — Terraform

🔦Introduction

Hi everyone, in today’s article, we will try to create a project and repository in Huawei Cloud’s DevSecOps platform, CodeArts, using Terraform instead of the cloud console. Throughout this process, we will configure Terraform, create a Terraform file, and then establish a project and repository within it. This work is solely focused on CodeArts, but it can also serve as a guideline for utilizing other cloud services on Huawei Cloud.

Before we begin to demo, I want to overview CodeArts and Terraform briefly.

1️⃣ CodeArts: CodeArts is a one-stop DevSecOps platform that provides out-of-the-box cloud services for requirement delivery, code commit, check, build, verification, deployment, and release throughout the entire software lifecycle. It consists of the following services: Req (Project Management), Repo (Code Hosting), Check (Code Quality), Build (Code Build), Artifact (Build Hosting), Deploy (Build Deployment), Pipeline (Continuous Delivery), TestPlan (Deployment Test).

2️⃣ Terraform: Terraform is an infrastructure-as-code tool that allows you to define both cloud and on-premises resources in human-readable configuration files that can be versioned, reused, and shared. Using a consistent workflow, you can then provision and manage all of your infrastructure throughout its lifecycle. Terraform is capable of managing low-level components such as compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features through their respective application programming interfaces (APIs).

💻Demo

Prerequisites for this demo

⚡️ CodeArts Subscription: CodeArts Subscription: To access CodeArts services, you will need a subscription. However, there’s no need to worry about payment because we will be using the Basic Version, which is entirely free for up to 10 users!

CodeArts Pricing

⚡️ Configured Terraform Path: If you’re unsure how to configure Terraform, you can refer to this link for guidance on the configuration process.

Step 1- Authentication

In Terraform, the `main.tf` file plays a central role in defining your infrastructure. It’s often considered the entry point for your Terraform configuration, containing provider information, resource definitions, and module calls. For today’s demonstration, I’ll be using Visual Studio Code as my editor, as it facilitates connecting through the terminal/command prompt. However, feel free to use any editor of your preference.

After I create the file, I will go to this link and then click on ‘USE PROVIDER’

Terraform — 1

In the dropdown menu that appears, you’ll find the information Terraform requires to communicate with the cloud provider, which in this case is Huawei Cloud.

Terraform — 2

Here, we need to provide three pieces of information: the region, AK (Access Key), and SK (Secret Key). If you don’t have an AK/SK pair, you can refer to this link for obtaining them, and here to retrieve the code of the region that will be utilized. Once you’ve finished providing input for the relevant fields, it should resemble the screenshot below.

Terraform — 3

Step 2- Creating a Project

Now, we will use Terraform to create resources, starting with a CodeArts project. In this document, there’s an example snippet we can utilize. Please copy the snippet and make the necessary modifications.

Terraform — 4

Next, let’s initiate the creation of the project. To begin, we need to establish a dialogue between our computer and the cloud. Type ‘terraform init’ into the terminal to start this process. (For a few steps, I’ll be sending screenshots from the terminal). This command will initiate the dialogue between Terraform and Huawei Cloud. Upon entering the ‘terraform init’ command, it will install a package that facilitates communication.

Terraform — 5

The next command we should enter is ‘terraform validate’. This command will check our file for any syntax errors. As depicted in the screenshot below, the commands in the file are valid.

Terraform — 6

Next, we should enter ‘terraform plan’ into the terminal. This command will show us the changes that will occur after executing/applying the file. In the command we use, we’re only creating a resource, and Terraform will outline the actions it will take, as shown in the screenshot below. This command helps us estimate the changes we are about to make and can help prevent errors.

Terraform — 7

If you’re certain that you want to execute the command, the next step is to enter ‘terraform apply’ into the terminal. This will create the project in the CodeArts platform. After execution, remember to copy the ID. (Otherwise, you may need to open your browser!)

Terraform — 8

You can see that the execution was successful from the screenshot below.

CodeArts — 1

Step 3- Creating a Repository

We’ve created the project, and now it’s time to create a repository. To do so, we can utilize the snippet provided here. I’ll make some changes to it and then use it as shown in the screenshot below.

Terraform — 9

It seems like there’s an issue with the commands. When you run ‘terraform plan’, it indicates that applying this command will destroy the project created in the last step and create a repository that isn’t associated with the desired project. This is a significant problem, so it’s wise not to apply this file. Let’s review the commands and ensure they’re correctly configured to avoid unintended consequences.

Terraform — 10

To apply it without any destruction, we need to delete the ‘terraform.tfstate’ file in the folder. You may need to delete it between all operations you perform in the future to prevent unintended consequences.

Terraform — 11

Next, we can perform the same operations, and you’ll see that the results match our expectations. This indicates that we can proceed with applying the changes.

Terraform — 12

The apply process is illustrated in the screenshot below.

Terraform — 13

Finally, we can confirm that the execution was successful, as depicted in the screenshot below.

CodeArts — 2

👉Conclusion

In today’s article, we successfully created a project and repository in CodeArts using Terraform, entirely without using the cloud console and relying solely on commands. This approach can be highly beneficial for automating various processes, especially in scenarios where access to the cloud console is limited. While CodeArts may not offer extensive Terraform support, you can utilize this example to adapt and apply similar techniques to your own work. Until the next article, goodbye!

💁References

--

--