In Infrastructure as Code for Beginners, I briefly discussed the role of AI tools such as ChatGPT. Since then, new features have emerged, making this the perfect time to revisit the topic and evaluate whether my initial recommendations have evolved. Let's quickly recap: GPT-powered chat services, including ChatGPT, certainly have their place in Infrastructure as Code (IaC) projects. However, we are not quite at the point where we can ask these tools to create our projects from scratch.
The primary reason for this is straightforward: GPT, while impressive, can make mistakes. This is fine, except that when GPT errors, it often does so by inventing information, which it then presents in a highly convincing manner. On the one hand, you have Infrastructure as Code which has been taking away the heavy lifting of managing infrastructure for quite a while; a lot of the tooling is well established even though it is still considered a fast-moving technology to most.
On the contrary, artificial intelligence tools such as ChatGPT show up and quite dramatically change what we thought was possible even just a year ago, impacting how we create, test, and maintain code.
Here are just some of the use cases I have been using the two together for in the last few months.
● Code Quality: You can have ChatGPT check your code and make recommendations,
● Cutting Down on Human Error: ChatGPT can double-check and make sure you are not doing something dumb if you ask it to.
● Boosting Productivity: You can ask ChatGTP to add inline comments to your code, update variables, and handle the boring stuff.
Remember, we're just scratching the surface here. The combo of AI and IaC is still fresh, and there's a lot of territory left to explore.
Rather than talk anymore, let’s take a look at implementing of the use cases mentioned above, to start with let’s have ChatGPT review an example Terraform block and make some recommendations on how it could be improved.
Here is the prompt that we can use:
I have the following Terraform code; it creates a Storage Account in Microsoft Azure;
resource "azurerm_storage_account" "example" {
name = "saiacforbeg2022111534"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "GRS"
}
Could you check it and provide recommendations on how it could be improved?
The prompt above made the following recommendations, all of which are valid observations:
Image 1: Recommendations that ChatGPT gave me.
As well as rewriting the code considering its recommendations, it also recognized that we are deploying an Azure Storage Account and pointed out the following:
Additionally, please remember that the storage account names in Azure need to be globally unique and must be between 3 and 24 characters in length, and can include numbers and lowercase letters only. Thus, you might want to consider adding checks to ensure that these constraints are met.
Now let’s ask ChatGPT to add some inline comments which explain what is going to the main block of code using the following prompt :
Please add inline comments explaining what is happening to the code below …
ChatGPT gave the following response:
Image 2: Fully commented code
Finally, we can ask ChatGTP to check and make some suggestions about how we could better secure our Storage Account using the following prompt:
Image 3: list of recommendations for improving our Terraform code.
Let’s see if ChatGPT could update the code with some of those suggestions with the following prompt and also have a look at the result:
Image 4: recommendations within the context of the original Terraform code.
How do other AI Chatbots handle our original prompt, let’s start with Bing;
Image 5: Bing Chat had to say about our Terraform code.
Well, that wasn’t helpful at all - let’s now see if Google Bard fares any better:
Image 6: Google Bard had to say about our Terraform code.
It didn’t just give us a thumbs up, which is a good start. It made similar recommendations to ChatGPT. However, while Bard made some suggestions, they were not as detailed, and it didn’t point out anything outside of what we asked it to, unlike ChatGPT, which recognized we were creating a storage account and gave us some additional pointers.
Now don’t write Microsoft off just yet. While Bing didn’t blow me away with its initial responses, Microsoft’s various Copilot services, all of which are launching soon, all of which are GPT4 based, I expect them to be up to the same level of responsiveness of ChatGTP and in the case of GitHub Copilot X I expect it to potentially surpass ChatGTP in terms of usefulness and convenience as it is going to be built directly into your IDE and have all of GitHub to learn from.
Given the rate of change, which feels like new tools and features and power is being added every other day now - the next 6 to 12 months is going to be an exciting time for anyone writing Infrastructure as Code as the tools we are using day-to-day start get more direct access to the AI tools we have been discussing.
In conclusion, this article delved into the process of improving code quality through various means, including seeking recommendations, incorporating inline code comments, and leveraging the capabilities of ChatGPT, Bing, and Google Bard. The demonstration emphasized the importance of continuously enhancing code and actively involving AI-powered tools to identify areas for improvement. Additionally, the article explored how ChatGPT's insights and suggestions were utilized to enhance storage security through stronger and more reliable code implementation. By employing these techniques, developers can achieve code optimization and bolster the overall security of their storage systems.
Russ McKendrick is an experienced DevOps practitioner and system administrator with a passion for automation and containers. He has been working in IT and related industries for the better part of 30 years. During his career, he has had responsibilities in many different sectors, including first-line, second-line, and senior support in client-facing and internal teams for small and large organizations.
He works almost exclusively with Linux, using open-source systems and tools across dedicated hardware and virtual machines hosted in public and private clouds at Node4, where he holds the title of practice manager (SRE and DevOps). He also buys way too many records!
Author of the book: Infrastructure as Code for Beginners