Skip to content

Commit bcee152

Browse files
authored
feat: initial version (#1)
1 parent f60c255 commit bcee152

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Terraform
2+
*.tfstate
3+
*.tfstate.*
4+
*.tfvars
5+
*.tfvars.json
6+
.terraform/
7+
.terraform.lock.hcl
8+
crash.log
9+
crash.*.log
10+
11+
# OS
12+
.DS_Store
13+
Thumbs.db
14+
15+
# IDE
16+
.vscode/
17+
.idea/
18+
*.swp
19+
*.swo
20+
21+
# Logs
22+
*.log

context.tf

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#
2+
# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label
3+
# All other instances of this file should be a copy of that one
4+
#
5+
#
6+
# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf
7+
# and then place it in your Terraform module to automatically get
8+
# Cloud Posse's standard configuration inputs suitable for passing
9+
# to Cloud Posse modules.
10+
#
11+
# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf
12+
#
13+
# Modules should access the whole context as `module.this.context`
14+
# to get the input variables with nulls for defaults,
15+
# for example `context = module.this.context`,
16+
# and access individual variables as `module.this.<var>`,
17+
# with final values filled in.
18+
#
19+
# For example, when using defaults, `module.this.context.delimiter`
20+
# will be null, and `module.this.delimiter` will be `-` (hyphen).
21+
#
22+
23+
module "this" {
24+
source = "cloudposse/label/null"
25+
version = "0.25.0" # requires Terraform >= 0.13.0
26+
27+
enabled = var.enabled
28+
namespace = var.namespace
29+
tenant = var.tenant
30+
environment = var.environment
31+
stage = var.stage
32+
name = var.name
33+
delimiter = var.delimiter
34+
attributes = var.attributes
35+
tags = var.tags
36+
additional_tag_map = var.additional_tag_map
37+
label_order = var.label_order
38+
regex_replace_chars = var.regex_replace_chars
39+
id_length_limit = var.id_length_limit
40+
label_key_case = var.label_key_case
41+
label_value_case = var.label_value_case
42+
descriptor_formats = var.descriptor_formats
43+
labels_as_tags = var.labels_as_tags
44+
45+
context = var.context
46+
}
47+
48+
# Copy contents of cloudposse/terraform-null-label/variables.tf here
49+
50+
variable "context" {
51+
type = any
52+
default = {
53+
enabled = true
54+
namespace = null
55+
tenant = null
56+
environment = null
57+
stage = null
58+
name = null
59+
delimiter = null
60+
attributes = []
61+
tags = {}
62+
additional_tag_map = {}
63+
regex_replace_chars = null
64+
label_order = []
65+
id_length_limit = null
66+
label_key_case = null
67+
label_value_case = null
68+
descriptor_formats = {}
69+
labels_as_tags = ["unset"]
70+
}
71+
description = <<-EOT
72+
Single object for setting entire context at once.
73+
See description of individual variables for details.
74+
Leave string and numeric variables as `null` to use default value.
75+
Individual variable settings (non-null) override settings in context object,
76+
except for attributes, tags, and additional_tag_map, which are merged.
77+
EOT
78+
79+
validation {
80+
condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"])
81+
error_message = "Allowed values: `lower`, `title`, `upper`."
82+
}
83+
84+
validation {
85+
condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"])
86+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
87+
}
88+
}
89+
90+
variable "enabled" {
91+
type = bool
92+
default = null
93+
description = "Set to false to prevent the module from creating any resources"
94+
}
95+
96+
variable "namespace" {
97+
type = string
98+
default = null
99+
description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique"
100+
}
101+
102+
variable "tenant" {
103+
type = string
104+
default = null
105+
description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for"
106+
}
107+
108+
variable "environment" {
109+
type = string
110+
default = null
111+
description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'"
112+
}
113+
114+
variable "stage" {
115+
type = string
116+
default = null
117+
description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'"
118+
}
119+
120+
variable "name" {
121+
type = string
122+
default = null
123+
description = <<-EOT
124+
ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
125+
This is the only ID element not also included as a `tag`.
126+
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input.
127+
EOT
128+
}
129+
130+
variable "delimiter" {
131+
type = string
132+
default = null
133+
description = <<-EOT
134+
Delimiter to be used between ID elements.
135+
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all.
136+
EOT
137+
}
138+
139+
variable "attributes" {
140+
type = list(string)
141+
default = []
142+
description = <<-EOT
143+
ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
144+
in the order they appear in the list. New attributes are appended to the
145+
end of the list. The elements of the list are joined by the `delimiter`
146+
and treated as a single ID element.
147+
EOT
148+
}
149+
150+
variable "labels_as_tags" {
151+
type = set(string)
152+
default = ["default"]
153+
description = <<-EOT
154+
Set of labels (ID elements) to include as tags in the `tags` output.
155+
Default is to include all labels.
156+
Tags with empty values will not be included in the `tags` output.
157+
Set to `[]` to suppress all generated tags.
158+
**Notes:**
159+
The value of the `name` tag, if included, will be the `id`, not the `name`.
160+
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
161+
changed in later chained modules. Attempts to change it will be silently ignored.
162+
EOT
163+
}
164+
165+
variable "tags" {
166+
type = map(string)
167+
default = {}
168+
description = <<-EOT
169+
Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
170+
Neither the tag keys nor the tag values will be modified by this module.
171+
EOT
172+
}
173+
174+
variable "additional_tag_map" {
175+
type = map(string)
176+
default = {}
177+
description = <<-EOT
178+
Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
179+
This is for some rare cases where resources want additional configuration of tags
180+
and therefore take a list of maps with tag key, value, and additional configuration.
181+
EOT
182+
}
183+
184+
variable "label_order" {
185+
type = list(string)
186+
default = null
187+
description = <<-EOT
188+
The order in which the labels (ID elements) appear in the `id`.
189+
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
190+
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
191+
EOT
192+
}
193+
194+
variable "regex_replace_chars" {
195+
type = string
196+
default = null
197+
description = <<-EOT
198+
Terraform regular expression (regex) string.
199+
Characters matching the regex will be removed from the ID elements.
200+
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits.
201+
EOT
202+
}
203+
204+
variable "id_length_limit" {
205+
type = number
206+
default = null
207+
description = <<-EOT
208+
Limit `id` to this many characters (minimum 6).
209+
Set to `0` for unlimited length.
210+
Set to `null` for keep the existing setting, which defaults to `0`.
211+
Does not affect `id_full`.
212+
EOT
213+
validation {
214+
condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0
215+
error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length."
216+
}
217+
}
218+
219+
variable "label_key_case" {
220+
type = string
221+
default = null
222+
description = <<-EOT
223+
Controls the letter case of the `tags` keys (label names) for tags generated by this module.
224+
Does not affect keys of tags passed in via the `tags` input.
225+
Possible values: `lower`, `title`, `upper`.
226+
Default value: `title`.
227+
EOT
228+
229+
validation {
230+
condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case)
231+
error_message = "Allowed values: `lower`, `title`, `upper`."
232+
}
233+
}
234+
235+
variable "label_value_case" {
236+
type = string
237+
default = null
238+
description = <<-EOT
239+
Controls the letter case of ID elements (labels) as included in `id`,
240+
set as tag values, and output by this module individually.
241+
Does not affect values of tags passed in via the `tags` input.
242+
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
243+
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
244+
Default value: `lower`.
245+
EOT
246+
247+
validation {
248+
condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case)
249+
error_message = "Allowed values: `lower`, `title`, `upper`, `none`."
250+
}
251+
}
252+
253+
variable "descriptor_formats" {
254+
type = any
255+
default = {}
256+
description = <<-EOT
257+
Describe additional descriptors to be output in the `descriptors` output map.
258+
Map of maps. Keys are names of descriptors. Values are maps of the form
259+
`{
260+
format = string
261+
labels = list(string)
262+
}`
263+
(Type is `any` so the map values can later be enhanced to provide additional options.)
264+
`format` is a Terraform format string to be passed to the `format()` function.
265+
`labels` is a list of labels, in order, to pass to `format()` function.
266+
Label values will be normalized before being passed to `format()` so they will be
267+
identical to how they appear in `id`.
268+
Default is `{}` (`descriptors` output will be empty).
269+
EOT
270+
}
271+
272+
#### End of copy of cloudposse/terraform-null-label/variables.tf

