Skip to main content

Examples

Below you'll find real-world examples that demonstrate how the Terraform Editor API transforms your code. Each example provides:

  1. A Request JSON you'd send to the API
  2. The Before and After Terraform code showing the transformation
  3. Key Takeaways explaining what happened

This page provides examples of common operations with the Terraform Editor API.

  • Best Practice: Read these examples in sequence to understand the full API capability
  • Pro Tip: Compare the "Before" and "After" sections to understand what changes each operation makes

1. Adding a New Resource with Tags

Scenario

You want to create a new AWS S3 bucket called my-example-bucket and add some default tags. There is no existing Terraform code to start with.

Request

{
"code": "",
"edits": {
"add": {
"resource": {
"aws_s3_bucket": {
"my_example_bucket": [
{
"attributes": {
"bucket": "my-example-bucket",
"tags": {
"Environment": "dev",
"Owner": "team-xyz"
}
}
}
]
}
}
}
}
}

Result

Before

After

# (No existing Terraform code)
resource "aws_s3_bucket" "my_example_bucket" {
bucket = "my-example-bucket"

tags = {
Environment = "dev"
Owner = "team-xyz"
}
}

Key Takeaways

  • add fails if a block already exists. Since this is empty code, we're safe to create a brand-new S3 bucket.
  • The resulting Terraform code includes the new resource definition with the specified tags.

2. Updating an Existing Provider's Region

Scenario

You have a provider "google" block with an alias of "europe". You want to change its region attribute from europe-west1 to europe-west2.

Request

{
"code": "provider \"google\" {\n alias = \"europe\"\n region = \"europe-west1\"\n}\n",
"edits": {
"update": {
"provider": {
"google": [
{
"where": {
"alias": "europe"
},
"attributes": {
"region": "europe-west2"
}
}
]
}
}
}
}

Result

Before

After

provider "google" {
alias = "europe"
region = "europe-west1"
}
provider "google" {
alias = "europe"
region = "europe-west2"
}

Key Takeaways

  • update requires exactly one matching block. We used a where selector on alias = "europe", so it finds and updates the correct block.
  • If the provider block didn't exist, this operation would fail because update can't create new blocks.

3. Mixed Operations: Adding a Resource and Deleting Another

Scenario

You currently have an aws_instance "old" in your code. You'd like to delete that instance and add a new one called "new_one" in a single API call.

Request

{
"code": "resource \"aws_instance\" \"old\" {\n ami = \"ami-1111\"\n instance_type = \"t2.small\"\n}\n",
"edits": {
"add": {
"resource": {
"aws_instance": {
"new_one": [
{
"attributes": {
"ami": "ami-9999",
"instance_type": "t2.micro"
}
}
]
}
}
},
"delete": {
"resource": {
"aws_instance": {
"old": []
}
}
}
}
}

Result

Before

After

resource "aws_instance" "old" {
ami = "ami-1111"
instance_type = "t2.small"
}
resource "aws_instance" "new_one" {
ami = "ami-9999"
instance_type = "t2.micro"
}

Key Takeaways

  • Multiple operations (add + delete) happen in one request.
  • The old resource block is completely removed, and the new_one block is created.

4. Nested Operations for Sub-Blocks

Scenario

In an aws_instance "db" resource, you have one existing ebs_block_device. You want to update its volume_size and add a second ebs_block_device at the same time.

Request

{
"code": "resource \"aws_instance\" \"db\" {\n ami = \"ami-1234abcd\"\n instance_type = \"t2.small\"\n\n ebs_block_device {\n device_name = \"/dev/sdf\"\n volume_size = 100\n }\n}",
"edits": {
"update": {
"resource": {
"aws_instance": {
"db": [
{
"update": {
"blockTypes": {
"ebs_block_device": [
{
"where": {
"device_name": "/dev/sdf"
},
"attributes": {
"volume_size": 200
}
}
]
}
},
"add": {
"blockTypes": {
"ebs_block_device": [
{
"attributes": {
"device_name": "/dev/sdg",
"volume_size": 150
}
}
]
}
}
}
]
}
}
}
}
}

Result

Before

After

resource "aws_instance" "db" {
ami = "ami-1234abcd"
instance_type = "t2.small"

ebs_block_device {
device_name = "/dev/sdf"
volume_size = 100
}
}
resource "aws_instance" "db" {
ami = "ami-1234abcd"
instance_type = "t2.small"

ebs_block_device {
device_name = "/dev/sdf"
volume_size = 200
}

ebs_block_device {
device_name = "/dev/sdg"
volume_size = 150
}
}

Key Takeaways

  • Nested update modifies the existing EBS block device where device_name = "/dev/sdf".
  • Nested add adds a brand-new EBS block device in the same aws_instance.

5. Deleting Specific Attributes But Keeping the Block

Scenario

Your aws_instance "example" includes a couple of attributes—user_data and iam_instance_profile—that you want to remove without deleting the entire block.

Request

{
"code": "resource \"aws_instance\" \"example\" {\n ami = \"ami-1234abcd\"\n instance_type = \"t2.medium\"\n user_data = \"#!/bin/bash\\necho hello\"\n iam_instance_profile = \"my-profile\"\n}",
"edits": {
"delete": {
"resource": {
"aws_instance": {
"example": [
{
"attributes": [
"user_data",
"iam_instance_profile"
]
}
]
}
}
}
}
}

Result

Before

After

resource "aws_instance" "example" {
ami = "ami-1234abcd"
instance_type = "t2.medium"
user_data = "#!/bin/bash\necho hello"
iam_instance_profile = "my-profile"
}
resource "aws_instance" "example" {
ami = "ami-1234abcd"
instance_type = "t2.medium"
}

Key Takeaways

  • Attributes under delete are removed, but the block remains.
  • No selector needed because there's only one aws_instance.example.

6. Using set with where to Avoid Duplication

Scenario

There are two provider "google" blocks. One is labeled alias = "dev", the other is alias = "prod". You want to update or create (upsert) the block where alias = "prod" to set its region to "us-west1".

Request

{
"code": "provider \"google\" {\n alias = \"dev\"\n region = \"us-east1\"\n}\n\nprovider \"google\" {\n alias = \"prod\"\n region = \"us-central1\"\n}\n",
"edits": {
"set": {
"provider": {
"google": [
{
"where": {
"alias": "prod"
},
"attributes": {
"region": "us-west1"
}
}
]
}
}
}
}

Result

Before

After

provider "google" {
alias = "dev"
region = "us-east1"
}

provider "google" {
alias = "prod"
region = "us-central1"
}
provider "google" {
alias = "dev"
region = "us-east1"
}

provider "google" {
alias = "prod"
region = "us-west1"
}

Key Takeaways

  • set checks for a matching block (using "where": {"alias": "prod"}) and updates it if found.
  • If "prod" didn't exist, set would create it.
  • If multiple blocks matched alias = "prod", an error would occur unless you further disambiguate.

Conclusion

These examples demonstrate how you can:

  1. Create and modify Terraform configurations with a powerful, JSON-based API
  2. Perform operations on both complete and empty files
  3. Work with different block types: resources, variables, outputs, providers, data sources, and more
  4. Create, update, and delete blocks with various attributes
  5. Target specific blocks using selectors
  6. Incrementally modify your configuration

For more specialized operations:

Check API Reference for a complete list of operations and Core Concepts to dive deeper into the API design.