Aws lambda Lambda与cloud formation:将cloud formation变量传递给python代码
下面是clouformation,我们有lambda函数s3到s3副本的python代码段。这里无法将目标bucket作为target_bucket=Ref:dstBucket从云形成参数传递到python代码段 我现在正在硬编码target_bucket=destination1,但我想作为Ref值传递 我尝试过不同的方法,但没有成功Aws lambda Lambda与cloud formation:将cloud formation变量传递给python代码,aws-lambda,amazon-cloudformation,Aws Lambda,Amazon Cloudformation,下面是clouformation,我们有lambda函数s3到s3副本的python代码段。这里无法将目标bucket作为target_bucket=Ref:dstBucket从云形成参数传递到python代码段 我现在正在硬编码target_bucket=destination1,但我想作为Ref值传递 我尝试过不同的方法,但没有成功 --- AWSTemplateFormatVersion: '2010-09-0
---
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.com
- s3.amazonaws.com
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.com
SourceAccount:
Ref: AWS::AccountId
SourceArn:
Fn::Sub: arn:aws:s3:::${srcBucket}
下面是clouformation,我们有lambda函数s3到s3副本的python代码段。这里无法将目标bucket作为target_bucket=Ref:dstBucket从云形成参数传递到python代码段
我现在正在硬编码target_bucket=destination1,但我想作为Ref值传递
我尝试过不同的方法,但没有成功 您可以在lambda上使用该属性,就像
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
...
Environment:
Variables:
DESTINATION_BUCKET: !Ref dstBucket
然后可以更新代码以读取环境变量
target_bucket = os.environ['DESTINATION_BUCKET']
或者,如果您真的想使用参考,您可以使用!子函数,如
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
Code:
ZipFile: !Sub |
...
target_bucket = "${dstBucket}"
...
你可以在你的lambda上使用这个属性
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
...
Environment:
Variables:
DESTINATION_BUCKET: !Ref dstBucket
然后可以更新代码以读取环境变量
target_bucket = os.environ['DESTINATION_BUCKET']
或者,如果您真的想使用参考,您可以使用!子函数,如
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
Code:
ZipFile: !Sub |
...
target_bucket = "${dstBucket}"
...
如果你需要更多的上下文
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
...
Environment:
...
如果你需要更多的上下文
LambdaFunction:
Type: AWS::Lambda::Function
DependsOn: LambdaRole
Properties:
...
Environment:
...
添加了以下代码,但在CFT中收到错误,因为遇到不支持的属性DESTINATION_BUCKET LambdaFunction:Type:AWS::Lambda::Function DependsOn:LambdaRole属性:环境:DESTINATION_BUCKET:!非常感谢你,遗漏了很多变量。更新添加了以下代码,但在CFT中收到错误,因为遇到不支持的属性DESTINATION_BUCKET LambdFunction:Type:AWS::Lambda::Function Dependedson:LambdaRole属性:环境:DESTINATION_BUCKET:!非常感谢你,遗漏了很多变量。Updated只在该环境中查找一个用于prod或dev的变量。我想添加3个os.environ,它们是目标bucket,key.LambdaFunctionType:AWS::Lambda::Function DependsOn:LambdaRole属性:环境:目标_bucket:!CFT中的Ref dstBucketerror遇到不受支持的属性目标\u BUCKETLambdaFunction@user9075162来自手册:类型:键值对的映射。注意复数形式。这是一个列表。对不起,谢谢您在这个环境中只查找了一个变量,用于prod或dev。我想添加3个os.environ,它们是目标bucket,key.LambdaFunctionType:AWS::Lambda::Function DependsOn:LambdaRole属性:环境:destination\u bucket:!CFT中的Ref dstBucketerror遇到不受支持的属性目标\u BUCKETLambdaFunction@user9075162来自手册:类型:键值对的映射。注意复数形式。这是一张单子。对不起,谢谢