Creating AWS S3 Bucket
AWS Authentication
Section titled “AWS Authentication”Before creating resources, you need to configure AWS credentials for Terraform to authenticate with AWS APIs.
Authentication Methods
Section titled “Authentication Methods”- AWS CLI Configuration:
aws configure - Environment Variables:
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY - IAM Roles: For EC2 instances or AWS services
- AWS Profiles: Named credential profiles
S3 (Simple Storage Service)
Section titled “S3 (Simple Storage Service)”Object storage service that offers scalability, data availability, security, and performance.
Tasks for Practice
Section titled “Tasks for Practice”Prerequisites
Section titled “Prerequisites”- Create AWS Account: Sign up for AWS free tier if you don’t have an account
- Install AWS CLI: Download and install from AWS official website
- Configure Credentials: Set up your AWS access keys
AWS CLI Installation
Section titled “AWS CLI Installation”Check your system architecture first:
# Linux/macOSuname -m
# Windows PowerShell$env:PROCESSOR_ARCHITECTUREOfficial Website: https://aws.amazon.com/cli/
Windows:
# Using MSI installer (recommended)# Download from: <https://awscli.amazonaws.com/AWSCLIV2.msi>
# Using wingetwinget install Amazon.AWSCLI
# Using chocolateychoco install awsclimacOS:
# Using official installercurl "<https://awscli.amazonaws.com/AWSCLIV2.pkg>" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /
# Using Homebrewbrew install awscliUbuntu/Debian:
# Update package indexsudo apt update
# Install AWS CLI v2 (choose based on your architecture)# For x86_64curl "<https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip>" -o "awscliv2.zip"
# For ARM64curl "<https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip>" -o "awscliv2.zip"
unzip awscliv2.zipsudo ./aws/install
# Verify installationaws --versionAuthentication Setup
Section titled “Authentication Setup”Method 1: AWS CLI Configuration
Section titled “Method 1: AWS CLI Configuration”aws configureEnter your:
- AWS Access Key ID
- AWS Secret Access Key
- Default region (e.g., us-east-1)
- Default output format (json)
Method 2: Environment Variables
Section titled “Method 2: Environment Variables”export AWS_ACCESS_KEY_ID="your-access-key"export AWS_SECRET_ACCESS_KEY="your-secret-key"export AWS_DEFAULT_REGION="us-east-1"Tasks to Complete
Section titled “Tasks to Complete”- Get familiar with Terraform AWS documentation
- Visit: https://registry.terraform.io/providers/hashicorp/aws/latest
- Explore S3 resource documentation
- Create AWS resources using terraform
- S3 bucket with unique name
- Practice Terraform commands
- Initialize the working directory
- Plan the infrastructure changes
- Apply the configuration
- Verify resources in AWS Console
Important Notes
Section titled “Important Notes”- Resource Names: S3 bucket names must be globally unique
- Regions: Ensure you’re working in your intended AWS region
- Costs: Monitor AWS costs, even in free tier
- Cleanup: Always destroy resources when done practicing
Common Commands
Section titled “Common Commands”# Initialize Terraformterraform init
# Validate configurationterraform validate
# Plan changesterraform plan
# Apply changesterraform apply
# Show current stateterraform show
# Destroy resourcesterraform destroyTroubleshooting Tips
Section titled “Troubleshooting Tips”- Check AWS credentials are properly configured
- Verify region settings match your intended deployment location
- Ensure S3 bucket names are unique and follow naming conventions
- Review AWS CloudTrail for API call logs if needed
Solution…
Section titled “Solution…”terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 6.0" } }}
provider "aws" { # Configuration options region = "us-east-1"}
# Create a S3 bucketresource "aws_s3_bucket" "tf_test_baivab_bucket" { bucket = "my-tf-test-baiv-bucket-101"
tags = { Name = "My bucket" Environment = "Dev" }}