π Create S3 Buckets with Terraform
Welcome to a comprehensive guide on automating the creation of AWS S3 buckets using Terraform! This project ensures robust security, logging, and efficient data management. Below is a detailed breakdown of each resource and configuration step.
π οΈ Prerequisites
π¨ Important: Never hardcode your AWS credentials in Terraform files! Use environment variables or AWS Secrets Manager to enhance security.
πΊοΈ 1. AWS Provider Configuration
provider "aws" {
region = "ap-northeast-1"
access_key = "YOUR_ACCESS_KEY" # Replace with your AWS access key
secret_key = "YOUR_SECRET_KEY" # Replace with your AWS secret key
}
π‘ What is an IAM user?
An IAM user in AWS allows you to securely access and manage your AWS resources without using the root user account. This protects your root account from potential security breaches.
π€ 2. IAM User Creation
resource "aws_iam_user" "example_user" {
name = "example-user"
}
π‘οΈ 3. IAM Policy
resource "aws_iam_user_policy" "s3_full_access" {
name = "s3-full-access-policy"
user = aws_iam_user.example_user.name
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "s3:*",
Effect = "Allow",
Resource = [
"arn:aws:s3:::my-example-bucket-1-demo",
"arn:aws:s3:::my-example-bucket-1-demo/*",
"arn:aws:s3:::my-unique-log-bucket-12345",
"arn:aws:s3:::my-unique-log-bucket-12345/*"
],
}
]
})
}
π Policy Notes: This grants the IAM user full access to the specified buckets. Adjust permissions for production environments to adhere to the principle of least privilege.
πͺ΅ 4. Logging Bucket
resource "aws_s3_bucket" "log_bucket" {
bucket = "my-unique-log-bucket-12345"
acl = "private"
tags = {
Name = "LogBucket"
Environment = "Dev"
}
}
π¦ 5. Main S3 Bucket
resource "aws_s3_bucket" "example" {
bucket = "my-example-bucket-1-demo"
acl = "private"
tags = {
Name = "MyExampleBucket"
Environment = "Dev"
}
}
π 6. Bucket Versioning
resource "aws_s3_bucket_versioning" "example" {
bucket = aws_s3_bucket.example.bucket
versioning_configuration {
status = "Enabled"
}
}
π‘ Why Enable Versioning?
Versioning helps keep track of all changes made to your objects, offering a safeguard against accidental deletions or overwrites.
π 7. Server-Side Encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "example" {
bucket = aws_s3_bucket.example.bucket
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
π 8. Bucket Logging
resource "aws_s3_bucket_logging" "example" {
bucket = aws_s3_bucket.example.bucket
target_bucket = aws_s3_bucket.log_bucket.bucket
target_prefix = "log/"
}
π Security Warning: Ensure logging permissions are correctly configured to avoid unauthorized access to logs.
π 9. Lifecycle Configuration
resource "aws_s3_bucket_lifecycle_configuration" "example" {
bucket = aws_s3_bucket.example.bucket
rule {
id = "example-rule"
status = "Enabled"
expiration {
days = 365
}
transition {
days = 30
storage_class = "STANDARD_IA"
}
}
}
π Lifecycle Tips: Use lifecycle policies to optimize storage costs by transitioning older data to cheaper storage classes.
π 10. Bucket Policy
resource "aws_s3_bucket_policy" "example" {
bucket = aws_s3_bucket.example.bucket
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Principal = {
AWS = aws_iam_user.example_user.arn
},
Action = "s3:*",
Resource = [
"${aws_s3_bucket.example.arn}",
"${aws_s3_bucket.example.arn}/*"
]
}
]
})
}
π Outputs
output "bucket_id" {
value = aws_s3_bucket.example.id
}
output "log_bucket_id" {
value = aws_s3_bucket.log_bucket.id
}
output "iam_user_access_key" {
value = aws_iam_user.example_user.name
}
π Outputs: These help you quickly reference created resources, making the setup reusable and scalable.
π― Highlights
-
IAM User & Policies: Granular access control.
-
S3 Buckets: Secure, organized, and scalable storage.
-
Versioning & Encryption: Data safety and compliance.
-
Logging: Enhanced auditing capabilities.
-
Lifecycle Rules: Cost-effective data management.
Happy Terraforming! π
For questions or feedback, reach out:
π¨ Email: projects@briankimemia.is-a.dev
π Portfolio: Brian Kimemia
GitHub: BrianKN019
Thank you for exploring this project! Letβs innovate and build secure AWS solutions together. π
Responses are generated using AI and may contain mistakes.