Skip to content

cloudengine-labs/terraform-aws-s3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Module for AWS S3 resource management

Terraform AWS S3 Bucket Setup

This Terraform configuration creates an AWS S3 bucket with a unique name using a random ID generator. The bucket is tagged with a name and environment variables.

Prerequisites

Before you begin, ensure you have the following installed:

  1. Terraform: Follow the official installation guide to install Terraform.
  2. AWS CLI: Install the AWS CLI by following the installation instructions.
  3. AWS Account: You need an AWS account to create resources.
  4. AWS Credentials: Configure your AWS credentials using aws configure command.

Configuration

  1. Clone the Repository: Clone the repository containing this Terraform configuration.

    git clone <repository-url>
    cd <repository-directory>
  2. Set Up Environment Variables: Create a terraform.tfvars file to define the required variables.

    environment     = "dev"
    s3_bucket_name  = "my-terraform-state-bucket"
    
  3. Initialize Terraform: Initialize the Terraform working directory.

    terraform init
  4. Plan the Infrastructure: Generate and review the execution plan.

    terraform plan
  5. Apply the Configuration: Apply the configuration to create the resources.

    terraform apply

Code Explanation

random_id Resource

resource "random_id" "bucket_id" {
  byte_length = 6
}
  • random_id: Generates a random ID with a byte length of 6.

aws_s3_bucket Resource

resource "aws_s3_bucket" "terraform_state_bucket" {
  bucket = "tf-backend-${var.environment}-${random_id.bucket_id.hex}"

  lifecycle {
    prevent_destroy = false
  }

  tags = {
    Name        = var.s3_bucket_name
    Environment = var.environment
  }
}
  • aws_s3_bucket: Creates an S3 bucket with a unique name using the random ID and environment variable.
  • lifecycle: The prevent_destroy attribute is set to false, allowing the bucket to be destroyed.
  • tags: Tags the bucket with a name and environment.

Clean Up

To destroy the created resources, run:

terraform destroy

Additional Resources


How to refer the GitHub based module to create resources

To refer to a GitHub module to create an S3 bucket in your Terraform configuration, follow these steps:

  1. Identify the GitHub Repository: Ensure you have the URL of the GitHub repository containing the Terraform module.

  2. Define the Module in Your Terraform Configuration: Use the module block to refer to the GitHub repository.

Example Directory Structure

.create_s3
├── main.tf

Example

main.tf

in Root Directory

provider "aws" {
  region = "us-west-2"
}

module "s3_bucket" {
  source          = "git::https://github.com/username/repo.git//path/to/module"
  environment     = "dev"
  s3_bucket_name  = "my-terraform-state-bucket"
}

Steps to Use the Module

  1. Initialize Terraform: Initialize the Terraform working directory.

    terraform init
  2. Plan the Infrastructure: Generate and review the execution plan.

    terraform plan
  3. Apply the Configuration: Apply the configuration to create the resources.

    terraform apply

Explanation

  • source: Specifies the GitHub repository URL. The //path/to/module part is optional and used if the module is in a subdirectory.
  • environment and s3_bucket_name: These are variables defined in the module that you need to pass values for.

Methods to configure AWS CLI

You can configure AWS credentials using the AWS CLI in several ways:

  1. Using aws configure Command:

    aws configure

    This command will prompt you to enter your AWS Access Key ID, Secret Access Key, region, and output format.

  2. Environment Variables: Set the following environment variables:

    export AWS_ACCESS_KEY_ID=your_access_key_id
    export AWS_SECRET_ACCESS_KEY=your_secret_access_key
    export AWS_DEFAULT_REGION=your_region
  3. Shared Credentials File: Add your credentials to the ~/.aws/credentials file:

    [default]
    aws_access_key_id = your_access_key_id
    aws_secret_access_key = your_secret_access_key
    
  4. Config File: Add your configuration to the ~/.aws/config file:

    [default]
    region = your_region
    output = json
    
  5. IAM Roles for EC2: If running on an EC2 instance, you can assign an IAM role to the instance with the necessary permissions.

  6. AWS CLI Named Profiles: You can create multiple profiles in the ~/.aws/credentials and ~/.aws/config files:

    [profile_name]
    aws_access_key_id = your_access_key_id
    aws_secret_access_key = your_secret_access_key
    

    Use the profile with:

    aws configure --profile profile_name
  7. AWS SSO (Single Sign-On): Configure AWS SSO with:

    aws configure sso

Packages

No packages published

Languages