Skip to content

Commit

Permalink
refactor: replace samversion with sam_asset (#168)
Browse files Browse the repository at this point in the history
The previous module was overly restrictive, by assuming that a caller
would always want to know only the CodeUri of a few functions. Make the
base module more generic: given an asset and a version, return the body.
The primary value the module provides is to give us a consistent default
version.
  • Loading branch information
jta authored Jun 7, 2024
1 parent 99253b7 commit 22ff86a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 49 deletions.
2 changes: 1 addition & 1 deletion modules/forwarder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module "forwarder" {

| Name | Source | Version |
|------|--------|---------|
| <a name="module_samversion"></a> [samversion](#module\_samversion) | ../samversion | n/a |
| <a name="module_sam_asset"></a> [sam\_asset](#module\_sam\_asset) | ../sam_asset | n/a |

## Resources

Expand Down
13 changes: 6 additions & 7 deletions modules/forwarder/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
code_uri = var.code_uri != "" ? var.code_uri : module.samversion[0].code_uri
code_uri = var.code_uri != "" ? var.code_uri : yamldecode(module.sam_asset[0].body)["Resources"]["Forwarder"]["Properties"]["CodeUri"]
parsed_s3_uri = regex("s3://(?P<bucket>[^/]+)/(?P<key>.+)", local.code_uri)
destination_uri = var.destination.uri != "" ? var.destination.uri : "s3://${var.destination.bucket}/${var.destination.prefix}"
name_prefix = "${substr(var.name, 0, 37)}-"
Expand All @@ -21,10 +21,9 @@ data "aws_region" "current" {}

data "aws_partition" "current" {}

module "samversion" {
count = var.code_uri != "" ? 0 : 1
source = "../samversion"
app = "forwarder"
function = "Forwarder"
release = var.sam_release_version
module "sam_asset" {
count = var.code_uri != "" ? 0 : 1
source = "../sam_asset"
asset = "forwarder.yaml"
release_version = var.sam_release_version
}
18 changes: 18 additions & 0 deletions modules/sam_asset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SAM Asset

This is a helper module to retrieve objects for a given release version from
https://github.com/observeinc/aws-sam-apps.

If no `release_version` is provided, we will default to a hard-coded value
which is maintained up to date by `updatecli`. This ensures that a given
terraform module release produces the same behavior across time.

## Usage

```hcl
module "sam_asset" {
source = "observeinc/collection/aws//modules/sam_asset"
asset = "forwarder.yaml"
release_version = "1.19.3"
}
```
7 changes: 3 additions & 4 deletions modules/samversion/main.tf → modules/sam_asset/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
locals {
latest_release = "1.19.3"
sam_release_version = var.release != "" ? var.release : local.latest_release
code_uri = yamldecode(data.http.manifest.response_body)["Resources"][var.function]["Properties"]["CodeUri"]
latest_release = "1.19.3"
release_version = var.release_version != "" ? var.release_version : local.latest_release
}

data "aws_region" "current" {}

data "http" "manifest" {
url = "https://observeinc-${data.aws_region.current.name}.s3.amazonaws.com/aws-sam-apps/${local.sam_release_version}/${var.app}.yaml"
url = "https://observeinc-${data.aws_region.current.name}.s3.amazonaws.com/aws-sam-apps/${local.release_version}/${var.asset}"

lifecycle {
postcondition {
Expand Down
4 changes: 4 additions & 0 deletions modules/sam_asset/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "body" {
description = "Response body"
value = data.http.manifest.response_body
}
16 changes: 6 additions & 10 deletions modules/samversion/variables.tf → modules/sam_asset/variables.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
variable "app" {
description = "App name"
variable "asset" {
description = <<-EOF
Asset key. This value is appended to the SAM apps version prefix to form
the object key.
EOF
type = string
nullable = false
}

variable "function" {
description = "Function name"
type = string
default = ""
nullable = false
}

variable "release" {
variable "release_version" {
description = "Release version on github.com/observeinc/aws-sam-apps."
type = string
default = ""
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions modules/samversion/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions modules/samversion/outputs.tf

This file was deleted.

2 changes: 1 addition & 1 deletion modules/subscriber/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This app is specifically to register new cloudwatch log groups for the `logwrite

| Name | Source | Version |
|------|--------|---------|
| <a name="module_samversion"></a> [samversion](#module\_samversion) | ../samversion | n/a |
| <a name="module_sam_asset"></a> [sam\_asset](#module\_sam\_asset) | ../sam_asset | n/a |

## Resources

Expand Down
13 changes: 6 additions & 7 deletions modules/subscriber/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ locals {
has_discovery_rate = var.discovery_rate != ""
name_prefix = "${substr(var.name, 0, 37)}-"

code_uri = var.code_uri != "" ? var.code_uri : module.samversion[0].code_uri
code_uri = var.code_uri != "" ? var.code_uri : yamldecode(module.sam_asset[0].body)["Resources"]["Subscriber"]["Properties"]["CodeUri"]
parsed_s3_uri = regex("s3://(?P<bucket>[^/]+)/(?P<key>.+)", local.code_uri)
}

module "samversion" {
count = var.code_uri != "" ? 0 : 1
source = "../samversion"
app = "logwriter"
function = "Subscriber"
release = var.sam_release_version
module "sam_asset" {
count = var.code_uri != "" ? 0 : 1
source = "../sam_asset"
asset = "logwriter.yaml"
release_version = var.sam_release_version
}

0 comments on commit 22ff86a

Please sign in to comment.