如何将AWS CDK打包到Lambda层中? [英] How to package AWS CDK into Lambda layer?
问题描述
请问将AWS CDK捆绑为Lambda层的最佳方法是什么?
What is the best way to bundle up the AWS CDK as a Lambda layer please?
我需要从Java进程调用CDK,因此希望使用Java 11运行时,然后将Nodejs和CDK安装为2个单独的层.目前,我下载了Nodejs二进制文件,然后使用类似以下的内容:
I need to call the CDK from a Java process, so wish to use the Java 11 runtime and then install Nodejs and the CDK as 2 separate layers. Currently I download the Nodejs binaries and then use something like the following:
LayerVersion nodeLayer = LayerVersion.Builder.create(this, "node-layer")
.description("Layer containing Node.js")
.code(
Code.fromAsset(somePathToNodejs)
)
.build();
然后引用Lambda构造器/构建器中的层.
then refer to the layer within the Lambda constructor / builder.
CDK被安装为npm install -g aws-cdk
,因此我不知道如何最好地以类似的方式将其捆绑在一起.
The CDK is installed as npm install -g aws-cdk
, so I don't know how best to bundle this up in a similar fashion.
推荐答案
我最终使用Docker容器将aws-cdk
模块安装到特定路径中,然后压缩该容器并将其复制到绑定安装的目录中这样它就可以在基础主机上使用.
I ended up using a Docker container to install the aws-cdk
module into a specific path, then zip up the container and copying it to a bind-mounted directory so that it is available on the underlying host.
我使用Gradle和Ben Muschko的 gradle-docker-plugin
构建了该项目.我没有花时间尝试使用较小的Node映像来加快构建速度.
I built the project using Gradle and the gradle-docker-plugin
by Ben Muschko. I have not spent time trying to use a smaller Node image to speed up builds.
task createCdkDockerfile(type: Dockerfile) {
from 'node:latest'
defaultCommand('/bin/bash', '-c', "apt-get update && apt-get install -y zip && mkdir -p /nodejs && npm config set prefix /nodejs/bin && npm install -g aws-cdk && pushd /nodejs/bin && zip -r --symlinks /opt/aws-cdk.zip *")
}
task buildCdkImage(type:DockerBuildImage) {
dependsOn createCdkDockerfile
images.add('my-aws-cdk:latest')
}
ext.maybeConvertWindowsPath = { path ->
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
path = "/${path.replace("\\", "/").replace(":", "")}"
}
return path
}
task createCdkContainer(type: DockerCreateContainer) {
def dockerBindDir = new File(buildDir, "docker")
dockerBindDir.mkdirs()
dependsOn buildCdkImage
targetImageId buildCdkImage.getImageId()
hostConfig.autoRemove = true
hostConfig.binds = ["${maybeConvertWindowsPath(buildDir.toString())}/docker" : "/opt"]
}
task startCdkContainer(type: DockerStartContainer) {
dependsOn createCdkContainer
targetContainerId createCdkContainer.getContainerId()
}
task waitCdkContainer(type: DockerWaitContainer) {
dependsOn startCdkContainer
targetContainerId createCdkContainer.getContainerId()
}
该zip文件位于${buildDir}/docker/aws-cdk.zip
.
按照我的问题中的Nodejs示例,将CDK添加为一层,就像这样:
Adding the CDK as a layer is as per the Nodejs example in my question, so something like this:
LayerVersion nodeLayer = LayerVersion.Builder.create(this, "aws-cdk-layer")
.description("Layer containing AWS CDK")
.code(
Code.fromAsset(somePathTo-aws-cdk.zip)
)
.build();
需要注意的要点:
- 如果在Windows上构建,则在Windows上进行压缩,然后在Linux上解压缩,将出现应不再可执行的文件问题,即
cdk
本身. - 在Windows上,绑定安装目录似乎存在问题,因此需要清理文件名.
- 压缩时,使用
--symlinks
选项很重要,否则cdk
可执行文件将无法找到cdk.js
文件.
- if building on Windows, zipping up on Windows then unzipping on Linux will give issues with files that should be executable no longer being executable, i.e.
cdk
itself. - bind mounting directories seems to have issues when on Windows, hence the need to sanitize the filename.
- when zipping, it's important to use the
--symlinks
option, otherwise thecdk
executable will not be able to find thecdk.js
file.
我本来希望使用tar
压缩文件,但fromAsset()
不会接受tar文件.
I would have preferred using tar
to zip the file up, but fromAsset()
won't accept tar files.
这篇关于如何将AWS CDK打包到Lambda层中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!