Skip to content

Commit

Permalink
initial module implementation (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun authored Nov 18, 2020
1 parent 7ee889e commit 54406f6
Show file tree
Hide file tree
Showing 18 changed files with 423 additions and 160 deletions.
117 changes: 72 additions & 45 deletions README.md

Large diffs are not rendered by default.

32 changes: 22 additions & 10 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

# Name of this project
name: terraform-aws-s3-config
name: terraform-aws-config-storage

# Logo for this project
#logo: docs/logo.png
Expand All @@ -20,13 +20,13 @@ copyrights:
year: "2020"

# Canonical GitHub repo
github_repo: cloudposse/terraform-aws-s3-config
github_repo: cloudposse/terraform-aws-config-storage

# Badges to display
badges:
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-aws-s3-config.svg"
url: "https://github.com/cloudposse/terraform-aws-s3-config/releases/latest"
image: "https://img.shields.io/github/release/cloudposse/terraform-aws-config-storage.svg"
url: "https://github.com/cloudposse/terraform-aws-config-storage/releases/latest"
- name: "Slack Community"
image: "https://slack.cloudposse.com/badge.svg"
url: "https://slack.cloudposse.com"
Expand Down Expand Up @@ -57,8 +57,16 @@ references:

# Short description of this project
description: |-
This is `terraform-aws-s3-config` project provides all the scaffolding for a typical well-built Cloud Posse module. It's a template repository you can
use when creating new repositories.
This module creates an S3 bucket suitable for storing `AWS Config` data.
It implements a configurable log retention policy, which allows you to efficiently manage logs across different
storage classes (_e.g._ `Glacier`) and ultimately expire the data altogether.
It enables server-side default encryption.
https://docs.aws.amazon.com/AmazonS3/latest/dev/bucket-encryption.html
It blocks public access to the bucket by default.
https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html
# Introduction to the project
#introduction: |-
Expand All @@ -69,16 +77,20 @@ usage: |-
Here's how to invoke this example module in your projects
```hcl
module "example" {
source = "https://github.com/cloudposse/terraform-aws-s3-config.git?ref=master"
example = "Hello world!"
module "aws_config_storage" {
source = "git::https://github.com/cloudposse/terraform-aws-config-storage.git?ref=master"
name = "aws-config"
stage = "test"
namespace = "eg"
standard_transition_days = 30
glacier_transition_days = 60
}
```
# Example usage
examples: |-
Here is an example of using this module:
- [`examples/complete`](https://github.com/cloudposse/terraform-aws-s3-config/) - complete example of using this module
- [`examples/complete`](https://github.com/cloudposse/terraform-aws-config-storage/) - complete example of using this module
# How to get started quickly
#quickstart: |-
Expand Down
3 changes: 2 additions & 1 deletion context.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#

module "this" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
source = "cloudposse/label/null"
version = "0.21.0"

enabled = var.enabled
namespace = var.namespace
Expand Down
31 changes: 23 additions & 8 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,56 @@

| Name | Version |
|------|---------|
| terraform | >= 0.12.0, < 0.14.0 |
| local | ~> 1.2 |
| random | ~> 2.2 |
| terraform | >= 0.13.0 |
| aws | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| random | ~> 2.2 |
| aws | >= 2.0 |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| abort\_incomplete\_multipart\_upload\_days | Maximum time (in days) that you want to allow multipart uploads to remain in progress | `number` | `5` | no |
| access\_log\_bucket\_name | Name of the S3 bucket where s3 access log will be sent to | `string` | `""` | no |
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
| enable\_glacier\_transition | Enables the transition to AWS Glacier which can cause unnecessary costs for huge amount of small files | `bool` | `true` | no |
| enabled | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
| environment | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
| example | Example variable | `string` | `"hello world"` | no |
| expiration\_days | Number of days after which to expunge the objects | `number` | `90` | no |
| force\_destroy | (Optional, Default:false ) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable | `bool` | `false` | no |
| glacier\_transition\_days | Number of days after which to move the data to the glacier storage tier | `number` | `60` | no |
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
| kms\_master\_key\_arn | The AWS KMS master key ARN used for the SSE-KMS encryption. This can only be used when you set the value of sse\_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse\_algorithm is aws:kms | `string` | `""` | no |
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
| lifecycle\_prefix | Prefix filter. Used to manage object lifecycle events | `string` | `""` | no |
| lifecycle\_rule\_enabled | Enable lifecycle events on this bucket | `bool` | `true` | no |
| lifecycle\_tags | Tags filter. Used to manage object lifecycle events | `map(string)` | `{}` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
| noncurrent\_version\_expiration\_days | Specifies when noncurrent object versions expire | `number` | `90` | no |
| noncurrent\_version\_transition\_days | Specifies when noncurrent object versions transitions | `number` | `30` | no |
| policy | A valid bucket policy JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a terraform plan. In this case, please make sure you use the verbose/specific version of the policy | `string` | `""` | no |
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
| sse\_algorithm | The server-side encryption algorithm to use. Valid values are AES256 and aws:kms | `string` | `"AES256"` | no |
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| standard\_transition\_days | Number of days to persist in the standard storage tier before moving to the infrequent access tier | `number` | `30` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| example | Example output |
| id | ID of the created example |
| random | Stable random number for this example |
| bucket\_arn | Bucket ARN |
| bucket\_domain\_name | FQDN of bucket |
| bucket\_id | Bucket Name (aka ID) |
| enabled | Is module enabled |
| prefix | Prefix configured for lifecycle rules |

<!-- markdownlint-restore -->
3 changes: 2 additions & 1 deletion examples/complete/context.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#

module "this" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
source = "cloudposse/label/null"
version = "0.21.0"

enabled = var.enabled
namespace = var.namespace
Expand Down
12 changes: 3 additions & 9 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
region = "us-east-2"

namespace = "eg"

region = "us-east-2"
namespace = "eg"
environment = "ue2"

stage = "test"

name = "example"

stage = "test"
10 changes: 7 additions & 3 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module "example" {
source = "../.."
provider "aws" {
region = var.region
}

module "s3_aws_config" {
source = "../../"

example = var.example
force_destroy = true

context = module.this.context
}
18 changes: 9 additions & 9 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
output "id" {
description = "ID of the created example"
value = module.example.id
output "bucket_domain_name" {
value = module.s3_aws_config.bucket_domain_name
description = "FQDN of bucket"
}

output "example" {
description = "Output \"example\" from example module"
value = module.example.example
output "bucket_id" {
value = module.s3_aws_config.bucket_id
description = "Bucket Name (aka ID)"
}

output "random" {
description = "Output \"random\" from example module"
value = module.example.random
output "bucket_arn" {
value = module.s3_aws_config.bucket_arn
description = "Bucket ARN"
}
5 changes: 2 additions & 3 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
variable "example" {
type = string
description = "The value which will be passed to the example module"
variable "region" {
type = string
}
4 changes: 2 additions & 2 deletions examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_version = ">= 0.12.0, < 0.14"
required_version = ">= 0.13.0"

required_providers {
local = "~> 1.2"
aws = ">=2"
}
}
88 changes: 82 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
resource "random_integer" "example" {
module "aws_config_label" {
source = "cloudposse/label/null"
version = "0.21.0"

attributes = ["aws-config"]
context = module.this.context
}

module "storage" {
source = "cloudposse/s3-log-storage/aws"
version = "0.14.0"
count = module.this.enabled ? 1 : 0

lifecycle_prefix = var.lifecycle_prefix
lifecycle_tags = var.lifecycle_tags
force_destroy = var.force_destroy
lifecycle_rule_enabled = var.lifecycle_rule_enabled
versioning_enabled = true
noncurrent_version_expiration_days = var.noncurrent_version_expiration_days
noncurrent_version_transition_days = var.noncurrent_version_transition_days
standard_transition_days = var.standard_transition_days
glacier_transition_days = var.glacier_transition_days
enable_glacier_transition = var.enable_glacier_transition
expiration_days = var.expiration_days
abort_incomplete_multipart_upload_days = var.abort_incomplete_multipart_upload_days
sse_algorithm = var.sse_algorithm
kms_master_key_arn = var.kms_master_key_arn
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
access_log_bucket_name = var.access_log_bucket_name

tags = module.this.tags
attributes = ["aws-config"]
context = module.this.context
}

data "aws_iam_policy_document" "aws_config_bucket_policy" {
count = module.this.enabled ? 1 : 0

min = 1
max = 50000
keepers = {
example = var.example
statement {
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}

effect = "Allow"

actions = [
"s3:GetBucketAcl",
"s3:ListBucket",
]

resources = [
local.s3_bucket_arn
]
}

statement {
actions = ["s3:PutObject"]

effect = "Allow"

principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}

condition {
test = "StringLike"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}

resources = [local.s3_object_prefix]
}
}

#-----------------------------------------------------------------------------------------------------------------------
# Locals and Data Sources
#-----------------------------------------------------------------------------------------------------------------------
data "aws_caller_identity" "current" {}

locals {
example = format("%v %v", var.example, join("", random_integer.example[*].result))
current_account_id = data.aws_caller_identity.current.account_id
s3_bucket_arn = module.this.enabled ? module.storage[0].bucket_arn : ""
s3_object_prefix = format("%s/AWSLogs/%s/Config/*", local.s3_bucket_arn, local.current_account_id)
}
28 changes: 19 additions & 9 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
output "id" {
description = "ID of the created example"
value = module.this.enabled ? module.this.id : null
output "bucket_domain_name" {
value = join("", module.storage.*.bucket_domain_name)
description = "FQDN of bucket"
}

output "example" {
description = "Example output"
value = module.this.enabled ? local.example : null
output "bucket_id" {
value = join("", module.storage.*.bucket_id)
description = "Bucket Name (aka ID)"
}

output "random" {
description = "Stable random number for this example"
value = module.this.enabled ? join("", random_integer.example[*].result) : null
output "bucket_arn" {
value = join("", module.storage.*.bucket_arn)
description = "Bucket ARN"
}

output "prefix" {
value = var.lifecycle_prefix
description = "Prefix configured for lifecycle rules"
}

output "enabled" {
value = module.this.enabled
description = "Is module enabled"
}
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ clean:
all: module examples/complete

## Run basic sanity checks against the module itself
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
## Diable module-pinning BATS test until updated to support terraform registry sytnax
module: export TESTS ?= installed lint get-modules get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
module: deps
$(call RUN_TESTS, ../)

Expand Down
Loading

0 comments on commit 54406f6

Please sign in to comment.