# Terraform Day 2: Mastering HCL Syntax and AWS S3 Bucket Setup

As part of **TerraWeek Day 2**, I dove deep into the basics of Terraform and HashiCorp Configuration Language (HCL). In this post, I’ll share my journey through Terraform's HCL syntax, the creation of AWS resources, and how to configure AWS S3 buckets using variables and providers. Let's break down each step I followed.

### Task 1: Understanding HCL Syntax

Terraform uses **HashiCorp Configuration Language (HCL)** to define infrastructure configurations. The syntax is designed to be human-readable and concise. In my exploration, I focused on three primary elements of HCL:

* **Blocks**: These define different parts of a configuration (e.g., `resource`, `provider`, `output`). Here's an example of creating an AWS S3 bucket:
    

```yaml
resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-name"
}
```

* **Parameters**: These define values inside a block (e.g., `bucket`).
    
* **Arguments**: The specific values passed to the parameters. For example, `"my-unique-bucket-name"` is the argument for the `bucket` parameter.
    

Through this exercise, I also explored **resources** (AWS instances, S3 buckets) and **data sources** (information about existing infrastructure).

### Task 2: Understanding Variables and Expressions in HCL

Variables are crucial for reusability in Terraform. I created a [`variables.tf`](http://variables.tf) file to define the `bucket_name` variable, which will dynamically assign the name of the S3 bucket.

#### Defining Variables

```yaml
variable "bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
  default     = "my-default-bucket"
}
```

Then, I used this variable inside the [`main.tf`](http://main.tf) file to configure the `aws_s3_bucket` resource:

```yaml
resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
}
```

This allows flexibility in using the same Terraform configuration for different environments by changing only the variable values.

### Task 3: Practicing with Providers and Testing the Configuration

Next, I configured the **AWS provider** in [`main.tf`](http://main.tf), which is necessary for Terraform to interact with AWS resources.

#### Required Providers Configuration

```yaml
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}
```

#### Testing the Configuration

To test the configuration, I followed these steps:

1. **Initialize the Terraform directory**:
    
    ```bash
    terraform init
    ```
    
    This command downloads the required provider plugins and prepares Terraform for use.
    
2. **Preview the changes**:
    
    ```bash
    terraform plan
    ```
    
    This command shows what Terraform plans to do (e.g., create the S3 bucket).
    
3. **Apply the configuration**:
    
    ```bash
    terraform apply
    ```
    
    This command applies the configuration, creating the S3 bucket as defined.
    

#### Screenshots of the execution:

* **terraform init**:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733326349349/665a85f4-f2cf-4d08-a9e8-2db6644cbdc3.png align="center")
    
* **terraform plan**:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733326356349/4e2fa0c0-0175-4a50-ad4c-800359b4a0f0.png align="center")
    
* **terraform apply**:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733326364554/9acf0bd6-96a3-4101-96d1-8acb514bbd73.png align="center")
    

### Conclusion

Day 2 of **TerraWeek** was all about mastering the basics of HCL syntax and gaining hands-on experience with Terraform. By completing these tasks, I’ve laid the foundation for creating and managing infrastructure with Terraform, especially when working with cloud providers like AWS.

In the coming days, I'll continue to deepen my Terraform knowledge and start working on more complex configurations. Stay tuned for future posts where I'll share more advanced use cases, including multi-cloud deployments and infrastructure automation!
