Can chatGPT write me a terraform infrastructure script?

The challenge

Writing a simple Terraform script by just asking a question.

The solution

Using chatGPT I asked the question “Can chatGPT write me a terraform script?”

Why Terraform?

We use Terraform to create infrastructure by code. Currently this is done by our staff. This is an experiment to see what chatGPT can do for us.

Warning, everything below this header is generated by AI

Yes, I can help you write a Terraform script! Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It uses configuration files to describe the desired state of your infrastructure, and then it automatically creates and manages that infrastructure for you.

Here’s a simple Terraform script that creates an Amazon Web Services (AWS) EC2 instance:

This script does the following:

It configures the AWS provider by setting the access_key, secret_key, and region values.

It creates an EC2 instance using the specified Amazon Machine Image (AMI) and instance type.

I hope this helps! Let me know if you have any other questions about Terraform or writing Terraform scripts.

Ok, below part is done by a real human again

The fact that chatGPT already knows Terraform is pretty amazing. Terraform is a very niche tool which is used to do infrastructure scripting. The script shown above does not prove anything in terms of AI so we need to adapt the prompt a little to make it more interesting.

Prompt: Write a Terraform script for Google Cloud for a Linux machine with 4 vcpus and 1 TB memory. Use the Eemshaven location. Also add 100 TB storage.

				
					resource "google_compute_instance" "vm" {
  name         = "my-vm"
  machine_type = "n1-standard-4"
  zone         = "europe-west4-c"

  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-1804-lts"
    }
  }

  memory = "1"
  memory_size = 1024

  metadata = {
    ssh-keys = "user:ssh-rsa KEY"
  }

  network_interface {
    network = "default"
  }

  service_account {
    scopes = ["https://www.googleapis.com/auth/cloud-platform"]
  }
}

resource "google_compute_disk" "disk" {
  name  = "my-disk"
  type  = "pd-standard"
  size  = 100
  zone  = "europe-west4-c"
}

resource "google_compute_instance_attached_disk" "disk" {
  instance = google_compute_instance.vm.name
  disk     = google_compute_disk.disk.name
}

				
			

The interesting part is is that chatGPT is aware of the machine types and zone, still not hugely exiting but it all depends on your prompts.

I’m not an infrastructure guy, but if you are, you will be able to easily create Terraform scripts.

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
LOGIN

Don’t have an account yet? Register and we’ll grant you access to our marketing and sales assets!