Skip to content

Commit 9b01756

Browse files
authored
feat: make SSM parameters optional, add name validation, fix build path (#2)
- Add enable_ssm_parameters variable (defaults true for backward compat) - Add lifecycle precondition to validate name/context is provided - Move archive output from .terraform/tmp/ to build/ - Add build/ to .gitignore
1 parent 4ea0a2c commit 9b01756

5 files changed

Lines changed: 97 additions & 20 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ terraform.tfstate.backup
1010
# OS
1111
.DS_Store
1212
Thumbs.db
13+
build/

‎README.md‎

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,83 @@ module "prebuilt_layer" {
4141
}
4242
```
4343

44+
<!-- BEGIN_TF_DOCS -->
45+
## Requirements
46+
47+
| Name | Version |
48+
| ---- | ------- |
49+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
50+
| <a name="requirement_archive"></a> [archive](#requirement\_archive) | >= 2.0 |
51+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0 |
52+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.0 |
53+
54+
## Providers
55+
56+
| Name | Version |
57+
| ---- | ------- |
58+
| <a name="provider_archive"></a> [archive](#provider\_archive) | >= 2.0 |
59+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0 |
60+
61+
## Modules
62+
63+
| Name | Source | Version |
64+
| ---- | ------ | ------- |
65+
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
66+
67+
## Resources
68+
69+
| Name | Type |
70+
| ---- | ---- |
71+
| [aws_lambda_layer_version.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version) | resource |
72+
| [aws_ssm_parameter.layer_arn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
73+
| [aws_ssm_parameter.layer_version](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | resource |
74+
| [archive_file.layer_zip](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
75+
4476
## Inputs
4577

4678
| Name | Description | Type | Default | Required |
47-
|------|-------------|------|---------|:--------:|
48-
| source_dir | Path to source directory containing layer code | `string` | `null` | no |
49-
| filename | Path to pre-built zip file | `string` | `null` | no |
50-
| compatible_runtimes | List of compatible runtimes | `list(string)` | `["provided.al2023"]` | no |
51-
| compatible_architectures | List of compatible architectures | `list(string)` | `["arm64"]` | no |
52-
| description | Layer description | `string` | `null` | no |
53-
| license_info | License information | `string` | `null` | no |
79+
| ---- | ----------- | ---- | ------- | :------: |
80+
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br/>This is for some rare cases where resources want additional configuration of tags<br/>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
81+
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br/>in the order they appear in the list. New attributes are appended to the<br/>end of the list. The elements of the list are joined by the `delimiter`<br/>and treated as a single ID element. | `list(string)` | `[]` | no |
82+
| <a name="input_compatible_architectures"></a> [compatible\_architectures](#input\_compatible\_architectures) | List of architectures this layer is compatible with | `list(string)` | <pre>[<br/> "arm64"<br/>]</pre> | no |
83+
| <a name="input_compatible_runtimes"></a> [compatible\_runtimes](#input\_compatible\_runtimes) | List of runtimes this layer is compatible with | `list(string)` | <pre>[<br/> "provided.al2023"<br/>]</pre> | no |
84+
| <a name="input_context"></a> [context](#input\_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. | `any` | <pre>{<br/> "additional_tag_map": {},<br/> "attributes": [],<br/> "delimiter": null,<br/> "descriptor_formats": {},<br/> "enabled": true,<br/> "environment": null,<br/> "id_length_limit": null,<br/> "label_key_case": null,<br/> "label_order": [],<br/> "label_value_case": null,<br/> "labels_as_tags": [<br/> "unset"<br/> ],<br/> "name": null,<br/> "namespace": null,<br/> "regex_replace_chars": null,<br/> "stage": null,<br/> "tags": {},<br/> "tenant": null<br/>}</pre> | no |
85+
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br/>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
86+
| <a name="input_description"></a> [description](#input\_description) | Description of the layer | `string` | `null` | no |
87+
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br/>Map of maps. Keys are names of descriptors. Values are maps of the form<br/>`{<br/> format = string<br/> labels = list(string)<br/>}`<br/>(Type is `any` so the map values can later be enhanced to provide additional options.)<br/>`format` is a Terraform format string to be passed to the `format()` function.<br/>`labels` is a list of labels, in order, to pass to `format()` function.<br/>Label values will be normalized before being passed to `format()` so they will be<br/>identical to how they appear in `id`.<br/>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
88+
| <a name="input_enable_ssm_parameters"></a> [enable\_ssm\_parameters](#input\_enable\_ssm\_parameters) | Whether to create SSM parameters for layer ARN and version | `bool` | `true` | no |
89+
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
90+
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
91+
| <a name="input_filename"></a> [filename](#input\_filename) | Path to pre-built zip file (alternative to source\_dir) | `string` | `null` | no |
92+
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br/>Set to `0` for unlimited length.<br/>Set to `null` for keep the existing setting, which defaults to `0`.<br/>Does not affect `id_full`. | `number` | `null` | no |
93+
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br/>Does not affect keys of tags passed in via the `tags` input.<br/>Possible values: `lower`, `title`, `upper`.<br/>Default value: `title`. | `string` | `null` | no |
94+
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br/>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br/>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
95+
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br/>set as tag values, and output by this module individually.<br/>Does not affect values of tags passed in via the `tags` input.<br/>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br/>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br/>Default value: `lower`. | `string` | `null` | no |
96+
| <a name="input_labels_as_tags"></a> [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.<br/>Default is to include all labels.<br/>Tags with empty values will not be included in the `tags` output.<br/>Set to `[]` to suppress all generated tags.<br/>**Notes:**<br/> The value of the `name` tag, if included, will be the `id`, not the `name`.<br/> Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be<br/> changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` | <pre>[<br/> "default"<br/>]</pre> | no |
97+
| <a name="input_license_info"></a> [license\_info](#input\_license\_info) | License info for the layer | `string` | `null` | no |
98+
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br/>This is the only ID element not also included as a `tag`.<br/>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
99+
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
100+
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br/>Characters matching the regex will be removed from the ID elements.<br/>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
101+
| <a name="input_source_dir"></a> [source\_dir](#input\_source\_dir) | Path to the source directory containing layer code | `string` | `null` | no |
102+
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
103+
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br/>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
104+
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
54105

55106
## Outputs
56107

57108
| Name | Description |
58-
|------|-------------|
59-
| layer_arn | Lambda layer ARN |
60-
| layer_version | Lambda layer version |
61-
| package_path | Path to deployment package |
62-
| ssm_parameters | SSM parameter names for integration |
109+
| ---- | ----------- |
110+
| <a name="output_layer_arn"></a> [layer\_arn](#output\_layer\_arn) | Lambda layer ARN |
111+
| <a name="output_layer_created_date"></a> [layer\_created\_date](#output\_layer\_created\_date) | Lambda layer creation date |
112+
| <a name="output_layer_source_code_hash"></a> [layer\_source\_code\_hash](#output\_layer\_source\_code\_hash) | Lambda layer source code hash |
113+
| <a name="output_layer_source_code_size"></a> [layer\_source\_code\_size](#output\_layer\_source\_code\_size) | Lambda layer source code size |
114+
| <a name="output_layer_version"></a> [layer\_version](#output\_layer\_version) | Lambda layer version |
115+
| <a name="output_package_path"></a> [package\_path](#output\_package\_path) | Path to the layer deployment package |
116+
| <a name="output_package_size"></a> [package\_size](#output\_package\_size) | Size of the layer deployment package |
117+
| <a name="output_ssm_parameters"></a> [ssm\_parameters](#output\_ssm\_parameters) | SSM parameter names for layer integration |
118+
<!-- END_TF_DOCS -->
119+
120+
63121

64122
## Layer Structure
65123

@@ -70,4 +128,5 @@ layer/
70128
├── bin/ # Executables
71129
├── lib/ # Libraries
72130
└── python/ # Python packages (if using Python runtime)
73-
```
131+
```
132+

‎main.tf‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ data "archive_file" "layer_zip" {
99

1010
type = "zip"
1111
source_dir = local.source_dir
12-
output_path = "${path.module}/.terraform/tmp/${module.this.id}.zip"
12+
output_path = "${path.module}/build/${module.this.id}.zip"
1313
}
1414

1515
# Lambda layer
@@ -25,10 +25,19 @@ resource "aws_lambda_layer_version" "this" {
2525
license_info = var.license_info
2626

2727
depends_on = [data.archive_file.layer_zip]
28+
29+
lifecycle {
30+
precondition {
31+
condition = module.this.id != ""
32+
error_message = "A 'name' or 'context' with a name must be provided."
33+
}
34+
}
2835
}
2936

3037
# SSM parameters for layer integration
3138
resource "aws_ssm_parameter" "layer_arn" {
39+
count = var.enable_ssm_parameters ? 1 : 0
40+
3241
name = "/${module.this.id}/layer_arn"
3342
type = "String"
3443
value = aws_lambda_layer_version.this.arn
@@ -37,6 +46,8 @@ resource "aws_ssm_parameter" "layer_arn" {
3746
}
3847

3948
resource "aws_ssm_parameter" "layer_version" {
49+
count = var.enable_ssm_parameters ? 1 : 0
50+
4051
name = "/${module.this.id}/layer_version"
4152
type = "String"
4253
value = aws_lambda_layer_version.this.version

‎outputs.tf‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ output "package_size" {
3838
# SSM parameter outputs
3939
output "ssm_parameters" {
4040
description = "SSM parameter names for layer integration"
41-
value = {
42-
layer_arn = aws_ssm_parameter.layer_arn.name
43-
layer_version = aws_ssm_parameter.layer_version.name
44-
}
41+
value = var.enable_ssm_parameters ? {
42+
layer_arn = aws_ssm_parameter.layer_arn[0].name
43+
layer_version = aws_ssm_parameter.layer_version[0].name
44+
} : null
4545
}

‎variables.tf‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ variable "compatible_architectures" {
2020
type = list(string)
2121
description = "List of architectures this layer is compatible with"
2222
default = ["arm64"]
23-
23+
2424
validation {
2525
condition = alltrue([
2626
for arch in var.compatible_architectures : contains(["x86_64", "arm64"], arch)
@@ -39,4 +39,10 @@ variable "license_info" {
3939
type = string
4040
description = "License info for the layer"
4141
default = null
42-
}
42+
}
43+
44+
variable "enable_ssm_parameters" {
45+
type = bool
46+
description = "Whether to create SSM parameters for layer ARN and version"
47+
default = true
48+
}

0 commit comments

Comments
 (0)