Creating and Managing an S3 Bucket with Terraform

ยท

3 min read

Terraform, an open-source infrastructure as code (IaC) tool, allows you to define and manage resources on AWS efficiently. In this blog, we'll walk through the process of creating an S3 bucket and uploading files to it using Terraform.

Prerequisites

Before you start, ensure the following:

  1. Terraform Installed: Download and install Terraform from here.

  2. AWS CLI Installed: Configure AWS CLI with appropriate credentials using aws configure

  3. IAM User Permissions: Ensure the IAM user has permissions to manage S3 resources (e.g., AmazonS3FullAccess).

Step 1: Setting Up Your Terraform Configuration

Create a working directory and a file named main.tf to define your Terraform configuration.

Provider Configuration

The provider configuration specifies the AWS region where your resources will be created:

provider "aws" {
  region = "us-east-1"
}

Step 2: Defining the S3 Bucket

Define the S3 bucket resource in your main.tf file:

resource "aws_s3_bucket" "hari" {
  bucket = "harilachannagari123"
  tags = {
    Name = "My-s3-bucket"
    Env  = "Dev"
  }
}
  1. bucket: Specifies the unique name of the bucket.

  2. tags: Adds metadata to help identify the bucket.

Step 3: Uploading Files to the S3 Bucket

Use the aws_s3_object resource to upload files to the bucket:

resource "aws_s3_object" "put_object" {
  bucket = aws_s3_bucket.hari.bucket
  key    = "my-file"
  source = "C:/Users/hari/Downloads/myfile.pdf"
  acl    = "private"

  tags = {
    Name = "Mydocument"
    Env  = "Dev"
  }
}
  1. bucket: Links to the S3 bucket.

  2. key: Specifies the object's key (name) in the bucket.

  3. source: Path to the local file to upload.

  4. acl: Sets the file's access permissions.

Step 4: Outputs

Define outputs to retrieve the bucket name and object details:

output "bucket_name" {
  value = aws_s3_bucket.my_bucket.bucket
}

output "object_url" {
  value = aws_s3_object.my_object.etag
}

Step 5: Initializing and Applying Terraform

Follow these steps to deploy the configuration:

  1. Initialize Terraform:

     terraform init
    
  2. Preview Changes:

     terraform plan
    

Apply Changes:

terraform apply --auto-approve

Step 6: Verify the Resources

After applying the configuration, verify that the bucket and object have been created:

  1. AWS Console: Check the S3 service for the bucket and file.

  2. AWS CLI: List buckets and objects:

     aws s3 ls
     aws s3 ls s3://my-unique-s3-bucket-name
    

Common Issues and Troubleshooting

  1. Bucket Name Already Exists:

    • S3 bucket names must be globally unique. Update the bucket name in your configuration.
  2. Permission Denied:

    • Ensure the IAM user has the necessary permissions (e.g., s3:CreateBucket, s3:PutObject).
  3. Invalid File Path:

    • Use forward slashes (e.g., ./example-file.txt) in file paths.

Conclusion

Using Terraform to create and manage S3 resources simplifies infrastructure management, making your workflows more efficient. This blog covered the basics of creating an S3 bucket and uploading files to it. You can extend this setup to include advanced configurations such as versioning, lifecycle rules, or server-side encryption.

Happy Terraforming! ๐Ÿš€

ย