我正在尝试将我的 boto3 代码(层创建)迁移到 Python 中的 CDK 代码,这是我的 boto3 代码,它按预期工作:
# Create the Lambda Function Layer
LAYERNAME = 'paramiko_Package'
if not aws_utils.checklayerExist(session,LAYERNAME):
boto_Lambda = session.client('lambda')
Layer = boto_Lambda.publish_layer_version(
LayerName=LAYERNAME,
Description='This contains the Python 3.8 libs for paramiko',
Content={
'S3Bucket': BUCKET_NAME,
'S3Key': 'aws_cdk/libs/packageParamiko.zip'
},
CompatibleRuntimes=['python3.8'],
LicenseInfo='none'
)
这很有魅力,但我需要通过 CDK 并基于此链接进行操作:AWS CDK DOCS
代码EX:
layer = lambda_.LayerVersion(stack, "MyLayer",
code=lambda_.Code.from_asset(path.join(__dirname, "layer-code")), <--- should be S3 resource
compatible_runtimes=[lambda_.Runtime.NODEJS_10_X],
license="Apache-2.0",
description="A layer to test the L2 construct"
)
问题是代码部分指向一个资产,我需要它像使用 boto3 一样指向 S3 存储桶。
我相信我需要找到一种方法来使用它,但我就是想不通:AWS CDK DOCS
尝试过但失败了:
resource = core.CfnResource(self, 'ResourceS3Bucket', type='AWS::S3::Bucket', properties=[S3Key='aws_cdk/libs/packageParamiko.zip'])
aws_lambda.Code.bind_to_resource(BUCKET_NAME,resource)
答案 0 :(得分:0)
经过一些调查,我设法像这样创建它:
iBucket = aws_s3.Bucket.from_bucket_arn(self,'s3bucketmonitor',bucket_arn='arn:aws:s3:::12111111-us-west-4-cdk-bucket')
aws_lambda.S3Code(iBucket,key='aws_cdk/libs/packageParamiko.zip')
layer = aws_lambda.LayerVersion(self, "MyLayer",
layer_version_name="MyLayer",
code=aws_lambda.S3Code(iBucket,key='aws_cdk/libs/packageParamiko.zip'),
compatible_runtimes=[aws_lambda.Runtime.PYTHON_3_8],
license="none",
description="A layer to test the L2 construct"
)
这将返回一个 iLayerVersion 对象,您可以直接在 lambda 代码上使用该对象。