AWSTemplateFormatVersion: 2010-09-09 Resources: 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:*:*:*' 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: traceback.print_exc() response["status"] = "failure" macro_response["errorMessage"] = str(e) return response Handler: index.handler Runtime: python3.6 Role: !GetAtt TransformExecutionRole.Arn TransformFunctionPermissions: Type: AWS::Lambda::Permission Properties: Action: 'lambda:InvokeFunction' FunctionName: !GetAtt TransformFunction.Arn Principal: 'cloudformation.amazonaws.com' Transform: Type: AWS::CloudFormation::Macro Properties: Name: 'String' Description: Provides various string processing functions FunctionName: !GetAtt TransformFunction.Arn