Repository navigation
Expand file tree
/
Copy pathmain.tf
More file actions
82 lines (62 loc) · 2.14 KB
/
Copy pathmain.tf
File metadata and controls
82 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
locals {
use_prebuilt_zip = var.filename != null
use_source_url = var.source_url != null
use_source_dir = !local.use_prebuilt_zip && !local.use_source_url
download_path = "${path.module}/build/${module.this.id}.zip"
layer_filename = (
local.use_prebuilt_zip ? var.filename :
local.use_source_url ? local.download_path :
data.archive_file.layer_zip[0].output_path
)
layer_source_code_hash = (
local.use_source_dir ? data.archive_file.layer_zip[0].output_base64sha256 : null
)
}
# Download zip from URL
resource "terraform_data" "download" {
count = local.use_source_url ? 1 : 0
input = var.source_url
provisioner "local-exec" {
command = "mkdir -p $(dirname ${local.download_path}) && curl -sSfL -o ${local.download_path} ${var.source_url}"
}
triggers_replace = [var.source_url]
}
# Create zip package from source directory
data "archive_file" "layer_zip" {
count = local.use_source_dir ? 1 : 0
type = "zip"
source_dir = var.source_dir
output_path = "${path.module}/build/${module.this.id}.zip"
}
# Lambda layer
resource "aws_lambda_layer_version" "this" {
layer_name = module.this.id
filename = local.layer_filename
source_code_hash = local.layer_source_code_hash
compatible_runtimes = var.compatible_runtimes
compatible_architectures = var.compatible_architectures
description = var.description
license_info = var.license_info
depends_on = [data.archive_file.layer_zip, terraform_data.download]
lifecycle {
precondition {
condition = module.this.id != ""
error_message = "A 'name' or 'context' with a name must be provided."
}
}
}
# SSM parameters for layer integration
resource "aws_ssm_parameter" "layer_arn" {
count = var.enable_ssm_parameters ? 1 : 0
name = "/${module.this.id}/layer_arn"
type = "String"
value = aws_lambda_layer_version.this.arn
tags = module.this.tags
}
resource "aws_ssm_parameter" "layer_version" {
count = var.enable_ssm_parameters ? 1 : 0
name = "/${module.this.id}/layer_version"
type = "String"
value = aws_lambda_layer_version.this.version
tags = module.this.tags
}