Reputation: 554
I'm a little stuck trying to combine two code functions that don't seem to be working.
the aws cloudformation
cli tool seems to be having problems and throws an error: 'list' object has no attribute 'get'
.
I'm trying to specify a lambda function (Type AWS::Serverless::Function), which otherwise works, but I want to specify the Event Schedule object as lower case (required for database).
Code in question, the last 8 lines are the issue:
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
I get an error:
'list' object has no attribute 'get'
Simplified code:
Input: !Sub |
{"database": "${AWS::StackName}_logs",
"tableNames": "${PartitionedTables}"}
If I simplify the code, I get no error, but I need the lowercase for my usecase. Can anyone help? I'm kinda stuck.
Upvotes: 2
Views: 1865
Reputation: 554
I managed to solve this, thanks to help from @Marcin Unfortunately, I got into a rabbit hole of other deployment issues, so I wanted to post my final findings as the answer to this question. I hope this might help someone else.
In my original question, there was indeed a -
that was causing issues, needed to remove this and resolved the first error.
Working code below: (Don't forget to similarly transform any references to DB within same template and others, the same Macro Transform code can be used)
Input:
'Fn::Transform':
Name: 'StringTransform'
Parameters:
InputString: !Sub |
{"database": "${AWS::StackName}_logs",
"tableNames": "${PartitionedTables}"}
Operation: Lower
Code of CloudFormation Macro Lambda required to make this work:
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
Other accompanying resources for Macro:
# 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
Thanks again @Marcin
Upvotes: 1