首页 > 解决方案 > AWS StepFunctions...模板错误:Fn::Sub 的实例引用了无效的资源属性

问题描述

我在 StepFunctions 状态机中使用 AWS Lambda。

下面的模板运行良好,直到我不得不更改 LogToDatabase 函数的名称。

现在 VS2019 中的 Publish to AWS 向导给出了这个错误:

未能创建 CloudFormation 更改集:模板错误:Fn::Sub 的实例引用了无效的资源属性 LogToDatabase.Arn

有任何想法吗?

这是我的 StepFunctions 状态机:

{
  "Comment": "Billing Portal Email - State Machine",
  "StartAt": "SendEmail",
  "States": {
    "SendEmail": {
      "Type": "Task",
      "Resource": "${SendEmail.Arn}",
      "Next": "WaitToActivate"
    },
    "WaitToActivate": {
      "Type": "Wait",
      "SecondsPath": "$.WaitInSeconds",
      "Next": "LogToDatabase"
    },
    "LogToDatabase": {
      "Type": "Task",
      "Resource": "${LogToDatabase.Arn}",
      "End": true
    }
  }
}

CloudFormation 模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::Serverless-2016-10-31",
    "Description": "APC Serverless Application for Billing Portal",
    "Resources": {
        "SendEmailTask": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "APC.AWS.SAM::APC.AWS.SAM.StepFunctionTasks::SendEmail",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaRole",
                        "Arn"
                    ]
                },
                "Runtime": "dotnetcore3.1",
                "MemorySize": 128,
                "Timeout": 60,
                "Code": {
                    "S3Bucket": "",
                    "S3Key": ""
                }
            }
        },
        "LogToDatabaseTask": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "APC.AWS.SAM::APC.AWS.SAM.StepFunctionTasks::LogToDatabase",
                "Role": {
                    "Fn::GetAtt": [
                        "LambdaRole",
                        "Arn"
                    ]
                },
                "Runtime": "dotnetcore3.1",
                "MemorySize": 128,
                "Timeout": 30,
                "Code": {
                    "S3Bucket": "",
                    "S3Key": ""
                }
            }
        },
        "StateMachine": {
            "Type": "AWS::StepFunctions::StateMachine",
            "Properties": {
                "RoleArn": {
                    "Fn::GetAtt": [
                        "StateMachineRole",
                        "Arn"
                    ]
                },
                "DefinitionString": {
                    "Fn::Sub": ""
                }
            }
        },
        "LambdaRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Action": [
                                "sts:AssumeRole"
                            ],
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            }
                        }
                    ]
                },
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
                ]
            }
        },
        "StateMachineRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": {
                                    "Fn::Sub": "states.${AWS::Region}.amazonaws.com"
                                }
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "Policies": [
                    {
                        "PolicyName": "StepFunctionLambdaInvoke",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "lambda:InvokeFunction"
                                    ],
                                    "Resource": "*"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    },
    "Outputs": {},
    "Metadata": {
        "AWS::CloudFormation::Designer": {}
    }
}

最后,我的 C# lambda 函数签名...

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace APC.AWS.SAM
{
    public partial class StepFunctionTasks
    {
        public StepFunctionTasks() { }


        public State SendEmail(State state, ILambdaContext context)
        {
            //code here
            return state;
        }

        public State LogToDatabase(State state, ILambdaContext context)
        {
            //more code here
            return state;
        }
    }
}

标签: c#amazon-web-servicesaws-lambda

解决方案


您只需要更改${LogToDatabase.Arn}${LogToDatabaseTask.Arn}. lambda 函数的逻辑 ID 是LogToDatabaseTask.


推荐阅读