Skip to content

Commit 855c618

Browse files
authored
Make enable_server_side_encryption and force_destroy variables (#4)
1 parent 6395872 commit 855c618

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ terraform {
2626
module "terraform_state_backend" {
2727
source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=master"
2828
namespace = "cp"
29-
stage = "dev"
30-
name = "app"
29+
stage = "prod"
30+
name = "terraform"
31+
attributes = ["state"]
3132
region = "us-east-1"
3233
}
3334
```
@@ -67,18 +68,20 @@ and the DynamoDB table will be used to lock the state to prevent concurrent modi
6768

6869
## Variables
6970

70-
| Name | Default | Description | Required |
71-
|:-------------------------|:-------------|:----------------------------------------------------------------------------------|:--------:|
72-
| `namespace` | `` | Namespace (_e.g._ `cp` or `cloudposse`) | Yes |
73-
| `stage` | `` | Stage (_e.g._ `prod`, `dev`, `staging`) | Yes |
74-
| `region` | `us-east-1` | AWS Region the S3 bucket should reside in | Yes |
75-
| `name` | `terraform` | Name (_e.g._ `app`, `cluster`, or `terraform`) | No |
76-
| `attributes` | `["state"]` | Additional attributes (_e.g._ `policy` or `role`) | No |
77-
| `tags` | `{}` | Additional tags (_e.g._ `map("BusinessUnit","XYZ")` | No |
78-
| `delimiter` | `-` | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | No |
79-
| `acl` | `private` | The canned ACL to apply to the S3 bucket | No |
80-
| `read_capacity` | `5` | DynamoDB read capacity units | No |
81-
| `write_capacity` | `5` | DynamoDB write capacity units | No |
71+
| Name | Default | Description | Required |
72+
|:---------------------------------|:-------------|:----------------------------------------------------------------------------------|:--------:|
73+
| `namespace` | `` | Namespace (_e.g._ `cp` or `cloudposse`) | Yes |
74+
| `stage` | `` | Stage (_e.g._ `prod`, `dev`, `staging`) | Yes |
75+
| `region` | `` | AWS Region the S3 bucket should reside in | Yes |
76+
| `name` | `terraform` | Name (_e.g._ `app`, `cluster`, or `terraform`) | No |
77+
| `attributes` | `["state"]` | Additional attributes (_e.g._ `state`) | No |
78+
| `tags` | `{}` | Additional tags (_e.g._ `map("BusinessUnit","XYZ")` | No |
79+
| `delimiter` | `-` | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | No |
80+
| `acl` | `private` | The canned ACL to apply to the S3 bucket | No |
81+
| `read_capacity` | `5` | DynamoDB read capacity units | No |
82+
| `write_capacity` | `5` | DynamoDB write capacity units | No |
83+
| `force_destroy` | `false` | A boolean that indicates the S3 bucket can be destroyed even if it contains objects. These objects are not recoverable | No |
84+
| `enable_server_side_encryption` | `true` | Enable DynamoDB server-side encryption | No |
8285

8386

8487
## Outputs

main.tf

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resource "aws_s3_bucket" "default" {
1212
bucket = "${module.s3_bucket_label.id}"
1313
acl = "${var.acl}"
1414
region = "${var.region}"
15-
force_destroy = false
15+
force_destroy = "${var.force_destroy}"
1616

1717
versioning {
1818
enabled = true
@@ -39,11 +39,12 @@ module "dynamodb_table_label" {
3939
tags = "${var.tags}"
4040
}
4141

42-
resource "aws_dynamodb_table" "default" {
42+
resource "aws_dynamodb_table" "with_server_side_encryption" {
43+
count = "${var.enable_server_side_encryption == "true" ? 1 : 0}"
4344
name = "${module.dynamodb_table_label.id}"
4445
read_capacity = "${var.read_capacity}"
4546
write_capacity = "${var.write_capacity}"
46-
hash_key = "LockID" # https://www.terraform.io/docs/backends/types/s3.html#dynamodb_table
47+
hash_key = "LockID" # https://www.terraform.io/docs/backends/types/s3.html#dynamodb_table
4748

4849
server_side_encryption {
4950
enabled = true
@@ -60,3 +61,22 @@ resource "aws_dynamodb_table" "default" {
6061

6162
tags = "${module.dynamodb_table_label.tags}"
6263
}
64+
65+
resource "aws_dynamodb_table" "without_server_side_encryption" {
66+
count = "${var.enable_server_side_encryption == "true" ? 0 : 1}"
67+
name = "${module.dynamodb_table_label.id}"
68+
read_capacity = "${var.read_capacity}"
69+
write_capacity = "${var.write_capacity}"
70+
hash_key = "LockID"
71+
72+
lifecycle {
73+
ignore_changes = ["read_capacity", "write_capacity"]
74+
}
75+
76+
attribute {
77+
name = "LockID"
78+
type = "S"
79+
}
80+
81+
tags = "${module.dynamodb_table_label.tags}"
82+
}

output.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ output "s3_bucket_arn" {
1111
}
1212

1313
output "dynamodb_table_name" {
14-
value = "${aws_dynamodb_table.default.name}"
14+
value = "${element(coalescelist(aws_dynamodb_table.with_server_side_encryption.*.name, aws_dynamodb_table.without_server_side_encryption.*.name), 0)}"
1515
}
1616

1717
output "dynamodb_table_id" {
18-
value = "${aws_dynamodb_table.default.id}"
18+
value = "${element(coalescelist(aws_dynamodb_table.with_server_side_encryption.*.id, aws_dynamodb_table.without_server_side_encryption.*.id), 0)}"
1919
}
2020

2121
output "dynamodb_table_arn" {
22-
value = "${aws_dynamodb_table.default.arn}"
22+
value = "${element(coalescelist(aws_dynamodb_table.with_server_side_encryption.*.arn, aws_dynamodb_table.without_server_side_encryption.*.arn), 0)}"
2323
}

variables.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ variable "namespace" {
55

66
variable "stage" {
77
type = "string"
8-
description = "Stage (e.g. `prod`, `dev`, `staging`, `infra`)"
8+
description = "Stage (e.g. `prod`, `dev`, `staging`)"
99
}
1010

1111
variable "name" {
@@ -23,19 +23,18 @@ variable "delimiter" {
2323
variable "attributes" {
2424
type = "list"
2525
default = ["state"]
26-
description = "Additional attributes (e.g. `policy` or `role`)"
26+
description = "Additional attributes (e.g. `state`)"
2727
}
2828

2929
variable "tags" {
3030
type = "map"
3131
default = {}
32-
description = "Additional tags (e.g. map('BusinessUnit`,`XYZ`)"
32+
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)"
3333
}
3434

3535
variable "region" {
3636
type = "string"
3737
description = "AWS Region the S3 bucket should reside in"
38-
default = "us-east-1"
3938
}
4039

4140
variable "acl" {
@@ -53,3 +52,13 @@ variable "write_capacity" {
5352
default = 5
5453
description = "DynamoDB write capacity units"
5554
}
55+
56+
variable "force_destroy" {
57+
description = "A boolean that indicates the S3 bucket can be destroyed even if it contains objects. These objects are not recoverable"
58+
default = "false"
59+
}
60+
61+
variable "enable_server_side_encryption" {
62+
description = "Enable DynamoDB server-side encryption"
63+
default = "true"
64+
}

0 commit comments

Comments
 (0)