DEV Community

Cover image for ->> Day-26 Provisioning an AWS S3 Bucket using HCP Terraform
Amit Kushwaha
Amit Kushwaha

Posted on

->> Day-26 Provisioning an AWS S3 Bucket using HCP Terraform

In this blog, I implemented a cloud-based Terraform workflow using HCP Terraform integrated with Github to provision an AWS S3 in a prodcution style setup.

>> Project Objective:

The goal was to:

  • Define AWS infrastructure using Terraform
  • Store and version control the code in Github
  • Execute Terraform runs a remotely using HCP Terraform
  • Implement a VCS- driven automated workflow
  • Manage state securely in the cloud
  • Isolate environments using Projects and Workspaces

>> Architecture Overview:

The deployment workflow follows this structure:


Developer -> Github -> HCP -> Terraform -> AWS -> S3 Bucket
Enter fullscreen mode Exit fullscreen mode

Execution Flow

  1. Write Terraform configuration for S3 bucket.
  2. Push the code to GitHub.
  3. HCP Terraform detects the change.
  4. Automatically runs terraform init and terraform plan.
  5. Review the plan in the UI.
  6. Confirm and apply the changes.
  7. AWS provisions the S3 bucket.

Step-by-Step Guide to deploy an aws s3 bucket using HCP Terraform

Prerequisites

Before starting, make sure you have:

  • AWS Account
  • GitHub Account
  • HCP Terraform Account
  • Basic knowledge of Terraform syntax

Step 1: Create a GitHub Repository

  1. Log in to GitHub.
  2. Create a new repository (e.g., terraform-s3-demo).
  3. Clone it locally:
git clone https://github.com/your-username/terraform-s3-demo.git
cd terraform-s3-demo
Enter fullscreen mode Exit fullscreen mode

Step:2 Write Terraform Configuration

Create the following files:

main.tf

provider "aws" {
  region = var.region
}

resource "aws_s3_bucket" "mybucket" {
  bucket = var.bucket_name

  tags = {
    Name        = var.bucket_name
    Environment = var.environment
  }
}
Enter fullscreen mode Exit fullscreen mode

variables.tf

variable "region" {}
variable "bucket_name" {}
variable "environment" {}
Enter fullscreen mode Exit fullscreen mode

Step3: Push Code to Github

git add .
git commit -m "Initial S3 bucket Terraform configuration"
git push origin main
Enter fullscreen mode Exit fullscreen mode

Your Terraform code is now version-controlled.


Step 4: Set Up HCP Terraform

  1. Log in to HCP Terraform
  2. Create a new Organization
  3. Inside the organization, create a Project

Projects help logically group infrastructure.


Step 5: Create a VCS-Driven Workspace

  1. Click Create Workspace
  2. Select Version Control Workflow
  3. Connect your GitHub account
  4. Choose the repository (terraform-s3-demo)
  5. Set working directory (if needed)
  6. Create workspace

Now your repo is linked to HCP Terraform.


Step:6 Configure Varibales in Workspace

Inside the Workspace -> varibales section:

Add Environment Variables

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

mark them as sensitive.

Add Terraform variables
Example:


region = ap-south-1
bucket_name = amit-terraform-demo-bucket
environment = dev
Enter fullscreen mode Exit fullscreen mode

Do NOT hardcode credentials in code.

Step 7: Trigger the First Run

Now go back to GitHub and make a small change (or re-push code).

HCP Terraform will automatically:

  • Clone the repository
  • Run terraform init
  • Run terraform plan
  • Show execution plan in UI

Step 8: Review and Apply

  1. Review the plan output.
  2. Click Confirm & Apply.
  3. Wait for execution to complete.

If successful, your S3 bucket will be created in AWS.


Step 9: Verify in AWS Console

  1. Log in to AWS.
  2. Navigate to S3.
  3. Confirm the bucket is created.

Congratulations - infrastructure deployed using cloud-based Terraform workflow.

>> Secure Credential Management:

AWS credentials were added as sensitive environment variables inside the HCP Terraform workspace.

This ensures:

  • No secrets in source code
  • Secure execution
  • Production-aligned security practice

Resource:
Github Repo: Github Repo
Hashicorp: Hashicorp

Conslusion

This project showcases how to provision AWS infrastructure using a cloud-native Terraform workflow powered by HCP Terraform and GitHub.

By combining Infrastructure as Code with automated VCS-driven execution, the deployment process becomes:

  • Repeatable
  • Secure
  • Collaborative

- Production-ready

>> Connect With Me

If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:

💼 LinkedIn: Amit Kushwaha

🐙 GitHub: Amit Kushwaha

📝 Hashnode / Amit Kushwaha

🐦 Twitter/X: Amit Kushwaha

Found this helpful? Drop a ❤️ and follow for more AWS and Terraform tutorials!

Questions? Drop them in the comments below! 👇

Top comments (0)