具有云形成的Lambda:将Cloud形成变量传递给python代码

编程入门 行业动态 更新时间:2024-10-26 02:30:51
本文介绍了具有云形成的Lambda:将Cloud形成变量传递给python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是clouformation,代码中有我们将lambda函数s3复制到s3的python代码段。这里无法将目标存储桶从云形成参数传递给python代码段,如target_bucket = Ref:dstBucket

Hi below is the clouformation ,code we have python snippet for lambda function s3 to s3 copy.Here could not able to pass the destination bucket from cloud formation parameters to python snippet as target_bucket = Ref: dstBucket

我现在正在硬编码target_bucket = destination1,但我想通过Ref值

I am hardcoding now target_bucket = destination1 but i want to pass as Ref value

我尝试了不同的方法但未成功。

I have tried different ways did not succeed.

--- AWSTemplateFormatVersion: '2010-09-09' Description: 'objects from Prod bucket to Dev data bucket ' Parameters: CustomerName: Description: Customer Name Type: String Default: incoming ProjectName: Description: Project Name Type: String Default: TEST ENV: Description: Environment (dev, prd) Type: String Default: dev srcBucket: Description: Source Bucket that receives data from outside Default: source1 Type: String dstBucket: Description: Destination Bucket that will receive files Type: String Default: destination1 Resources: LambdaRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws - s3.amazonaws Action: - sts:AssumeRole Path: Fn::Sub: "/${ProjectName}/" Policies: - PolicyName: Fn::Sub: "${AWS::StackName}" PolicyDocument: Version: '2012-10-17' Statement: - Sid: AllowLogging Effect: Allow Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:PutLogEvents Resource: "*" - Sid: SrcBucketPrivs Action: - s3:GetObject - s3:List* Resource: - Fn::Sub: arn:aws:s3:::${srcBucket}/* - Fn::Sub: arn:aws:s3:::${srcBucket} Effect: Allow - Sid: DstBucketPrivs Action: - s3:PutObject - s3:List* Resource: - Fn::Sub: arn:aws:s3:::${dstBucket}/* - Fn::Sub: arn:aws:s3:::${dstBucket} Effect: Allow LambdaFunction: Type: AWS::Lambda::Function DependsOn: LambdaRole Properties: Code: ZipFile: | from __future__ import print_function import os import json import boto3 import time import string import urllib print('Loading function') s3 = boto3.client('s3') def handler(event, context): source_bucket = event['Records'][0]['s3']['bucket']['name'] key = event['Records'][0]['s3']['object']['key'] target_bucket = Ref: dstBucket copy_source = {'Bucket':source_bucket, 'Key':key} try: s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source) except Exception as e: print(e) print('Error getting object {} from bucket {}. Make sure they exist ' 'and your bucket is in the same region as this ' 'function.'.format(key, source_bucket)) raise e Description: Copies objects from srcBucket to dstBucket based on S3 Event Trigger FunctionName: Fn::Sub: "${AWS::StackName}" Handler: index.handler MemorySize: 128 Role: Fn::GetAtt: - LambdaRole - Arn Runtime: python3.6 Timeout: 60 LambdaInvokePermission: Type: AWS::Lambda::Permission DependsOn: LambdaFunction Properties: FunctionName: Fn::GetAtt: - LambdaFunction - Arn Action: lambda:InvokeFunction Principal: s3.amazonaws SourceAccount: Ref: AWS::AccountId SourceArn: Fn::Sub: arn:aws:s3:::${srcBucket}

下面是clouformation,代码中有我们将lambda函数s3复制到s3的python代码段。在这里无法通过从云形成参数到python代码段的目标存储区为target_bucket = Ref:dstBucket

Hi below is the clouformation ,code we have python snippet for lambda function s3 to s3 copy.Here could not able to pass the destination bucket from cloud formation parameters to python snippet as target_bucket = Ref: dstBucket

我现在对target_bucket = destination1进行硬编码,但我想作为Ref值传递

I am hardcoding now target_bucket = destination1 but i want to pass as Ref value

我尝试了不同的方法但未成功。

I have tried different ways did not succeed.

推荐答案

您可以使用您的lambda上的环境属性,例如

LambdaFunction: Type: AWS::Lambda::Function DependsOn: LambdaRole Properties: ... Environment: Variables: DESTINATION_BUCKET: !Ref dstBucket

然后您可以更新您的代码以读取环境变量

Then you can update your code to read the environment variable

target_bucket = os.environ['DESTINATION_BUCKET']

或者,如果您确实要使用引用,则可以使用!Sub 函数,例如

Alternatively, if you really want to use the reference, you can use the !Sub function, like

LambdaFunction: Type: AWS::Lambda::Function DependsOn: LambdaRole Properties: Code: ZipFile: !Sub | ... target_bucket = "${dstBucket}" ...

更多推荐

具有云形成的Lambda:将Cloud形成变量传递给python代码

本文发布于:2023-10-12 01:31:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   代码   Lambda   Cloud   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!