Skip to content

Commit dbf10cf

Browse files
Scott WinklerScott Winkler
authored andcommitted
initial commit
0 parents  commit dbf10cf

File tree

10 files changed

+195
-0
lines changed

10 files changed

+195
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# S3 Backend Module
2+
This is a description for a module

examples/default/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Default example
2+
3+
The example shows the default usages of the module.

examples/default/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
module "s3backend" {
6+
source = "../../"
7+
namespace = "default"
8+
}

examples/default/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "config" {
2+
value = module.s3backend
3+
}

iam.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
locals {
4+
principal = var.principal != null ? var.principal : data.aws_caller_identity.current.account_id
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": "2012-10-17",
13+
"Statement": [
14+
{
15+
"Action": "sts:AssumeRole",
16+
"Principal": {
17+
"AWS": "${local.principal}"
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+
36+
resources = [
37+
aws_s3_bucket.s3_bucket.arn
38+
]
39+
}
40+
41+
statement {
42+
actions = ["s3:GetObject", "s3:PutObject"]
43+
44+
resources = [
45+
"${aws_s3_bucket.s3_bucket.arn}/*",
46+
]
47+
}
48+
49+
statement {
50+
actions = [
51+
"dynamodb:GetItem",
52+
"dynamodb:PutItem",
53+
"dynamodb:DeleteItem"
54+
]
55+
resources = [aws_dynamodb_table.dynamodb_table.arn]
56+
}
57+
}
58+
59+
resource "aws_iam_policy" "iam_policy" {
60+
name = "${local.namespace}-tf-policy"
61+
path = "/"
62+
policy = data.aws_iam_policy_document.policy_doc.json
63+
}
64+
65+
resource "aws_iam_role_policy_attachment" "policy_attach" {
66+
role = aws_iam_role.iam_role.name
67+
policy_arn = aws_iam_policy.iam_policy.arn
68+
}

main.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
data "aws_region" "current" {}
2+
3+
resource "random_string" "rand" {
4+
length = 24
5+
special = false
6+
upper = 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+
resource "aws_kms_key" "kms_key" {
34+
tags = {
35+
ResourceGroup = local.namespace
36+
}
37+
}
38+
39+
resource "aws_s3_bucket" "s3_bucket" {
40+
bucket = "${local.namespace}-state-bucket"
41+
force_destroy = var.force_destroy_state
42+
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_kms_key.kms_key.arn
52+
}
53+
}
54+
}
55+
56+
tags = {
57+
ResourceGroup = local.namespace
58+
}
59+
}
60+
61+
resource "aws_dynamodb_table" "dynamodb_table" {
62+
name = "${local.namespace}-state-lock"
63+
hash_key = "LockID"
64+
billing_mode = "PAY_PER_REQUEST"
65+
attribute {
66+
name = "LockID"
67+
type = "S"
68+
}
69+
tags = {
70+
ResourceGroup = local.namespace
71+
}
72+
}

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "namespace" {
2+
description = "The project namespace to use for unique resource naming"
3+
default = "s3backend"
4+
type = string
5+
}
6+
7+
variable "principal" {
8+
description = "AWS principal identifier allowed to assume IAM role"
9+
default = null
10+
type = string
11+
}
12+
13+
variable "force_destroy_state" {
14+
description = "Force destroy the s3 bucket containing state files?"
15+
default = true
16+
type = bool
17+
}

versions.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*terraform {
2+
required_version = "~> 0.12"
3+
required_providers {
4+
aws = "~> 2.19"
5+
random = "~> 2.1"
6+
}
7+
}*/

0 commit comments

Comments
 (0)