📦 EqualifyEverything / equalify

📄 main.tf · 120 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120/*
 * Bootstrap stack: creates the S3 bucket + DynamoDB lock table used as the
 * remote state backend for the main `infrastructure/` root module.
 *
 * This stack is applied once, on its own (local) state, before the root
 * module is initialized — it solves the chicken-and-egg problem of needing
 * a bucket to store state about a bucket.
 *
 *   cd bootstrap
 *   terraform init
 *   terraform apply
 *   terraform output
 *
 * Take the `state_bucket_name` and `lock_table_name` outputs and use them
 * to configure the root module's backend (see ../README.md).
 */

terraform {
  required_version = ">= 1.5.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

provider "aws" {
  region              = var.aws_region
  allowed_account_ids = length(var.allowed_account_ids) > 0 ? var.allowed_account_ids : null
}

variable "aws_region" {
  description = "AWS region to create the state bucket and lock table in."
  type        = string
  default     = "us-east-2"
}

variable "project_name" {
  description = "Short name used as a prefix for the bootstrap resources. Must be globally unique enough that the resulting S3 bucket name is available."
  type        = string
  default     = "equalify"
}

variable "allowed_account_ids" {
  description = "AWS account IDs this configuration may run against. Guards against provisioning into the wrong account (e.g. a prod profile left in the shell). Empty list = no restriction."
  type        = list(string)
  default     = []
}

resource "random_id" "suffix" {
  byte_length = 4
}

resource "aws_s3_bucket" "terraform_state" {
  bucket = "${var.project_name}-tfstate-${random_id.suffix.hex}"

  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_s3_bucket_versioning" "terraform_state" {
  bucket = aws_s3_bucket.terraform_state.id

  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_server_side_encryption_configuration" "terraform_state" {
  bucket = aws_s3_bucket.terraform_state.id

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "AES256"
    }
  }
}

resource "aws_s3_bucket_public_access_block" "terraform_state" {
  bucket = aws_s3_bucket.terraform_state.id

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_dynamodb_table" "terraform_lock" {
  name         = "${var.project_name}-tfstate-lock"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}

output "state_bucket_name" {
  description = "S3 bucket name to use as the root module's backend `bucket`."
  value       = aws_s3_bucket.terraform_state.id
}

output "lock_table_name" {
  description = "DynamoDB table name to use as the root module's backend `dynamodb_table`."
  value       = aws_dynamodb_table.terraform_lock.name
}

output "aws_region" {
  description = "Region to use as the root module's backend `region`."
  value       = var.aws_region
}