-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-layer.ps1
More file actions
125 lines (102 loc) · 4.92 KB
/
create-layer.ps1
File metadata and controls
125 lines (102 loc) · 4.92 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
.SYNOPSIS
Script to create Lambda layers with Docker or Podman.
.DESCRIPTION
This script creates Lambda layers for AWS using Docker or Podman. It allows to
install specified packages directly or from a requirements file (requirements.txt or package.json).
.PARAMETER layername
The name of the layer to create.
.PARAMETER runtime
The AWS Lambda runtime to use (e.g. python3.12, nodejs14.x).
- PYTHON: “python3.6”,“python3.7”,“python3.8”,“python3.9”,“python3.10”,“python3.11”,"python3.12”
- NODEJS: “nodejs10.x”,“nodejs12.x”,“nodejs14.x”,“nodejs16.x”,“nodejs18.x”,"nodejs20.x”
.PARAMETER packages
List of packages to install (only if requirementsFile is not used).
.PARAMETER requirementsFile
Path to the requirements file (requirements.txt for Python, package.json for Node.js).
.PARAMETER zipOnly
If specified, only creates the ZIP file without uploading it to AWS.
.PARAMETER containerEngine
Engine container to use: “docker” (default) or “podman”.
.EXAMPLE
.\create-layer.ps1 -layername “mi-layer-python” -runtime “python3.12” -packages “fastapi”, “mangum” -zipOnly
.EXAMPLE
.\create-layer.ps1 -layername “mi-layer-node” -runtime “nodejs14.x” -requirementsFile “path/to/package.json” -containerEngine podman
.NOTES
Make sure you have Docker or Podman installed and configured, as well as the AWS CLI if you want to upload the layer to AWS.
#>
param(
[Parameter(Mandatory=$true)][string]$layername,
[Parameter(Mandatory=$true)][string]$runtime,
[Parameter(ParameterSetName="Packages", Mandatory=$true)][string[]]$packages,
[Parameter(ParameterSetName="RequirementsFile")][string]$requirementsFile,
[switch]$zipOnly,
[ValidateSet("docker", "podman")][string]$containerEngine = "podman"
)
$ErrorActionPreference = "Stop"
Write-Host "================================="
Write-Host "Layer name: $layername"
Write-Host "Runtime: $runtime"
if ($PSCmdlet.ParameterSetName -eq "Packages") {
Write-Host "Packages: $($packages -join ' ')"
} else {
Write-Host "requirements file: $requirementsFile"
}
if ($zipOnly) {
Write-Host "Only Zip file, not AWS uploading"
}
else {
Write-Host "Uploading layer to AWS"
}
Write-Host "Container engine: $containerEngine"
Write-Host "================================="
$host_temp_dir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_.FullName }
$support_python_runtime = @("python3.6","python3.7","python3.8","python3.9","python3.10","python3.11","python3.12","python3.13")
$support_node_runtime = @("nodejs10.x","nodejs12.x","nodejs14.x","nodejs16.x","nodejs18.x","nodejs20.x")
if ($support_node_runtime -contains $runtime) {
$installation_path = "nodejs"
$container_image = "public.ecr.aws/sam/build-$runtime`:latest"
Write-Host "- Preparing layer..."
if ($PSCmdlet.ParameterSetName -eq "Packages") {
$packages_string = $packages -join ' '
$install_command = "npm install --prefix $installation_path --save $packages_string"
} else {
Copy-Item $requirementsFile -Destination "$host_temp_dir\package.json"
$install_command = "npm install --prefix $installation_path"
}
& $containerEngine run --rm -v "${host_temp_dir}:/lambda-layer:Z" -w "/lambda-layer" $container_image /bin/bash -c "mkdir $installation_path && $install_command && zip -r lambda-layer.zip *"
}
elseif ($support_python_runtime -contains $runtime) {
$installation_path = "python"
$container_image = "public.ecr.aws/sam/build-$runtime`:latest"
Write-Host "- Preparing layer..."
if ($PSCmdlet.ParameterSetName -eq "Packages") {
$packages_string = $packages -join ' '
$install_command = "pip install $packages_string -t $installation_path"
} else {
Copy-Item $requirementsFile -Destination "$host_temp_dir\requirements.txt"
$install_command = "pip install -r requirements.txt -t $installation_path"
}
& $containerEngine run --rm -v "${host_temp_dir}:/lambda-layer:Z" -w "/lambda-layer" $container_image /bin/bash -c "mkdir $installation_path && $install_command && zip -r lambda-layer.zip * -x '*/__pycache__/*'"
}
else {
Write-Host "- X Invalid runtime"
exit 1
}
if ($zipOnly) {
if ($support_node_runtime -contains $runtime) {
$destination = Join-Path -Path $PWD -ChildPath "${layername}_${runtime}_lambda_layer.zip"
}
elseif ($support_python_runtime -contains $runtime) {
$destination = Join-Path -Path $PWD -ChildPath "${layername}_${runtime}_lambda_layer.zip"
}
Copy-Item -Path "${host_temp_dir}\lambda-layer.zip" -Destination $destination
Write-Host "- Layer zip file created at: $destination"
}
else {
Write-Host "- Uploading layer to AWS"
aws lambda publish-layer-version --layer-name $layername --compatible-runtimes $runtime --zip-file "fileb://${host_temp_dir}/lambda-layer.zip"
}
Write-Host "- Finishing"
Remove-Item -Recurse -Force $host_temp_dir
Write-Host "- Ready!"