Getting Started with Terraform for AWS

Getting Started with Terraform for AWS

Hello, I am Charu from Classmethod.

If you are new to Terraform, then you are at the right place!
Terraform is an open-source infrastructure as code (IaC) tool that allows you to define and provision infrastructure using a high-level configuration language. This hands-on guide will walk you through the basics of installing Terraform and using it to create a simple Virtual Private Cloud (VPC) on AWS.

Installation:

Use Homebrew to install Terraform:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

Verify

Verify the installation by running:

terraform -v

Configure AWS CLI

Ensure your AWS CLI is configured with appropriate credentials. Run the following command and follow the prompts:

aws configure

Create a Terraform Configuration File

Create a new directory for your Terraform project and navigate into it:

mkdir my-terraform-vpc
cd my-terraform-vpc

Create a file named main.tf and open it in your favorite text editor.

code main.tf

Code

Write the following code in the main.tf file.

You need to mention the provider first(for example AWS, GCP, Azure etc) and mention the region. Here We will be moving forward with AWS.

provider "aws" {
  region = "ap-northeast-1"
}

resource "aws_vpc" "myVPC" {
  cidr_block = "10.0.0.0/16"
}
  • This configuration file defines a VPC with a CIDR block of 10.0.0.0/16

Initialize Terraform

Initialize your Terraform project to download the necessary provider plugins:

terraform init

Plan and Apply the Configuration

Generate an execution plan to see what Terraform will do:

terraform plan

If everything looks good, apply the configuration to create the resources:

terraform apply

Verify the Resources

Once the apply command completes, you can verify the resources in the AWS Management Console. Navigate to the VPC dashboard to see your newly created VPC.

Clean Up

To avoid incurring unnecessary charges, clean up the resources by running:

terraform destroy

Again, you will be prompted to confirm the action. Type yes and press Enter.

Conclusion

Congratulations! You’ve successfully created and managed AWS infrastructure using Terraform.

Thank you for reading!

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.