Infrastructure as Code (IaC) has become an essential component of modern software development, allowing teams to manage and provision infrastructure in a consistent and automated manner using tools like Terraform. In this guide, you'll learn how to connect AWS with Terraform, enabling users to seamlessly provision and manage resources on the AWS cloud.
Prerequisites
Before diving into the article, ensure that you have the following prerequisites in place:
If you are a Windows user, you can checkout my Step-by-step Guide to Installing Terraform on Windows
Step 1: Configure AWS CLI
Run the following command to configure the AWS CLI with your AWS credentials:
aws configure
You will be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and output format. Make sure to provide the necessary information.
Step 2: Set Up AWS Provider in Terraform
Create a new Terraform file, for example, main.tf
, and specify the AWS provider, and configure the region as shown below:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-east-1"
}
You can replace the region with the appropriate AWS region code where you want to deploy your resources.
Step 3: Initialize Terraform Configuration
Next initialize your Terraform project by running the following command:
terraform init
This command downloads the necessary provider plugins.
Step 4: Write Terraform Configuration
Now, you can start writing your Terraform configuration to define the AWS resources you want to create. For example, to create an S3 bucket, you can add the following code to your main.tf file:
resource "aws_s3_bucket" "annysahbucket" {
bucket = "annysah-terraform-practice"
}
Step 5: Apply Terraform Configuration
Apply the Terraform configuration to create the specified AWS resources:
terraform apply
Upon running the command above, enter 'yes' for each prompt displayed.
Step 6: Verify Resources in AWS Console
Check the AWS Management Console to ensure that the resources provided in your Terraform configuration file have been successfully created.
If you want to destroy the created resources, you can run
terraform destroy
from your terminal.
Conclusion
In this guide, you've gained an overview of how to connect AWS with Terraform, allowing you to automate and scale your infrastructure deployments with ease.
Top comments (0)