Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial module setup #1

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/commit-to-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
validation:
strategy:
matrix:
folder: ["add", "folders", "here"]
folder: ["./", "./examples/s3_files"]
name: Terraform validate for ${{ matrix.folder }}
runs-on: ubuntu-20.04
steps:
Expand All @@ -41,7 +41,7 @@ jobs:
linting:
strategy:
matrix:
folder: ["add", "folders", "here"]
folder: ["./", "./examples/s3_files"]
name: Terraform lint for ${{ matrix.folder }}
runs-on: ubuntu-20.04
steps:
Expand All @@ -59,7 +59,7 @@ jobs:
plan:
strategy:
matrix:
folder: ["add", "folders", "here"]
folder: ["./examples/s3_files"]
name: Terraform plan for ${{ matrix.folder }}
runs-on: ubuntu-20.04
needs: [validation, linting]
Expand Down
1 change: 1 addition & 0 deletions .terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.5.1
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,58 @@ The below documentation is intended to assist users in utilising the module, the
the module itself, and the [examples](#examples) section which has examples of how to utilise the module.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.5.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.61.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.67.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_s3_object.uploaded_files](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_object) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_raw_s3_files"></a> [raw\_s3\_files](#input\_raw\_s3\_files) | Data structure<br>---------------<br>A list of dictionaries, where each dictionary has the following attributes:<br><br>REQUIRED<br>---------<br>- name: : Friendly name used through Terraform for instantiation and cross-referencing of resources,<br> only relates to resource naming within the module.<br>- source\_folder : Which folder where the {source\_file} lives.<br>- source\_file : The path under {source\_folder} corresponding to the file to upload.<br>- destination\_key : Key in S3 bucket to upload to.<br>- destination\_bucket : The S3 bucket to upload the {source\_file} to.<br><br>OPTIONAL<br>---------<br>- template\_input : A dictionary of variable input for the template file needed for instantiation (leave blank if no template required) | <pre>list(<br> object({<br> name = string,<br> source_folder = string,<br> source_file = string,<br> destination_key = string,<br> destination_bucket = string,<br> template_input = optional(map(string), {})<br> })<br> )</pre> | n/a | yes |

## Outputs

No outputs.
<!-- END_TF_DOCS -->

## Data structure
<POPULATE WITH YOUR DATA STRUCTURE>
```
Data structure
---------------
A list of dictionaries, where each dictionary has the following attributes:

REQUIRED
---------
- name: : Friendly name used through Terraform for instantiation and cross-referencing of resources,
only relates to resource naming within the module.
- source_folder : Which folder where the {source_file} lives.
- source_file : The path under {source_folder} corresponding to the file to upload.
- destination_key : Key in S3 bucket to upload to.
- destination_bucket : The S3 bucket to upload the {source_file} to.

OPTIONAL
---------
- template_input : A dictionary of variable input for the template file needed for instantiation (leave blank if no template required)
```
## Examples
See `examples` folder for an example setup.
26 changes: 26 additions & 0 deletions aws_s3_object.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
All of the S3 files are templated, the difference is without input vars we simply get the content verbatim -
this allows flexibility, as we can handle both templated and non-templated input with the same resource(s).
*/

locals {
actual_s3_files = flatten([
for file in var.raw_s3_files : {
name : file.name,
destination_bucket : file.destination_bucket,
destination_key : file.destination_key,
content : templatefile("${file.source_folder}/${file.source_file}", try(file.template_input, {}))
path : "${file.source_folder}/${file.source_file}"
}
])
}

resource "aws_s3_object" "uploaded_files" {
for_each = { for file in local.actual_s3_files : file.name => file }

bucket = each.value["destination_bucket"]
key = each.value["destination_key"]
content = each.value["content"]

etag = filemd5(each.value["path"])
}
1 change: 1 addition & 0 deletions examples/s3_files/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.5.1
4 changes: 4 additions & 0 deletions examples/s3_files/files/plain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "plain.json",
"description": "I'm a plain file with static content."
}
9 changes: 9 additions & 0 deletions examples/s3_files/files/templated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "templated.json",
"description": "I'm a file that uses terraform templating syntax to dynamically do things.",
"examples": {
"string": ${string_input},
"number": ${number_input},
"list": ${list_input}
}
}
29 changes: 29 additions & 0 deletions examples/s3_files/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
locals {
raw_s3_files = [
{
name : "plain",
source_folder : "${path.module}/files/",
source_file : "plain.json",
destination_bucket : "asset-bucket",
destination_key : "plain-file.json"
},
{
name : "templated",
source_folder : "${path.module}/files/",
source_file : "templated.json",
template_input : {
string_input : "Hello World!",
number_input : "42",
list_input : jsonencode([
"All work and no play makes Jack a dull boy.",
"All work and no play makes Jack a dull boy.",
"All work and no play makes Jack a dull boy.",
"All work and no play makes Jack a dull boy.",
"All work and no play makes Jack a dull boy."
])
},
destination_bucket : "asset-bucket",
destination_key : "templated-file.json"
}
]
}
19 changes: 19 additions & 0 deletions examples/s3_files/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.61.0"
}
}
required_version = "~> 1.5.0"
}

provider "aws" {
region = "eu-west-2"
}

module "s3_files" {
source = "github.com/sudoblark/sudoblark.terraform.module.aws.s3_files?ref=1.0.0"
raw_s3_files = local.raw_s3_files

}
9 changes: 9 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.61.0"
}
}
required_version = "~> 1.5.0"
}
31 changes: 31 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "raw_s3_files" {
description = <<EOT

Data structure
---------------
A list of dictionaries, where each dictionary has the following attributes:

REQUIRED
---------
- name: : Friendly name used through Terraform for instantiation and cross-referencing of resources,
only relates to resource naming within the module.
- source_folder : Which folder where the {source_file} lives.
- source_file : The path under {source_folder} corresponding to the file to upload.
- destination_key : Key in S3 bucket to upload to.
- destination_bucket : The S3 bucket to upload the {source_file} to.

OPTIONAL
---------
- template_input : A dictionary of variable input for the template file needed for instantiation (leave blank if no template required)
EOT
type = list(
object({
name = string,
source_folder = string,
source_file = string,
destination_key = string,
destination_bucket = string,
template_input = optional(map(string), {})
})
)
}
Loading