Skip to content

lgswin/terraform-finalproject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Overview

This project creates the following AWS resources:

  • S3 bucket (for storing Terraform state files)
  • VPC and subnets
  • Internet Gateway
  • Route tables
  • EC2 instance
  • Security groups

Project Structure

.
├── backend.tf       # Backend configuration
├── bucket.tf        # S3 bucket configuration
├── vpc.tf           # VPC and network configuration
├── ec2.tf           # EC2 instance configuration
├── variables.tf     # Variable definitions
└── terraform.tfvars # Variable values

Initial Setup

  1. Clean up existing resources (if needed)

    # Delete existing state files (if local)
    rm -f terraform.tfstate terraform.tfstate.backup
    
    # Delete existing .terraform directory
    rm -rf .terraform
  2. Backend configuration

    • Modify backend.tf file to use local backend:
    terraform {
      required_version = ">= 1.0.0"
      
      # Use local backend
      backend "local" {}
    
      # Required providers
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }
  3. Initialize Terraform

    terraform init

S3 Bucket Creation

  1. Create S3 bucket

    terraform apply -target=aws_s3_bucket.terraform_state
  2. Why create S3 bucket first?

    • I created the S3 bucket first because it will be used as the backend for storing the Terraform state file.
    • This approach solves a "chicken and egg" problem: I needed the S3 bucket to store the state file, but I can't create the bucket if we're already using it as a backend.
    • By creating the bucket first with a local backend, then migrating to the S3 backend, I ensure a smooth transition without state file conflicts.
  3. Verify S3 bucket settings

    • Server-side encryption enabled
    • Public access blocked
    • Versioning enabled

Backend Migration

  1. Change backend configuration

    • Modify backend.tf file to use S3 backend:
    terraform {
      required_version = ">= 1.0.0"
      
      # Use S3 backend
      backend "s3" {
        bucket         = "gunsu-private-bucket-8926937-state"
        key            = "terraform.tfstate"
        region         = "us-east-1"
        encrypt        = true
      }
    
      # Required providers
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }
  2. Migrate state file

    terraform init -migrate-state

Infrastructure Creation

  1. Create all resources

    terraform apply
  2. Verify created resources

    • Check in AWS Console that the following resources have been created:
      • S3 bucket (gunsu-private-bucket-8926937-state)
      • terraform.tfstate file in the S3 bucket
        Image
      • VPC (gunsu-vpc)
        Image
      • Public subnet (terraform-project-public-subnet)
        Image
      • Internet Gateway (terraform-project-igw)
        Image
      • Route table (terraform-project-public-rt)
        Image
      • EC2 instance (terraform-project-web-server)
        Image
      • Security group (terraform-project-ec2-sg)
        Image

Resource Deletion

  1. Delete all resources

    terraform destroy
  2. S3 bucket deletion issues

    • S3 bucket must be empty before deletion.
    • If versioning is enabled, all versions of objects must be deleted.

Troubleshooting

  1. IAM permission issues
    • Problem: Cannot create resources due to insufficient IAM permissions.
    • Solution: Add necessary permissions in AWS IAM console.
    • Required permissions: s3:CreateBucket, ec2:CreateVpc, etc.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages