gpt4 book ai didi

amazon-web-services - AWS : how to use transform cloudformation function in YAML database name

转载 作者:行者123 更新时间:2023-12-03 07:15:31 24 4
gpt4 key购买 nike

我在尝试组合两个似乎不起作用的代码函数时遇到了一点困难。aws cloudformation cli 工具似乎存在问题并引发错误:'list' 对象没有属性 'get'

我正在尝试指定一个 lambda 函数(类型 AWS::Serverless::Function),否则该函数可以工作,但我想将事件计划对象指定为小写(数据库需要)。

有问题的代码,最后 8 行是问题所在:

Resources:
UpdatePartitionsLogTableFunction:
Type: AWS::Serverless::Function
Properties:
Handler: updatePartitionsTable.handler
Runtime: nodejs12.x
CodeUri: src
Description: Updates partitions Athena tables
MemorySize: 128
Timeout: 60
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "athena:*"
Resource:
- "*"
- Effect: Allow
Action:
- "glue:*"
Resource:
- "*"
- Effect: Allow
Action:
- "s3:*"
Resource:
- "*"
Events:
Timer:
Type: Schedule
Properties:
Schedule: cron(0 * * * ? *)
Input:
'Fn::Transform':
- Name: 'String'
Parameters:
InputString: !Sub |
{"database": "${AWS::StackName}_logs",
"tableNames": "${PartitionedTables}"}
Operation: Lower

我收到错误:

'list' object has no attribute 'get'

简化代码:

            Input: !Sub |
{"database": "${AWS::StackName}_logs",
"tableNames": "${PartitionedTables}"}

如果我简化代码,我不会收到错误,但我的用例需要小写。有人可以帮忙吗?我有点卡住了。

最佳答案

感谢@Marcin 的帮助,我成功解决了这个问题不幸的是,我陷入了其他部署问题的困境,所以我想发布我的最终发现作为这个问题的答案。我希望这对其他人有帮助。

在我原来的问题中,确实有一个 - 导致了问题,需要删除它并解决第一个错误。

工作代码如下:(不要忘记在同一模板和其他模板中类似地转换对数据库的任何引用,可以使用相同的宏转换代码)

            Input:
'Fn::Transform':
Name: 'StringTransform'
Parameters:
InputString: !Sub |
{"database": "${AWS::StackName}_logs",
"tableNames": "${PartitionedTables}"}
Operation: Lower

完成此工作所需的 CloudFormation 宏 Lambda 代码:

Resources:
# Custom Transform Macro Function
# Function is called to manipulate (transform) strings
TransformFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
import traceback


def handler(event, context):
response = {
"requestId": event["requestId"],
"status": "success"
}
try:
operation = event["params"]["Operation"]
input = event["params"]["InputString"]
no_param_string_funcs = ["Upper", "Lower", "Capitalize", "Title", "SwapCase"]
if operation in no_param_string_funcs:
response["fragment"] = getattr(input, operation.lower())()
elif operation == "Strip":
chars = None
if "Chars" in event["params"]:
chars = event["params"]["Chars"]
response["fragment"] = input.strip(chars)
elif operation == "Replace":
old = event["params"]["Old"]
new = event["params"]["New"]
response["fragment"] = input.replace(old, new)
elif operation == "MaxLength":
length = int(event["params"]["Length"])
if len(input) <= length:
response["fragment"] = input
elif "StripFrom" in event["params"]:
if event["params"]["StripFrom"] == "Left":
response["fragment"] = input[len(input)-length:]
elif event["params"]["StripFrom"] != "Right":
response["status"] = "failure"
else:
response["fragment"] = input[:length]
else:
response["status"] = "failure"
except Exception as e:
traceback.print_exc()
response["status"] = "failure"
response["errorMessage"] = str(e)
return response

Handler: index.handler
Runtime: python3.7
Role: !GetAtt TransformExecutionRole.Arn
FunctionName: !Sub ${AWS::StackName}-StringTransform

宏的其他附带资源:

  # Role for TransformFunction
TransformExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: ['logs:*']
Resource: 'arn:aws:logs:*:*:*'
# Permission for TransformFunction
TransformFunctionPermissions:
Type: AWS::Lambda::Permission
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt TransformFunction.Arn
Principal: 'cloudformation.amazonaws.com'
# Actual CloudFormationMacro entrypoint for TransformFunction
Transform:
Type: AWS::CloudFormation::Macro
Properties:
Name: 'StringTransform'
Description: Provides various string processing functions
FunctionName: !GetAtt TransformFunction.Arn

再次感谢@Marcin

关于amazon-web-services - AWS : how to use transform cloudformation function in YAML database name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63602850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com