-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
66 lines (58 loc) · 2.36 KB
/
main.tf
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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create resources for state stored in S3 backend
#
# TF config created based on:
# - https://blog.gruntwork.io/how-to-manage-terraform-state-28f5697e68fa
# - https://github.com/gruntwork-io/intro-to-terraform/blob/master/s3-backend/main.tf
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ------------------------------------------------------------------------------
# CONFIGURE OUR AWS CONNECTION
# ------------------------------------------------------------------------------
provider "aws" {
region = var.region
}
# ------------------------------------------------------------------------------
# CREATE THE S3 BUCKET
# ------------------------------------------------------------------------------
resource "aws_s3_bucket" "terraform_state" {
# checkov:skip=CKV_AWS_18 tfsec:ignore:AWS002 - Access logging is not needed for this bucket.
# checkov:skip=CKV_AWS_52 - The bucket stores generated files. MFA delete protection is not needed.
bucket = "${var.namespace}-${var.env}-terraform-state"
# state files
force_destroy = true # TODO: Make this optional
tags = var.tags
}
# Enable versioning by default
resource "aws_s3_bucket_versioning" "terraform_state" {
bucket = aws_s3_bucket.terraform_state.id
versioning_configuration {
status = "Enabled"
# TODO: Consider adding optional variable to enable MFA delete protection.
}
}
# Enable server-side encryption by default
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"
}
}
}
# ------------------------------------------------------------------------------
# CREATE THE DYNAMODB TABLE
#
# Warning
# The key here has to be "LockID".
# ------------------------------------------------------------------------------
resource "aws_dynamodb_table" "terraform_locks" {
name = "${var.namespace}-${var.env}-terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags = var.tags
# checkov:skip=CKV_AWS_28:"Point in time recovery" is not necessary for terraform locks table.
}