Connecting AWS with Terraform: A Short Guide
Tags: aws, terraform, devops, iac
Published: 2024-01-27 06:01:32

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:

- [AWS Account](https://aws.amazon.com/)

- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed

- [IAM user with an access key ID and secret key](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_root-user_manage_add-key.html) set up

- [Terraform](https://www.terraform.io/) installed

> If you are a Windows user, you can checkout my [Step-by-step Guide to Installing Terraform on Windows](https://dev.to/annysah/step-by-step-guide-to-installing-terraform-on-windows-m2e)

## 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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c22t4cqmifduomw50d0y.png)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j79601n1fjea3q9z5gu8.png)

## 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.