Skip to content

Commit f0d938a

Browse files
author
Kilian
committed
fix: wrapped image uri around object to avoid unkown amount of resource creation issue of Terraform
1 parent 7f84d15 commit f0d938a

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ This module provides a Lambda function which logs to CloudWatch. If no image URI
2121

2222
## Inputs
2323

24-
| Name | Description | Type | Default | Required |
25-
| ------------- | ------------------------------------------------------------------------- | -------------- | ------- | :------: |
26-
| identifier | Unique identifier to differentiate global resources. | `string` | n/a | yes |
27-
| policies | List of IAM policy ARNs for the Lambda's IAM role. | `list(string)` | [] | no |
28-
| vpc_config | Object to define the subnets and security groups for the Lambda function. | `object` | null | no |
29-
| log | A flag for make the Lambda function submit logs to CloudWatch. | `bool` | false | no |
30-
| image_uri | URI of the image which will be pulled by the Lambda function to execute. | `string` | "" | no |
31-
| memory_size | Amount of memory in MB the Lambda function can use at runtime. | `number` | 128 | no |
32-
| timeout | Amount of time the Lambda function has to run in seconds. | `number` | 3 | no |
33-
| env_variables | A map of environment variables for the Lambda function at runtime. | `map(string)` | {} | no |
34-
| tags | A map of tags to add to all resources. | `map(string)` | {} | no |
24+
| Name | Description | Type | Default | Required |
25+
| ------------- | --------------------------------------------------------------------------- | -------------- | ------- | :------: |
26+
| identifier | Unique identifier to differentiate global resources. | `string` | n/a | yes |
27+
| policies | List of IAM policy ARNs for the Lambda's IAM role. | `list(string)` | [] | no |
28+
| vpc_config | Object to define the subnets and security groups for the Lambda function. | `object` | null | no |
29+
| log | A flag for make the Lambda function submit logs to CloudWatch. | `bool` | false | no |
30+
| image | Object of the image which will be pulled by the Lambda function to execute. | `object` | null | no |
31+
| memory_size | Amount of memory in MB the Lambda function can use at runtime. | `number` | 128 | no |
32+
| timeout | Amount of time the Lambda function has to run in seconds. | `number` | 3 | no |
33+
| env_variables | A map of environment variables for the Lambda function at runtime. | `map(string)` | {} | no |
34+
| tags | A map of tags to add to all resources. | `map(string)` | {} | no |
3535

3636
### `vpc_config`
3737

@@ -40,6 +40,12 @@ This module provides a Lambda function which logs to CloudWatch. If no image URI
4040
| subnets | List of subnet IDs in which the Lambda function will run in. | `list(string)` | n/a | yes |
4141
| security_groups | List of security group IDs the Lambda function will hold. | `list(string)` | n/a | yes |
4242

43+
### `image`
44+
45+
| Name | Description | Type | Default | Required |
46+
| ---- | ----------------- | -------- | ------- | :------: |
47+
| uri | URI to the image. | `string` | n/a | yes |
48+
4349
## Outputs
4450

4551
| Name | Description |
@@ -57,8 +63,10 @@ module "function" {
5763
"arn:aws:iam::aws:policy/aws-service-role/AccessAnalyzerServiceRolePolicy",
5864
"arn:aws:iam::aws:policy/AdministratorAccess-Amplify"
5965
]
60-
log = true
61-
image_uri = "test.registry:latest"
66+
log = true
67+
image = {
68+
uri = "test.registry:latest"
69+
}
6270
memory_size = 128
6371
timeout = 3
6472
env_variables = {

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ resource "aws_iam_role_policy_attachment" "log" {
8989
################################
9090

9191
resource "aws_ecr_repository" "main" {
92-
count = length(var.image_uri) > 0 ? 1 : 0
92+
count = var.image == null ? 1 : 0
9393
name = "${var.identifier}-lambda"
9494
image_tag_mutability = "MUTABLE"
9595
force_delete = true
@@ -105,7 +105,7 @@ resource "aws_lambda_function" "main" {
105105
function_name = var.identifier
106106
package_type = "Image"
107107
role = aws_iam_role.main.arn
108-
image_uri = length(var.image_uri) > 0 ? "${aws_ecr_repository.main[0].repository_url}:latest" : var.image_uri
108+
image_uri = var.image == null ? "${aws_ecr_repository.main[0].repository_url}:latest" : try(var.image["uri"], null)
109109
memory_size = var.memory_size
110110
timeout = var.timeout
111111

variables.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ variable "log" {
3636
default = false
3737
}
3838

39-
variable "image_uri" {
40-
description = "URI of the image which will be pulled by the Lambda function to execute."
41-
type = string
42-
default = ""
39+
variable "image" {
40+
description = "Object of the image which will be pulled by the Lambda function to execute."
41+
type = object({
42+
uri = string
43+
})
44+
default = null
4345
}
4446

4547
variable "memory_size" {

0 commit comments

Comments
 (0)