Json 操作==“最大长度”: 长度=int(事件[“参数”][“长度”]) 如果len(input)
Young先生是正确的,那么这就是调用宏所需的语法 但是,他们和文档都没有提到的一个关键因素是,为了调用转换宏,您需要在使用自述文件中列出的函数之前将此堆栈部署到您的帐户中Json 操作==“最大长度”: 长度=int(事件[“参数”][“长度”]) 如果len(input),json,amazon-web-services,amazon-s3,amazon-cloudformation,aws-cloudformation-custom-resource,Json,Amazon Web Services,Amazon S3,Amazon Cloudformation,Aws Cloudformation Custom Resource,Young先生是正确的,那么这就是调用宏所需的语法 但是,他们和文档都没有提到的一个关键因素是,为了调用转换宏,您需要在使用自述文件中列出的函数之前将此堆栈部署到您的帐户中 我认为这些文档可以在这方面得到澄清,我会看看是否可以进行澄清接受的答案建议使用CloudFormation宏,另一个答案建议使用FindInMap FindInMap在这里不是很有用,因为它只适用于硬编码的值 宏建议会起作用,但需要相当多的设置(在单独的堆栈中声明宏,确保您的部署者角色有权调用Lambda,并且您的Clou
我认为这些文档可以在这方面得到澄清,我会看看是否可以进行澄清接受的答案建议使用CloudFormation宏,另一个答案建议使用
FindInMap
FindInMap
在这里不是很有用,因为它只适用于硬编码的值
宏建议会起作用,但需要相当多的设置(在单独的堆栈中声明宏,确保您的部署者角色有权调用Lambda,并且您的CloudFormation堆栈部署有功能\u AUTO\u EXPAND
,等等)
在模板中声明自定义资源将起作用,与依赖宏相比,IMO所涉及的工作更少。下面是一个CFN片段,对您询问的S3 bucket资源进行了调整,演示了自定义资源的使用,该资源将任意S3 bucket名称小写:
# Custom resource to transform input to lowercase.
LowerCaseLambda:
Type: 'AWS::Lambda::Function'
Properties:
Description: Returns the lowercase version of a string
MemorySize: 256
Runtime: python3.8
Handler: index.lambda_handler
Role: !GetAtt LowerCaseLambdaRole.Arn
Timeout: 30
Code:
ZipFile: |
import cfnresponse
def lambda_handler(event, context):
output = event['ResourceProperties'].get('InputString', '').lower()
responseData = {'OutputString': output}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
LowerCaseLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "lambda-write-logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*"
S3BucketName:
Type: Custom::Lowercase
Properties:
ServiceToken: !GetAtt LowerCaseLambda.Arn
InputString: !Ref selectedEnv
S3Bucket:
BucketName: !Join
- ''
- - !GetAtt S3BucketName.OutputString
- "-deployment.companyname.com"
JSON无效,您可以共享完整的cloudformatino模板吗?我已经更新了问题和JSON。请帮帮我。ThanksIt从您的问题中不清楚是否有lambda函数实现aws创建自定义转换所需的
MyString
宏。可以找到更多信息。示例(包括lambda函数)可以找到。您可以简化用户输入的大小写,即DEV=DEV。您不能仅使用json/yaml进行此转换。虽然你可以用json/yaml做的只是将DEV映射到DEV,PROD映射到PROD,等等
合并到你的模板中。我也这样做过,但我真的希望他们能证明我们可以使用这个函数进行这些简单的操作。在发送到CloudFormation之前,我一直在Jenkins Shell中进行这些类型的操作。Shell示例:UpperVar=DEV,LowerVar=${UpperVar,,}从发布的自述文件中不清楚,但是您需要将该Python文件添加为转换函数,而不仅仅是使用upper函数。CF对此没有任何内置支持,您需要添加自己的转换函数来实现。@shanet这是一个cloudformation宏,您将作为lambda部署。更多关于CloudFormation宏的信息可以在这里找到。是的,这就是我要说的。上面的答案和github链接中的自述文件看起来像是一种语法,当您实际需要将链接示例中的Python代码添加为宏/lambda/what have you以使用它时,CF新手可以使用这种语法。我不确定您是如何得出这一结论的。文档中明确指出“使用宏处理模板有两个主要步骤:创建宏本身,然后使用宏对模板执行处理。”。。。要创建宏定义,您需要创建以下内容:“。。。“用于执行模板处理的AWS Lambda函数。”。。。“类型为AWS::CloudFormation::Macro
的资源,允许用户从AWS CloudFormation模板中调用Lambda函数。”哪些文档?您的答案或链接到GitHub repo的内容中没有任何内容表明“使用宏处理模板有两个主要步骤……”。对于刚接触CloudFormation的人,我认为“宏”是内置的,我可以在模板中使用它。事实并非如此,我需要先自己定义宏。
Parameters:
InputString:
Default: "This is a test input string"
Type: String
Resources:
S3Bucket:
Type: "AWS::S3::Bucket"
Properties:
Tags:
- Key: Upper
Value:
'Fn::Transform':
- Name: 'String'
Parameters:
InputString: !Ref InputString
Operation: Upper
# Custom resource to transform input to lowercase.
LowerCaseLambda:
Type: 'AWS::Lambda::Function'
Properties:
Description: Returns the lowercase version of a string
MemorySize: 256
Runtime: python3.8
Handler: index.lambda_handler
Role: !GetAtt LowerCaseLambdaRole.Arn
Timeout: 30
Code:
ZipFile: |
import cfnresponse
def lambda_handler(event, context):
output = event['ResourceProperties'].get('InputString', '').lower()
responseData = {'OutputString': output}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
LowerCaseLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "lambda-write-logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "arn:aws:logs:*:*"
S3BucketName:
Type: Custom::Lowercase
Properties:
ServiceToken: !GetAtt LowerCaseLambda.Arn
InputString: !Ref selectedEnv
S3Bucket:
BucketName: !Join
- ''
- - !GetAtt S3BucketName.OutputString
- "-deployment.companyname.com"