main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module "ecr" {
2+
source = "cloudposse/ecr/aws"
3+
version = "0.42.1"
4+
5+
context = module.this.context
6+
attributes = concat(["lambda-runtime"], var.attributes)
7+
8+
image_tag_mutability = "MUTABLE"
9+
force_delete = true
10+
}
11+
12+
locals {
13+
deploy_tag = var.deploy_tag
14+
15+
ssm_prefix = join("/", concat([
16+
"",
17+
module.this.namespace,
18+
module.this.name,
19+
], module.this.attributes))
20+
}

outputs.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
output "ecr" {
2+
description = "ECR repository details"
3+
value = module.ecr
4+
}
5+
6+
output "image" {
7+
description = "Lambda runtime image details"
8+
sensitive = true
9+
value = {
10+
name = "${module.ecr.repository_url}:${local.deploy_tag}"
11+
arn = "${module.ecr.repository_arn}:${local.deploy_tag}"
12+
ssm_name = aws_ssm_parameter.image.name
13+
}
14+
}
15+
16+
output "repository_url" {
17+
description = "ECR repository URL"
18+
value = module.ecr.repository_url
19+
}
20+
21+
output "repository_arn" {
22+
description = "ECR repository ARN"
23+
value = module.ecr.repository_arn
24+
}

ssm.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws_ssm_parameter" "image" {
2+
name = "${local.ssm_prefix}/image"
3+
type = "String"
4+
value = "${module.ecr.repository_url}:${local.deploy_tag}"
5+
6+
tags = module.this.tags
7+
}

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "deploy_tag" {
2+
type = string
3+
description = "Docker image tag for deployment"
4+
default = "latest"
5+
}

0 commit comments

Comments
 (0)