Skip to main content

 Terraform

In this guide, we will help you understand how to set Terraform to create an EC2 instance on AWS and run this instance using Runme

Before we proceed, there are a few requirements needed to get started

Prerequisites

  1. Cloud Provider

    For this guide, we will be using Amazon Web Services (AWS) as our cloud provider. Ensure you have an AWS account, with an account you will be able to access the AWS key credentials which will be used subsequently.

  2. Terraform Installation

    Install Terraform on your local machine. You can download the latest version from the official Terraform website.

  3. Runme Installation

    To get Runme available on your local machine or server, you are required to install the Runme. Runme has various client interfaces with which you can use Runme, this includes; VS Code editor, CLI, and web app. In this guide, we will use Runme for VS Code editor using the Runme Notebook extension.

Setting Up Terraform for EC2 Instance

Create a Project Directory:

Begin by creating a new directory for your Terraform project and navigate to the directory in your terminal.

mkdir terraform-ec2-instance
cd terraform-ec2-instance

Next, create a new file named main.tf in your project directory. This file will contain the Terraform configuration for your EC2 instance.

provider "aws" {
region = "us-east-1" # Replace with your desired AWS region
}

resource "aws_instance" "example" {
ami = "ami-0e731c8a588258d0d" # Amazon Linux 2 AMI ID
instance_type = "t2.micro"
}

In the code snippet above, we set the AWS region,

  • We set the AWS region (you can replace the region with your desired region).
  • Next, define the the Aws EC2 instance, example using the ”aws_instance”.
  • Specify the ami (Amazone Machine Image) ID for the instance.
  • Lastly, the instance_type to t2.micro” to indicate the instance a micro-sized instance.

Run your file with Runme

In this section of the guide, we will creating a runbook with Runme which will be used to run the project.

Runme has amazing functionalities that not only allows you run your commands within your Markdown file but also saves the output in a separate file through a feature knwn as session output. Through this you are not only running your command but your output is saved for future reference.

Initialize Terraform

To begin, first create a .md file within your project directory. With Runme installed, every Markdown (.md) file created in your code editor will ipen with Markdown. Alternatively you can set Runme as your editor Markdown viewer.

In your Markdown file, creating a code block to execute your command.

start terraform code

Add your code to initialize Terraform

terraform init

Run the above command to initialize your Terraform configuration

terraform init

This command initializes your working directory and downloads the necessary providers.

Review and Apply

Before applying the changes, you can review the execution plan to ensure everything looks correct:

terraform plan

You should get a similar output:

terraform plan

Once this is thoroughly checked for errors or misconfiguration, you can apply the changes to create the EC2 instance.

terraform apply

Output:

terraform

Terraform will prompt you to confirm the changes. Type yes and press Enter. Once the apply process is complete, you can go to the AWS Management Console to verify that your EC2 instance has been created.

Save Output

Runme allows you to save your output throughout your deployment process. Whether it's running code or Markdown, you can keep track of every activity that has taken place throughout your deployment process by saving the output This saved output is automatically stored in a new file which is created by default, so you can easily reference it at any time. To use this feature, just toggle the "Autosave" option at the top of your code editor in your Markdown file to turn “on” or “off”. It's that simple!

auto save

Alternatively you set the Auto-save as default. This will ensure that autosave is always on and your outputs are automatically saved. See the Auto-save feature for more information.

Cleaning Up

If you want to destroy the resources created by Terraform, you can run:

terraform destroy

This command will prompt you to confirm the destruction of resources. Type yes to proceed.

Terraform Workspaces

Within your code cell you can set the directory structure of where you want your executable command to be initiated from

terraform structure

  • Create workspace for production and staging:
#production
terraform workspace new production

Set your block to run from your production directory

#staging
terraform workspace new staging

Set your block to run from your staging directory

  • Select between workspaces:
#production
terraform workspace select production

or

#Staging

terraform workspace select staging

Lastly based on your preferred workspace, execute the command by applying the terraform configuration t0 modify the infrastructure in the environment of your choice.

For example, in production, run the command below:

# For production
terraform workspace select production
terraform apply -var-file=terraform.tfvars
# For staging
terraform workspace select production
terraform apply -var-file=terraform.tfvars

This will allow for the separation of configurations for different environments, making it easier to manage and apply changes to infrastructure in distinct contexts, such as development, testing, and production.

Conclusion

Using Runme with Terraform helps you to create an operational workflow that integrates Terraform commands within Runme’s automated runbook. In this guide, we walked through running a Terraform within a Runme Notebook in our VS Code editor. We covered how you can run your EC2 instance with Runme, review and apply your changes, set directory commands for each workspace, and save your output for future reference. We hope this tutorial was of help to you. Wish you a smooth operation!