Engineering

How to Export AWS to Terraform to Manage Infrastructure as Code

September 17, 2023
Ankur Khurana
Saubhagya Maheshwari

Managing AWS infrastructure through the console or other tools can quickly become overwhelming and complex. By exporting your AWS resources into Terraform, you can manage Infrastructure as Code, making it easier to scale and maintain your infrastructure.

Traditionally, converting AWS resources into Terraform code involves multiple, often tedious steps. That's why we developed the One-Click Terraform Generator – a tool designed to streamline this process and save you time.

In this post, I’ll show you how to leverage Kapstan’s Terraform Generator to efficiently manage your AWS account. Let’s dive in!

Step 1: Sign up on Kapstan

Head over to Kapstan to sign up for free. We've ensured the process is intuitive to get you started quickly.

Step 2: Connect Your AWS Account

Once you’re in, you’ll need to connect your AWS account. Kapstan only requires read-only access to ensure the safety of your infrastructure.

Step 3: Generate Terraform Code

With your account connected, hit the “Generate Terraform” button. In 5-7 minutes, your AWS infrastructure will be mirrored into a Terraform file, ready for deployment.

Step 4: Set up Terraform and AWS credentials locally

Install Terraform

If you're new to Terraform, you'll need it installed on your local machine:

# For macOS users
brew install terraform

# For Linux users
sudo apt-get install terraform

Install the AWS CLI

Install AWS CLI if you’ve never done that before. You can download it from the official AWS CLI website.

Configure AWS CLI

Once the AWS CLI is installed, open your terminal or command prompt and run:

aws configure

You'll be prompted to enter the following:

  • AWS Access Key ID: Enter your access key ID here.
  • AWS Secret Access Key: Enter your secret access key here.
  • Default region name: Enter your default AWS region (e.g., us-west-1).
  • Default output format: json

Step 5: Initialize and Apply Terraform Code

You’re now ready to use your Terraform Code. Go through the following steps:

  • Unzip the downloaded file into a folder and go to the folder.
  • You’ll see a list of resources. Go to a resource you plan to make changes to and open it.
  • Once you're in that resource folder, you'll run a command. Any terraform installation >0.13 requires this for terraformer generated files. Because we performed a fresh install, we'll have to run this command:

terraform state replace-provider -auto-approve "registry.terraform.io/-/aws" "hashicorp/aws"

Now, you're ready to initialize the Terraform directory. Navigate to where your generated Terraform file resides and execute:

terraform init

This prepares your directory, fetching necessary plugins and setting the stage for Terraform magic.

You're now ready to review your infrastructure changes. To see Terraform's action plan, run:

terraform plan

This command previews the operations Terraform will perform.

If you see any error that looks like the following:

> "name_prefix": conflicts with name

Then run the following command:

# For macOS users
find .  -type f -name "*.tf" -exec sed -i ''  '/name_prefix/d' {} +

# For Linux users
find .  -type f -name "*.tf" -exec sed -i  '/name_prefix/d' {} +

Satisfied with the plan? You're now ready to deploy your Terraform code. Deploy it:

terraform apply

You'll have to acknowledge the changes, and then Terraform will start orchestrating your AWS resources. If you didn't make any changes, you'll get the following:

No changes. Your infrastructure matches the configuration.

Step 6: Managing and Modifying Your Infrastructure

The beauty of Terraform lies in its simplicity. To illustrate, let's add an AWS S3 bucket. We'll update the s3 resources folder by editing s3_bucket.tf :

resource "aws_s3_bucket" "my_first_terraform_bucket" {
bucket = "my_first_terraform_bucket"
acl = "private"
}

Incorporate the above snippet into your Terraform file, execute "terraform apply", and your new S3 bucket is provisioned!

Conclusion

At Kapstan, we’re committed to improving developer productivity by simplifying the cloud management experience. The Kapstan Terraform Generator offers a streamlined approach to AWS management through Infrastructure as Code (IaC).  

Kapstan is an internal developer platform that helps forward-thinking engineering teams streamline their DevSecOps processes. Kapstan enables developer self-service and helps companies build, deploy, and scale cloud-native applications in their own cloud quickly and efficiently.  

Want to learn more? Reach out to schedule a demo with our DevOps experts.

Ankur Khurana
Principal Engineer @ Kapstan. Ankur brings over ten years of expertise in designing and constructing complex systems. He prefers to solve problems by applying first principles and enjoys exploring emerging technologies.

Simplify your DevEx with a single platform

Schedule a demo