Skip to content

Commit e9aed29

Browse files
fix: Updated code style to use try() (#256)
Co-authored-by: Kevin Brockhoff <kbrockhoff@codekaizen.org>
1 parent 53c17e5 commit e9aed29

File tree

7 files changed

+40
-39
lines changed

7 files changed

+40
-39
lines changed

iam.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ locals {
33

44
# Lambda@Edge uses the Cloudwatch region closest to the location where the function is executed
55
# The region part of the LogGroup ARN is then replaced with a wildcard (*) so Lambda@Edge is able to log in every region
6-
log_group_arn_regional = element(concat(data.aws_cloudwatch_log_group.lambda.*.arn, aws_cloudwatch_log_group.lambda.*.arn, [""]), 0)
7-
log_group_name = element(concat(data.aws_cloudwatch_log_group.lambda.*.name, aws_cloudwatch_log_group.lambda.*.name, [""]), 0)
6+
log_group_arn_regional = try(data.aws_cloudwatch_log_group.lambda[0].arn, aws_cloudwatch_log_group.lambda[0].arn, "")
7+
log_group_name = try(data.aws_cloudwatch_log_group.lambda[0].name, aws_cloudwatch_log_group.lambda[0].name, "")
88
log_group_arn = local.create_role && var.lambda_at_edge ? format("arn:%s:%s:%s:%s:%s", data.aws_arn.log_group_arn[0].partition, data.aws_arn.log_group_arn[0].service, "*", data.aws_arn.log_group_arn[0].account, data.aws_arn.log_group_arn[0].resource) : local.log_group_arn_regional
99

1010
# Defaulting to "*" (an invalid character for an IAM Role name) will cause an error when

main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
data "aws_partition" "current" {}
22

33
locals {
4-
archive_filename = element(concat(data.external.archive_prepare.*.result.filename, [null]), 0)
5-
archive_was_missing = element(concat(data.external.archive_prepare.*.result.was_missing, [false]), 0)
4+
archive_filename = try(data.external.archive_prepare[0].result.filename, null)
5+
archive_filename_string = local.archive_filename != null ? local.archive_filename : ""
6+
archive_was_missing = try(data.external.archive_prepare[0].result.was_missing, false)
67

78
# Use a generated filename to determine when the source code has changed.
89
# filename - to get package from local
@@ -11,8 +12,8 @@ locals {
1112

1213
# s3_* - to get package from S3
1314
s3_bucket = var.s3_existing_package != null ? lookup(var.s3_existing_package, "bucket", null) : (var.store_on_s3 ? var.s3_bucket : null)
14-
s3_key = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename, "/^.*//", "")) : replace(local.archive_filename, "/^\\.//", "") : null)
15-
s3_object_version = var.s3_existing_package != null ? lookup(var.s3_existing_package, "version_id", null) : (var.store_on_s3 ? element(concat(aws_s3_bucket_object.lambda_package.*.version_id, [null]), 0) : null)
15+
s3_key = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename_string, "/^.*//", "")) : replace(local.archive_filename_string, "/^\\.//", "") : null)
16+
s3_object_version = var.s3_existing_package != null ? lookup(var.s3_existing_package, "version_id", null) : (var.store_on_s3 ? try(aws_s3_bucket_object.lambda_package[0].version_id, null) : null)
1617

1718
}
1819

modules/alias/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
version = element(concat(data.aws_lambda_alias.existing.*.function_version, aws_lambda_alias.with_refresh.*.function_version, aws_lambda_alias.no_refresh.*.function_version, [""]), 0)
2+
version = try(data.aws_lambda_alias.existing[0].function_version, aws_lambda_alias.with_refresh[0].function_version, aws_lambda_alias.no_refresh[0].function_version, "")
33
qualifiers = zipmap(["version", "qualified_alias"], [var.create_version_async_event_config ? true : null, var.create_qualified_alias_async_event_config ? true : null])
44
}
55

