Skip to content

Commit bd803f1

Browse files
authored
feat: Added modules/object (Happy Amazon S3 Pi Day!) (terraform-aws-modules#74)
1 parent 17d253d commit bd803f1

File tree

15 files changed

+464
-12
lines changed

15 files changed

+464
-12
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: git://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.47.0
3+
rev: v1.48.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Terraform module which creates S3 bucket on AWS with all (or almost all) feature
44

55
This type of resources are supported:
66

7-
* [S3 Bucket](https://www.terraform.io/docs/providers/aws/r/s3_bucket.html)
8-
* [S3 Bucket Policy](https://www.terraform.io/docs/providers/aws/r/s3_bucket_policy.html)
9-
* [S3 Bucket Notification](https://www.terraform.io/docs/providers/aws/r/s3_bucket_notification.html) - use [modules/notification](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/modules/notification) to configure notifications to Lambda functions, SQS queues, and SNS topics.
7+
* [S3 Bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket)
8+
* [S3 Bucket Policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy)
9+
* [S3 Bucket Notification](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_notification) - use [modules/notification](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/modules/notification) to configure notifications to Lambda functions, SQS queues, and SNS topics.
10+
* [S3 Bucket Object](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object) - use [modules/object](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/modules/object) to upload files to S3 bucket.
1011

1112
These features of S3 bucket configurations are supported:
1213

@@ -20,10 +21,6 @@ These features of S3 bucket configurations are supported:
2021
- Cross-Region Replication (CRR)
2122
- ELB log delivery bucket policy
2223

23-
## Terraform versions
24-
25-
Terraform 0.12 and above are supported.
26-
2724
## Usage
2825

2926
### Private bucket with versioning enabled
@@ -76,7 +73,8 @@ module "s3_bucket" {
7673

7774
* [Complete](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/complete) - Complete S3 bucket with most of supported features enabled
7875
* [Cross-Region Replication](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/s3-replication) - S3 bucket with Cross-Region Replication (CRR) enabled
79-
- [S3 bucket Notifications](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/notification) - S3 bucket notifications to Lambda functions, SQS queues, and SNS topics.
76+
- [S3 Bucket Notifications](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/notification) - S3 bucket notifications to Lambda functions, SQS queues, and SNS topics.
77+
- [S3 Bucket Object](https://github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/master/examples/object) - Manage S3 bucket objects.
8078

8179

8280
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/object/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zip

examples/object/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# S3 bucket object
2+
3+
Configuration in this directory creates S3 bucket objects with different configurations.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Requirements
19+
20+
| Name | Version |
21+
|------|---------|
22+
| terraform | >= 0.13 |
23+
| aws | >= 3.0 |
24+
| random | >= 2 |
25+
26+
## Providers
27+
28+
| Name | Version |
29+
|------|---------|
30+
| aws | >= 3.0 |
31+
| random | >= 2 |
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| object | ../../modules/object | |
38+
| object_complete | ../../modules/object | |
39+
| object_locked | ../../modules/object | |
40+
| s3_bucket | ../../ | |
41+
| s3_bucket_with_object_lock | ../../ | |
42+
43+
## Resources
44+
45+
| Name |
46+
|------|
47+
| [aws_kms_key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) |
48+
| [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
49+
50+
## Inputs
51+
52+
No input.
53+
54+
## Outputs
55+
56+
| Name | Description |
57+
|------|-------------|
58+
| this\_s3\_bucket\_arn | The ARN of the bucket. Will be of format arn:aws:s3:::bucketname. |
59+
| this\_s3\_bucket\_id | The name of the bucket. |
60+
| this\_s3\_bucket\_object\_etag | The ETag generated for the object (an MD5 sum of the object content). |
61+
| this\_s3\_bucket\_object\_id | The key of S3 object |
62+
| this\_s3\_bucket\_object\_version\_id | A unique version ID value for the object, if bucket versioning is enabled. |
63+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/object/main.tf

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
4+
# Make it faster by skipping something
5+
skip_get_ec2_platforms = true
6+
skip_metadata_api_check = true
7+
skip_region_validation = true
8+
skip_credentials_validation = true
9+
skip_requesting_account_id = true
10+
}
11+
12+
module "object" {
13+
source = "../../modules/object"
14+
15+
bucket = module.s3_bucket.this_s3_bucket_id
16+
key = "${random_pet.this.id}-local"
17+
18+
file_source = "README.md"
19+
# content = file("README.md")
20+
# content_base64 = filebase64("README.md")
21+
22+
tags = {
23+
Sensitive = "not-really"
24+
}
25+
}
26+
module "object_complete" {
27+
source = "../../modules/object"
28+
29+
bucket = module.s3_bucket.this_s3_bucket_id
30+
key = "${random_pet.this.id}-complete"
31+
32+
content = jsonencode({ data : "value" })
33+
34+
acl = "public-read"
35+
storage_class = "ONEZONE_IA"
36+
force_destroy = true
37+
38+
cache_control = "public; max-age=1200"
39+
content_disposition = "attachment; filename=\"invoice.pdf\""
40+
content_encoding = "gzip"
41+
content_language = "en-US"
42+
content_type = "application/json"
43+
44+
website_redirect = "https://www.google.com/"
45+
metadata = {
46+
key = "value1"
47+
another-key = "value2"
48+
}
49+
50+
server_side_encryption = "aws:kms"
51+
kms_key_id = aws_kms_key.this.arn
52+
}
53+
54+
module "object_locked" {
55+
source = "../../modules/object"
56+
57+
bucket = module.s3_bucket_with_object_lock.this_s3_bucket_id
58+
key = "${random_pet.this.id}-locked"
59+
60+
content = "some-content-locked-by-governance"
61+
62+
force_destroy = true
63+
64+
object_lock_legal_hold_status = true # boolean or string ("ON" or "OFF")
65+
object_lock_mode = "GOVERNANCE"
66+
object_lock_retain_until_date = formatdate("YYYY-MM-DD'T'hh:00:00Z", timeadd(timestamp(), "1h")) # some time in the future
67+
}
68+
69+
##################
70+
# Extra resources
71+
##################
72+
resource "random_pet" "this" {
73+
length = 2
74+
}
75+
76+
resource "aws_kms_key" "this" {
77+
description = "KMS key for S3 object"
78+
deletion_window_in_days = 7
79+
}
80+
81+
#############
82+
# S3 buckets
83+
#############
84+
module "s3_bucket" {
85+
source = "../../"
86+
87+
bucket = random_pet.this.id
88+
force_destroy = true
89+
}
90+
91+
module "s3_bucket_with_object_lock" {
92+
source = "../../"
93+
94+
bucket = "${random_pet.this.id}-with-object-lock"
95+
force_destroy = true
96+
97+
object_lock_configuration = {
98+
object_lock_enabled = "Enabled"
99+
}
100+
}

examples/object/outputs.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# S3 object
2+
output "this_s3_bucket_object_id" {
3+
description = "The key of S3 object"
4+
value = module.object.this_s3_bucket_object_id
5+
}
6+
7+
output "this_s3_bucket_object_etag" {
8+
description = "The ETag generated for the object (an MD5 sum of the object content)."
9+
value = module.object.this_s3_bucket_object_etag
10+
}
11+
12+
output "this_s3_bucket_object_version_id" {
13+
description = "A unique version ID value for the object, if bucket versioning is enabled."
14+
value = module.object.this_s3_bucket_object_version_id
15+
}
16+
17+
# S3 bucket
18+
output "this_s3_bucket_id" {
19+
description = "The name of the bucket."
20+
value = module.s3_bucket.this_s3_bucket_id
21+
}
22+
23+
output "this_s3_bucket_arn" {
24+
description = "The ARN of the bucket. Will be of format arn:aws:s3:::bucketname."
25+
value = module.s3_bucket.this_s3_bucket_arn
26+
}

examples/object/variables.tf

Whitespace-only changes.

examples/object/versions.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_version = ">= 0.13"
3+
4+
required_providers {
5+
aws = ">= 3.0"
6+
random = ">= 2"
7+
}
8+
}

modules/notification/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Creates S3 bucket notification resource with all supported types of deliveries:
99
|------|---------|
1010
| terraform | >= 0.12.6 |
1111
| aws | >= 3.0 |
12-
| random | >= 2.0 |
1312

1413
## Providers
1514

modules/notification/versions.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ terraform {
22
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 3.0"
6-
random = ">= 2.0"
5+
aws = ">= 3.0"
76
}
87
}

modules/object/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# S3 bucket object
2+
3+
Creates S3 bucket objects with different configurations.
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Requirements
7+
8+
| Name | Version |
9+
|------|---------|
10+
| terraform | >= 0.13 |
11+
| aws | >= 3.0 |
12+
13+
## Providers
14+
15+
| Name | Version |
16+
|------|---------|
17+
| aws | >= 3.0 |
18+
19+
## Modules
20+
21+
No Modules.
22+
23+
## Resources
24+
25+
| Name |
26+
|------|
27+
| [aws_s3_bucket_object](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object) |
28+
29+
## Inputs
30+
31+
| Name | Description | Type | Default | Required |
32+
|------|-------------|------|---------|:--------:|
33+
| acl | The canned ACL to apply. Valid values are private, public-read, public-read-write, aws-exec-read, authenticated-read, bucket-owner-read, and bucket-owner-full-control. Defaults to private. | `string` | `null` | no |
34+
| bucket | The name of the bucket to put the file in. Alternatively, an S3 access point ARN can be specified. | `string` | `""` | no |
35+
| cache\_control | Specifies caching behavior along the request/reply chain. | `string` | `null` | no |
36+
| content | Literal string value to use as the object content, which will be uploaded as UTF-8-encoded text. | `string` | `null` | no |
37+
| content\_base64 | Base64-encoded data that will be decoded and uploaded as raw bytes for the object content. This allows safely uploading non-UTF8 binary data, but is recommended only for small content such as the result of the gzipbase64 function with small text strings. For larger objects, use source to stream the content from a disk file. | `string` | `null` | no |
38+
| content\_disposition | Specifies presentational information for the object. | `string` | `null` | no |
39+
| content\_encoding | Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field. | `string` | `null` | no |
40+
| content\_language | The language the content is in e.g. en-US or en-GB. | `string` | `null` | no |
41+
| content\_type | A standard MIME type describing the format of the object data, e.g. application/octet-stream. All Valid MIME Types are valid for this input. | `string` | `null` | no |
42+
| create | Whether to create this resource or not? | `bool` | `true` | no |
43+
| etag | Used to trigger updates. This attribute is not compatible with KMS encryption, kms\_key\_id or server\_side\_encryption = "aws:kms". | `string` | `null` | no |
44+
| file\_source | The path to a file that will be read and uploaded as raw bytes for the object content. | `string` | `null` | no |
45+
| force\_destroy | Allow the object to be deleted by removing any legal hold on any object version. Default is false. This value should be set to true only if the bucket has S3 object lock enabled. | `bool` | `false` | no |
46+
| key | The name of the object once it is in the bucket. | `string` | `""` | no |
47+
| kms\_key\_id | Amazon Resource Name (ARN) of the KMS Key to use for object encryption. If the S3 Bucket has server-side encryption enabled, that value will automatically be used. If referencing the aws\_kms\_key resource, use the arn attribute. If referencing the aws\_kms\_alias data source or resource, use the target\_key\_arn attribute. Terraform will only perform drift detection if a configuration value is provided. | `string` | `null` | no |
48+
| metadata | A map of keys/values to provision metadata (will be automatically prefixed by x-amz-meta-, note that only lowercase label are currently supported by the AWS Go API). | `map(string)` | `{}` | no |
49+
| object\_lock\_legal\_hold\_status | The legal hold status that you want to apply to the specified object. Valid values are ON and OFF. | `string` | `null` | no |
50+
| object\_lock\_mode | The object lock retention mode that you want to apply to this object. Valid values are GOVERNANCE and COMPLIANCE. | `string` | `null` | no |
51+
| object\_lock\_retain\_until\_date | The date and time, in RFC3339 format, when this object's object lock will expire. | `string` | `null` | no |
52+
| server\_side\_encryption | Specifies server-side encryption of the object in S3. Valid values are "AES256" and "aws:kms". | `string` | `null` | no |
53+
| storage\_class | Specifies the desired Storage Class for the object. Can be either STANDARD, REDUCED\_REDUNDANCY, ONEZONE\_IA, INTELLIGENT\_TIERING, GLACIER, DEEP\_ARCHIVE, or STANDARD\_IA. Defaults to STANDARD. | `string` | `null` | no |
54+
| tags | A map of tags to assign to the object. | `map(string)` | `{}` | no |
55+
| website\_redirect | Specifies a target URL for website redirect. | `string` | `null` | no |
56+
57+
## Outputs
58+
59+
| Name | Description |
60+
|------|-------------|
61+
| this\_s3\_bucket\_object\_etag | The ETag generated for the object (an MD5 sum of the object content). |
62+
| this\_s3\_bucket\_object\_id | The key of S3 object |
63+
| this\_s3\_bucket\_object\_version\_id | A unique version ID value for the object, if bucket versioning is enabled. |
64+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/object/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
resource "aws_s3_bucket_object" "this" {
2+
count = var.create ? 1 : 0
3+
4+
bucket = var.bucket
5+
key = var.key
6+
force_destroy = var.force_destroy
7+
8+
acl = var.acl
9+
storage_class = try(upper(var.storage_class), var.storage_class)
10+
11+
source = var.file_source
12+
content = var.content
13+
content_base64 = var.content_base64
14+
etag = var.etag
15+
16+
cache_control = var.cache_control
17+
content_disposition = var.content_disposition
18+
content_encoding = var.content_encoding
19+
content_language = var.content_language
20+
content_type = var.content_type
21+
website_redirect = var.website_redirect
22+
metadata = var.metadata
23+
24+
server_side_encryption = var.server_side_encryption
25+
kms_key_id = var.kms_key_id
26+
27+
object_lock_legal_hold_status = try(tobool(var.object_lock_legal_hold_status) ? "ON" : upper(var.object_lock_legal_hold_status), var.object_lock_legal_hold_status)
28+
object_lock_mode = try(upper(var.object_lock_mode), var.object_lock_mode)
29+
object_lock_retain_until_date = var.object_lock_retain_until_date
30+
31+
tags = var.tags
32+
33+
lifecycle {
34+
ignore_changes = [object_lock_retain_until_date]
35+
}
36+
}

modules/object/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "this_s3_bucket_object_id" {
2+
description = "The key of S3 object"
3+
value = element(concat(aws_s3_bucket_object.this.*.id, [""]), 0)
4+
}
5+
6+
output "this_s3_bucket_object_etag" {
7+
description = "The ETag generated for the object (an MD5 sum of the object content)."
8+
value = element(concat(aws_s3_bucket_object.this.*.etag, [""]), 0)
9+
}
10+
11+
output "this_s3_bucket_object_version_id" {
12+
description = "A unique version ID value for the object, if bucket versioning is enabled."
13+
value = element(concat(aws_s3_bucket_object.this.*.version_id, [""]), 0)
14+
}

0 commit comments

Comments
 (0)