Skip to content

Commit 2cae927

Browse files
author
nex9
committed
initial commit
0 parents  commit 2cae927

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.vscode
3+
*.tfstate
4+
*.tfstate.*
5+
terraform
6+
**/.terraform/*
7+
crash.log

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# S3 Backend Module
2+
3+
This module will deploy and S3 remote backend for Terraform

iam.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
locals {
4+
principal_arns = var.principal_arns != null ? var.principal_arns : [data.aws_caller_identity.current.arn]
5+
}
6+
7+
resource "aws_iam_role" "iam_role" {
8+
name = "${local.namespace}-tf-assume-role"
9+
10+
assume_role_policy = <<-EOF
11+
{
12+
"Version": "2021-10-17",
13+
"Statement": [
14+
{
15+
"Action": "sts:AssumeRole",
16+
"Principal": {
17+
"AWS": ${jsonencode(local.principal_arns)}
18+
},
19+
"Effect": "Allow"
20+
}
21+
]
22+
}
23+
EOF
24+
25+
tags = {
26+
ResourceGroup = local.namespace
27+
}
28+
}
29+
30+
data "aws_iam_policy_document" "policy_doc" {
31+
statement {
32+
actions = [
33+
"s3:ListBucket",
34+
]
35+
resources = [
36+
aws_s3_bucket.s3_bucket.arn
37+
]
38+
}
39+
40+
statement {
41+
actions = ["s3.GetObject", "s3:PutObject", "s3:DeleteObject"]
42+
resources = [
43+
"${aws_s3_bucket.s3_bucket.arn}/*",
44+
]
45+
}
46+
47+
statement {
48+
actions = [
49+
"dynamodb:GetItem",
50+
"dynamodb:PutItem",
51+
"dynamodb:DeleteItem"
52+
]
53+
resources = [aws_dynamodb_table.dynamodb_table.arn]
54+
}
55+
}
56+
57+
resource "aws_iam_policy" "iam_policy" {
58+
name = "${local.namespace}-tf-policy"
59+
path = "/"
60+
policy = data.aws_iam_policy_document.policy_doc.json
61+
}
62+
63+
resource "aws_iam_role_policy_attachment" "policy_attach" {
64+
role = aws_iam_role.iam_role.name
65+
policy_arn = aws_iam_policy.iam_policy.arn
66+
}

main.tf

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
data "aws_region" "current" {}
2+
3+
resource random_string "rand" {
4+
length = 24
5+
upper = false
6+
special = false
7+
}
8+
9+
locals {
10+
namespace = substr(join("-", [var.namespace, random_string.rand.result]), 0, 24)
11+
}
12+
13+
resource "aws_resourcegroups_group" "resourcegroups_group" {
14+
name = "${local.namespace}-group"
15+
16+
resource_query {
17+
query = <<-JSON
18+
{
19+
"ResourceTypeFilters": [
20+
"AWS::AllSupported"
21+
],
22+
"TagFilters": [
23+
{
24+
"Key": "ResourceGroup",
25+
"Values": ["${local.namespace}"]
26+
}
27+
]
28+
}
29+
JSON
30+
31+
}
32+
}
33+
34+
resource "aws_kem_key" "kms_key" {
35+
tags = {
36+
ResourceGroup = local.namespace
37+
}
38+
}
39+
40+
resource "aws_s3_bucket" "s3_bucket" {
41+
bucket = "${local.namespace}-state-bucket"
42+
force_destroy = var.force_destroy_state
43+
versioning {
44+
enabled = true
45+
}
46+
47+
server_side_encryption_configuration {
48+
rule {
49+
apply_server_side_encryption_by_default {
50+
sse_algorithm = "aws:kms"
51+
kms_master_key_id = aws_kem_key.kms_key.arn
52+
}
53+
}
54+
}
55+
tags = {
56+
ResourceGroup = local.namespace
57+
}
58+
}
59+
60+
resource "aws_s3_bucket_public_access_block" "s3_bucket" {
61+
bucket = aws_s3_bucket.s3_bucket.id
62+
63+
block_public_acls = true
64+
block_public_policy = true
65+
ignore_public_acls = true
66+
restrict_public_buckets = true
67+
}
68+
69+
resource "aws_dynamodb_table" "dynamodb_table" {
70+
name = "${local.namespace}-state-lock"
71+
hash_key = "LockID"
72+
billing_mode = "PAY_PER_REQUEST"
73+
attribute {
74+
name = "LockID"
75+
type = "S"
76+
}
77+
tags = {
78+
ResourceGroup = local.namespace
79+
}
80+
}

outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "config" {
2+
value = {
3+
bucket = aws_s3_bucket.s3_bucket.bucket
4+
region = data.aws_region.current.name
5+
role_arn = aws_iam_role.iam_role.arn
6+
dynamodb_table = aws_dynamodb_table.dynamodb_table.name
7+
}
8+
}

variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "namespace" {
2+
type = string
3+
default = "s3backend"
4+
description = "The project napespace to user for unique resource naming"
5+
}
6+
7+
variable "principal_arns" {
8+
type = list(string)
9+
default = null
10+
description = "A list of pricipal arns allowed to assume the IAM role"
11+
}
12+
13+
variable "force_destroy_state" {
14+
type = bool
15+
default = true
16+
description = "Force destroy the s3 bucket containing state files"
17+
}
18+
19+

versions.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_version = ">= 0.15"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 3.28"
7+
}
8+
random = {
9+
source = "hashicorp/random"
10+
version = "~> 3.0"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)