modules/alias/outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Lambda Alias
22
output "lambda_alias_name" {
33
description = "The name of the Lambda Function Alias"
4-
value = element(concat(data.aws_lambda_alias.existing.*.name, aws_lambda_alias.with_refresh.*.name, aws_lambda_alias.no_refresh.*.name, [""]), 0)
4+
value = try(data.aws_lambda_alias.existing[0].name, aws_lambda_alias.with_refresh[0].name, aws_lambda_alias.no_refresh[0].name, "")
55
}
66

77
output "lambda_alias_arn" {
88
description = "The ARN of the Lambda Function Alias"
9-
value = element(concat(data.aws_lambda_alias.existing.*.arn, aws_lambda_alias.with_refresh.*.arn, aws_lambda_alias.no_refresh.*.arn, [""]), 0)
9+
value = try(data.aws_lambda_alias.existing[0].arn, aws_lambda_alias.with_refresh[0].arn, aws_lambda_alias.no_refresh[0].arn, "")
1010
}
1111

1212
output "lambda_alias_invoke_arn" {
1313
description = "The ARN to be used for invoking Lambda Function from API Gateway"
14-
value = element(concat(data.aws_lambda_alias.existing.*.invoke_arn, aws_lambda_alias.with_refresh.*.invoke_arn, aws_lambda_alias.no_refresh.*.invoke_arn, [""]), 0)
14+
value = try(data.aws_lambda_alias.existing[0].invoke_arn, aws_lambda_alias.with_refresh[0].invoke_arn, aws_lambda_alias.no_refresh[0].invoke_arn, "")
1515
}
1616

1717
output "lambda_alias_description" {
1818
description = "Description of alias"
19-
value = element(concat(data.aws_lambda_alias.existing.*.description, aws_lambda_alias.with_refresh.*.description, aws_lambda_alias.no_refresh.*.description, [""]), 0)
19+
value = try(data.aws_lambda_alias.existing[0].description, aws_lambda_alias.with_refresh[0].description, aws_lambda_alias.no_refresh[0].description, "")
2020
}
2121

2222
output "lambda_alias_function_version" {
2323
description = "Lambda function version which the alias uses"
24-
value = element(concat(data.aws_lambda_alias.existing.*.function_version, aws_lambda_alias.with_refresh.*.function_version, aws_lambda_alias.no_refresh.*.function_version, [""]), 0)
24+
value = try(data.aws_lambda_alias.existing[0].function_version, aws_lambda_alias.with_refresh[0].function_version, aws_lambda_alias.no_refresh[0].function_version, "")
2525
}

