Lädt...

🔧 Deploying a Static Website on AWS S3 & EC2 Using Terraform" published: true


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Deploying a Static Website on AWS S3 & EC2 Using Terraform

Infrastructure as Code (IaC) simplifies cloud management. In this guide, we'll automate the deployment of a static website on AWS S3 and create an EC2 instance using Terraform.

What We'll Cover

  • S3 Bucket Creation & Hosting – Automating website deployment.
  • Bucket Policy for Public Access – Ensuring public accessibility.
  • EC2 Instance Creation – Managing compute resources.
  • Using Variables & Outputs – Making the configuration reusable.

Prerequisites

  • AWS account with CLI configured (aws configure)
  • Terraform installed (terraform -v)

Step 1: Configure Terraform & AWS Provider

Create a main.tf file and define the Terraform and AWS provider configuration:

terraform {
  required_version = ">= 1.7.4"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.40.0"
    }
  }
}

provider "aws" {
  profile = "default"
  region  = "us-east-1"
}

Step 2: Create an S3 Bucket for Static Website Hosting

resource "aws_s3_bucket" "website_bucket" {
  bucket = "my-static-website-43234"
}

resource "aws_s3_bucket_website_configuration" "website_config" {
  bucket = aws_s3_bucket.website_bucket.id
  index_document {
    suffix = "index.html"
  }
}

resource "aws_s3_object" "index_file" {
  bucket        = aws_s3_bucket.website_bucket.id
  key           = "index.html"
  source        = "index.html"
  content_type  = "text/html"
  etag          = filemd5("index.html")
}

Step 3: Configure Public Access Policy

resource "aws_s3_bucket_public_access_block" "public_access" {
  bucket                  = aws_s3_bucket.website_bucket.id
  block_public_acls       = false
  block_public_policy     = false
}

resource "aws_s3_bucket_policy" "allow_public_access" {
  bucket = aws_s3_bucket.website_bucket.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid       = "PublicReadGetObject"
        Effect    = "Allow"
        Principal = "*"
        Action    = "s3:GetObject"
        Resource  = "${aws_s3_bucket.website_bucket.arn}/*"
      }
    ]
  })
  depends_on = [aws_s3_bucket_public_access_block.public_access]
}

Step 4: Create an EC2 Instance

resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "WebServerInstance"
  }
}

Step 5: Use Variables & Outputs

Define variables in variables.tf:

variable "instance_type" {
  description = "Type of EC2 instance"
  type        = string
  default     = "t2.micro"
}

variable "website_bucket_name" {
  description = "S3 bucket name for website hosting"
  type        = string
  default     = "my-static-website-43234"
}

Define outputs in outputs.tf:

output "website_url" {
  value = "http://${aws_s3_bucket.website_bucket.bucket}.s3-website.${provider.aws.region}.amazonaws.com"
}

output "ec2_instance_id" {
  value = aws_instance.web_server.id
}

output "ec2_public_ip" {
  value = aws_instance.web_server.public_ip
}

Step 6: Deploy with Terraform

terraform init
terraform plan
terraform apply -auto-approve

To destroy the infrastructure:

terraform destroy -auto-approve

Conclusion

By following these steps, you automated the deployment of an S3-hosted static website and an EC2 instance using Terraform. This approach ensures reproducibility, scalability, and efficient cloud management. 🚀

...

🔧 Deploying a Static Website on AWS S3 & EC2 Using Terraform" published: true


📈 80.59 Punkte
🔧 Programmierung

🔧 Deploying a Static HTTPS Website to AWS S3 Using Terraform & GitHub Actions — Part 1


📈 47.13 Punkte
🔧 Programmierung

🔧 Deploying a Static HTTPS Website to AWS S3 Using Terraform & GitHub Actions — Part 1


📈 47.13 Punkte
🔧 Programmierung

🔧 Deploying a static website with AWS EC2 using Nginx


📈 46.55 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS S3 Using Terraform


📈 45.24 Punkte
🔧 Programmierung

🔧 Customizing and Deploying a Static Website with NGINX on AWS EC2


📈 42.83 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS EC2 with NGINX


📈 42.83 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS EC2 with Nginx: A Comprehensive Guide


📈 42.83 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS S3 with Terraform: A Beginner's Guide


📈 41.52 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on AWS S3 with Terraform: A Beginner's Guide


📈 41.52 Punkte
🔧 Programmierung

🔧 🚀 Deploying Sonatype Nexus on AWS EC2 using Terraform


📈 40.13 Punkte
🔧 Programmierung

🔧 Deploying a Flask Web App on an AWS EC2 Instance using Docker and Terraform


📈 40.13 Punkte
🔧 Programmierung

🔧 Deploying a Discord Bot to AWS EC2 Using Terraform


📈 40.13 Punkte
🔧 Programmierung

🔧 Deploy a static (Next.js) website to AWS using AWS CDK & AWS console


📈 37.5 Punkte
🔧 Programmierung

🔧 Getting Started With Terraform! Deploying an AWS EC2 Instance


📈 36.41 Punkte
🔧 Programmierung

🔧 Terraform in Action: Deploying an Ubuntu EC2 Instance with Nginx on AWS


📈 36.41 Punkte
🔧 Programmierung

🔧 Deploying an AWS EC2 Instance with Terraform and SSH Access


📈 36.41 Punkte
🔧 Programmierung

🔧 Using Pulumi IaC to deploy NextJs static website with AWS S3 and EC2


📈 36.37 Punkte
🔧 Programmierung

🔧 How to Deploy a Static Website on AWS EC2 Using Apache


📈 36.37 Punkte
🔧 Programmierung

🔧 Deploying a Static Website on Amazon S3 with Terraform!


📈 36.31 Punkte
🔧 Programmierung

🔧 Deploying a Static website using AWS (S3+cloudfront)-React App


📈 35.38 Punkte
🔧 Programmierung

🔧 Build a Secure Static Website on AWS S3 Using Terraform


📈 35.06 Punkte
🔧 Programmierung

🔧 Deploy a Static Website with Route53, CloudFront and AWS Certificate using a Terraform Script


📈 35.06 Punkte
🔧 Programmierung

🔧 Hosting a Static Website Using S3 in AWS with Terraform


📈 35.06 Punkte
🔧 Programmierung

🔧 Hosting a Static Website Using S3 in AWS with Terraform


📈 35.06 Punkte
🔧 Programmierung

🔧 AWS Networking with Terraform: Hosting a static website using S3


📈 35.06 Punkte
🔧 Programmierung

🔧 SSH to your AWS EC2 instances using EC2-connect eice using this alias


📈 34.98 Punkte
🔧 Programmierung

🔧 Deploying an EC2 Instance with a Dockerized App using Terraform


📈 34.92 Punkte
🔧 Programmierung

🔧 Deploying an EC2 Instance Using Terraform


📈 34.92 Punkte
🔧 Programmierung

🔧 Deploying a Full Stack AWS Architecture Using Terraform: Ensuring High Availability in AWS


📈 34.16 Punkte
🔧 Programmierung

🔧 What is EC2 in AWS? Everything about EC2 Fundamentals in AWS


📈 32.75 Punkte
🔧 Programmierung

matomo