Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 如何在AWS CDK(Python)中使用S3 bucket资源创建层_Amazon S3_Aws Lambda_Aws Cdk - Fatal编程技术网

Amazon s3 如何在AWS CDK(Python)中使用S3 bucket资源创建层

Amazon s3 如何在AWS CDK(Python)中使用S3 bucket资源创建层,amazon-s3,aws-lambda,aws-cdk,Amazon S3,Aws Lambda,Aws Cdk,我正在尝试将我的boto3代码(层创建)迁移到python中的CDK代码,这是我的boto3代码,其工作方式与Expected相同: # Create the Lambda Function Layer LAYERNAME = 'paramiko_Package' if not aws_utils.checklayerExist(session,LAYERNAME): boto_Lambda = session.client('lambda') Layer = boto_Lamb

我正在尝试将我的boto3代码(层创建)迁移到python中的CDK代码,这是我的boto3代码,其工作方式与Expected相同:

# 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并基于此链接:

代码示例:

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"
)

经过一些调查后,我成功地创建了如下:

   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代码上使用它

   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"
                            )