modules/deploy/main.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
locals {
22
# AWS CodeDeploy can't deploy when CurrentVersion is "$LATEST"
3-
qualifier = element(concat(data.aws_lambda_function.this.*.qualifier, [""]), 0)
3+
qualifier = try(data.aws_lambda_function.this[0].qualifier, "")
44
current_version = local.qualifier == "$LATEST" ? 1 : local.qualifier
55

6-
app_name = element(concat(aws_codedeploy_app.this.*.name, [var.app_name]), 0)
7-
deployment_group_name = element(concat(aws_codedeploy_deployment_group.this.*.deployment_group_name, [var.deployment_group_name]), 0)
6+
app_name = try(aws_codedeploy_app.this[0].name, var.app_name)
7+
deployment_group_name = try(aws_codedeploy_deployment_group.this[0].deployment_group_name, var.deployment_group_name)
88

99
appspec = merge({
1010
version = "0.0"
@@ -140,7 +140,7 @@ resource "aws_codedeploy_deployment_group" "this" {
140140

141141
app_name = local.app_name
142142
deployment_group_name = var.deployment_group_name
143-
service_role_arn = element(concat(aws_iam_role.codedeploy.*.arn, data.aws_iam_role.codedeploy.*.arn, [""]), 0)
143+
service_role_arn = try(aws_iam_role.codedeploy[0].arn, data.aws_iam_role.codedeploy[0].arn, "")
144144
deployment_config_name = var.deployment_config_name
145145

146146
deployment_style {
@@ -208,7 +208,7 @@ data "aws_iam_policy_document" "assume_role" {
208208
resource "aws_iam_role_policy_attachment" "codedeploy" {
209209
count = var.create && var.create_codedeploy_role ? 1 : 0
210210

211-
role = element(concat(aws_iam_role.codedeploy.*.id, [""]), 0)
211+
role = try(aws_iam_role.codedeploy[0].id, "")
212212
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda"
213213
}
214214

@@ -237,7 +237,7 @@ resource "aws_iam_policy" "hooks" {
237237
resource "aws_iam_role_policy_attachment" "hooks" {
238238
count = var.create && var.create_codedeploy_role && var.attach_hooks_policy && (var.before_allow_traffic_hook_arn != "" || var.after_allow_traffic_hook_arn != "") ? 1 : 0
239239

240-
role = element(concat(aws_iam_role.codedeploy.*.id, [""]), 0)
240+
role = try(aws_iam_role.codedeploy[0].id, "")
241241
policy_arn = aws_iam_policy.hooks[0].arn
242242
}
243243

@@ -265,7 +265,7 @@ resource "aws_iam_policy" "triggers" {
265265
resource "aws_iam_role_policy_attachment" "triggers" {
266266
count = var.create && var.create_codedeploy_role && var.attach_triggers_policy ? 1 : 0
267267

268-
role = element(concat(aws_iam_role.codedeploy.*.id, [""]), 0)
268+
role = try(aws_iam_role.codedeploy[0].id, "")
269269
policy_arn = aws_iam_policy.triggers[0].arn
270270
}
271271

modules/deploy/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ output "codedeploy_deployment_group_name" {
1010

1111
output "codedeploy_deployment_group_id" {
1212
description = "CodeDeploy deployment group id"
13-
value = element(concat(aws_codedeploy_deployment_group.this.*.id, [""]), 0)
13+
value = try(aws_codedeploy_deployment_group.this[0].id, "")
1414
}
1515

1616
output "codedeploy_iam_role_name" {
1717
description = "Name of IAM role used by CodeDeploy"
18-
value = element(concat(aws_iam_role.codedeploy.*.name, [""]), 0)
18+
value = try(aws_iam_role.codedeploy[0].name, "")
1919
}
2020

2121
output "appspec" {
@@ -40,5 +40,5 @@ output "script" {
4040

4141
output "deploy_script" {
4242
description = "Path to a deployment script"
43-
value = element(concat(local_file.deploy_script.*.filename, [""]), 0)
43+
value = try(local_file.deploy_script[0].filename, "")
4444
}

outputs.tf

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
# Lambda Function
22
output "lambda_function_arn" {
33
description = "The ARN of the Lambda Function"
4-
value = element(concat(aws_lambda_function.this.*.arn, [""]), 0)
4+
value = try(aws_lambda_function.this[0].arn, "")
55
}
66

77
output "lambda_function_invoke_arn" {
88
description = "The Invoke ARN of the Lambda Function"
9-
value = element(concat(aws_lambda_function.this.*.invoke_arn, [""]), 0)
9+
value = try(aws_lambda_function.this[0].invoke_arn, "")
1010
}
1111

1212
output "lambda_function_name" {
1313
description = "The name of the Lambda Function"
14-
value = element(concat(aws_lambda_function.this.*.function_name, [""]), 0)
14+
value = try(aws_lambda_function.this[0].function_name, "")
1515
}
1616

1717
output "lambda_function_qualified_arn" {
1818
description = "The ARN identifying your Lambda Function Version"
19-
value = element(concat(aws_lambda_function.this.*.qualified_arn, [""]), 0)
19+
value = try(aws_lambda_function.this[0].qualified_arn, "")
2020
}
2121

2222
output "lambda_function_version" {
2323
description = "Latest published version of Lambda Function"
24-
value = element(concat(aws_lambda_function.this.*.version, [""]), 0)
24+
value = try(aws_lambda_function.this[0].version, "")
2525
}
2626

2727
output "lambda_function_last_modified" {
2828
description = "The date Lambda Function resource was last modified"
29-
value = element(concat(aws_lambda_function.this.*.last_modified, [""]), 0)
29+
value = try(aws_lambda_function.this[0].last_modified, "")
3030
}
3131

3232
output "lambda_function_kms_key_arn" {
3333
description = "The ARN for the KMS encryption key of Lambda Function"
34-
value = element(concat(aws_lambda_function.this.*.kms_key_arn, [""]), 0)
34+
value = try(aws_lambda_function.this[0].kms_key_arn, "")
3535
}
3636

3737
output "lambda_function_source_code_hash" {
3838
description = "Base64-encoded representation of raw SHA-256 sum of the zip file"
39-
value = element(concat(aws_lambda_function.this.*.source_code_hash, [""]), 0)
39+
value = try(aws_lambda_function.this[0].source_code_hash, "")
4040
}
4141

4242
output "lambda_function_source_code_size" {
4343
description = "The size in bytes of the function .zip file"
44-
value = element(concat(aws_lambda_function.this.*.source_code_size, [""]), 0)
44+
value = try(aws_lambda_function.this[0].source_code_size, "")
4545
}
4646

4747
# Lambda Layer
4848
output "lambda_layer_arn" {
4949
description = "The ARN of the Lambda Layer with version"
50-
value = element(concat(aws_lambda_layer_version.this.*.arn, [""]), 0)
50+
value = try(aws_lambda_layer_version.this[0].arn, "")
5151
}
5252

5353
output "lambda_layer_layer_arn" {
5454
description = "The ARN of the Lambda Layer without version"
55-
value = element(concat(aws_lambda_layer_version.this.*.layer_arn, [""]), 0)
55+
value = try(aws_lambda_layer_version.this[0].layer_arn, "")
5656
}
5757

5858
output "lambda_layer_created_date" {
5959
description = "The date Lambda Layer resource was created"
60-
value = element(concat(aws_lambda_layer_version.this.*.created_date, [""]), 0)
60+
value = try(aws_lambda_layer_version.this[0].created_date, "")
6161
}
6262

6363
output "lambda_layer_source_code_size" {
6464
description = "The size in bytes of the Lambda Layer .zip file"
65-
value = element(concat(aws_lambda_layer_version.this.*.source_code_size, [""]), 0)
65+
value = try(aws_lambda_layer_version.this[0].source_code_size, "")
6666
}
6767

6868
output "lambda_layer_version" {
6969
description = "The Lambda Layer version"
70-
value = element(concat(aws_lambda_layer_version.this.*.version, [""]), 0)
70+
value = try(aws_lambda_layer_version.this[0].version, "")
7171
}
7272

7373
# Lambda Event Source Mapping
@@ -94,17 +94,17 @@ output "lambda_event_source_mapping_uuid" {
9494
# IAM Role
9595
output "lambda_role_arn" {
9696
description = "The ARN of the IAM role created for the Lambda Function"
97-
value = element(concat(aws_iam_role.lambda.*.arn, [""]), 0)
97+
value = try(aws_iam_role.lambda[0].arn, "")
9898
}
9999

100100
output "lambda_role_name" {
101101
description = "The name of the IAM role created for the Lambda Function"
102-
value = element(concat(aws_iam_role.lambda.*.name, [""]), 0)
102+
value = try(aws_iam_role.lambda[0].name, "")
103103
}
104104

105105
output "lambda_role_unique_id" {
106106
description = "The unique id of the IAM role created for the Lambda Function"
107-
value = element(concat(aws_iam_role.lambda.*.unique_id, [""]), 0)
107+
value = try(aws_iam_role.lambda[0].unique_id, "")
108108
}
109109

110110
# CloudWatch Log Group

0 commit comments

Comments
 (0)