Follow the instructions in the AWS CLI installation guide to install AWS CLI on your system.
Open your terminal or command prompt and run the following command:
which aws
If installed correctly, this should return the path to the AWS CLI executable.
Follow the instructions in the Terraform installation guide to install Terraform on your system.
Run the following command to ensure Terraform is installed properly:
terraform -v
This should display the installed Terraform version.
Use curl
or wget
to download a sample project template:
curl -O https://www.free-css.com/assets/files/free-css-templates/download/page296/oxer.zip
or
wget https://www.free-css.com/assets/files/free-css-templates/download/page296/oxer.zip
Unzip the downloaded template:
unzip oxer.zip
Refer to the official AWS Provider documentation to understand how to configure AWS in Terraform.
- Log in to the AWS Management Console.
- Navigate to the IAM service.
- Create a new IAM user with programmatic access.
- Attach the necessary policies (e.g.,
AmazonS3FullAccess
). - Generate and download the access key and secret key.
Use the access key and secret key to configure the AWS CLI:
aws configure
You will be prompted to enter the following information:
- AWS Access Key ID
- AWS Secret Access Key
- Default region name (e.g., us-east-1)
- Default output format (e.g., json)
Navigate to your Terraform project directory and initialize the Terraform working directory:
terraform init
Create a Terraform configuration file (main.tf
) with the following content to set up an S3 bucket:
provider "aws" {
region = "us-east-1" # Change to your preferred region
}
resource "aws_s3_bucket" "mybucket" {
bucket = "my-unique-bucket-name" # Change to a unique bucket name
acl = "public-read"
website {
index_document = "index.html"
error_document = "error.html"
}
}
Build and apply the Terraform configuration:
terraform apply -auto-approve
Add the following resources to your main.tf
to configure S3 bucket ownership and public access:
resource "aws_s3_bucket_ownership_controls" "example" {
bucket = aws_s3_bucket.mybucket.bucket
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_public_access_block" "example" {
bucket = aws_s3_bucket.mybucket.bucket
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
If you have more files in the unzipped template, add them to your Terraform configuration:
resource "aws_s3_object" "index" {
bucket = aws_s3_bucket.mybucket.bucket
key = "index.html"
source = "path/to/index.html"
acl = "public-read"
}
resource "aws_s3_object" "error" {
bucket = aws_s3_bucket.mybucket.bucket
key = "error.html"
source = "path/to/error.html"
acl = "public-read"
}
Configure the S3 bucket for website hosting:
resource "aws_s3_bucket_website_configuration" "website" {
bucket = aws_s3_bucket.mybucket.bucket
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
}
Apply the changes to set up the S3 bucket and upload files:
terraform apply -auto-approve
Add the following output to your main.tf
to display the website endpoint:
output "website_endpoint" {
value = aws_s3_bucket.mybucket.website_endpoint
}
Log in to your AWS account and verify the changes made to the S3 bucket. You should see the objects and the configured website.
Go to your S3 bucket, click on "Properties," and scroll down to the "Static website hosting" section. Click on the provided link to access your static website.