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.
Before you begin, ensure you have the following installed:
- Terraform: Follow the official installation guide to install Terraform.
- AWS CLI: Install the AWS CLI by following the installation instructions.
- AWS Account: You need an AWS account to create resources.
- AWS Credentials: Configure your AWS credentials using
aws configure
command.
-
Clone the Repository: Clone the repository containing this Terraform configuration.
git clone <repository-url> cd <repository-directory>
-
Set Up Environment Variables: Create a
terraform.tfvars
file to define the required variables.environment = "dev" s3_bucket_name = "my-terraform-state-bucket"
-
Initialize Terraform: Initialize the Terraform working directory.
terraform init
-
Plan the Infrastructure: Generate and review the execution plan.
terraform plan
-
Apply the Configuration: Apply the configuration to create the resources.
terraform apply
resource "random_id" "bucket_id" {
byte_length = 6
}
- random_id: Generates a random ID with a byte length of 6.
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 tofalse
, allowing the bucket to be destroyed. - tags: Tags the bucket with a name and environment.
To destroy the created resources, run:
terraform destroy
To refer to a GitHub module to create an S3 bucket in your Terraform configuration, follow these steps:
-
Identify the GitHub Repository: Ensure you have the URL of the GitHub repository containing the Terraform module.
-
Define the Module in Your Terraform Configuration: Use the
module
block to refer to the GitHub repository.
.create_s3
├── main.tf
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"
}
-
Initialize Terraform: Initialize the Terraform working directory.
terraform init
-
Plan the Infrastructure: Generate and review the execution plan.
terraform plan
-
Apply the Configuration: Apply the configuration to create the resources.
terraform apply
- 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.
You can configure AWS credentials using the AWS CLI in several ways:
-
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.
-
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
-
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
-
Config File: Add your configuration to the
~/.aws/config
file:[default] region = your_region output = json
-
IAM Roles for EC2: If running on an EC2 instance, you can assign an IAM role to the instance with the necessary permissions.
-
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
-
AWS SSO (Single Sign-On): Configure AWS SSO with:
aws configure sso