AWS CFN Lambda 未将所有消息发送到 SQS

时间:2021-05-28 06:46:58

标签: aws-lambda amazon-cloudformation amazon-sqs

我是 AWS cloudformation 的新手,我的堆栈有问题。我有这些工作,但出了点问题。

假设我有一个 ArtifactBucket 堆栈,其中包含我的 .Net 核心 lambda 和 IAM 用户、角色和 KMS 密钥的 zip 文件。

我有这个主堆栈,它有一个 Creator lambda 将向 SQS Queue 发送消息。处理器功能将接收该消息并将其发送到 SNS,然后 SNS 向我发送电子邮件(请参阅订阅)

这一切都在 AWS 控制台中部署得很好,但我发现当我调用 lambda 时,我需要在收到消息之前多次调用它,并且一些消息丢失了。例如..我会一个接一个发送5条消息,只收到4条消息。

我怀疑它是 lambda 和队列的超时问题,但我正在努力查明它。

您是否发现我在此主模板中遗漏了任何明显的东西,或者队列属性 va lambda 时间值是否存在问题?

   "DelaySeconds" : 0,
   "MessageRetentionPeriod" : 900,
   "ReceiveMessageWaitTimeSeconds" : 5,
   "VisibilityTimeout" : 120,

堆栈模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Template to create IntergrationQueue, Intergrator and use storage bucket",
  "Parameters": {
    "ArtifactBucketName": {
      "Type": "String"
    },
    "StorageBucketArn": {
      "Type": "String"
    },
    "JogDayIAMUserArn": {
      "Type": "String"
    },
    "JogDayIAMRoleArn": {
      "Type": "String"
    },
    "JogDayKMSKeyArn": {
      "Type": "String"
    }
  },
  "Resources": {  
    "DLQ": {
      "Type": "AWS::SQS::Queue",
      "DeletionPolicy": "Delete",
      "Properties": {
        "KmsMasterKeyId": {
          "Ref": "JogDayKMSKeyArn"
        }
      }
    },
    "Queue": {
      "Type": "AWS::SQS::Queue",
      "DeletionPolicy": "Delete",
      "DependsOn": "DLQ",
      "Properties": {
        "QueueName": "JogDayQueue",
        "DelaySeconds" : 0,
        "MessageRetentionPeriod" : 900,
        "ReceiveMessageWaitTimeSeconds" : 5,
        "VisibilityTimeout" : 120,
        "RedrivePolicy": {        
          "deadLetterTargetArn": {
            "Fn::GetAtt": [
              "DLQ",
              "Arn"
            ]
          },
          "maxReceiveCount" : 10          
        },
        "KmsMasterKeyId": {
          "Ref": "JogDayKMSKeyArn"
        },
        "Tags": [
          {
            "Key": "jogday",
            "Value": "jogday"
          }
        ]
      }
    },
    "JogDayTopic": {
      "Type": "AWS::SNS::Topic",      
      "DeletionPolicy": "Delete",
      "Properties": {
        "DisplayName": "JogDay topic",
        "KmsMasterKeyId" : {"Ref":"JogDayKMSKeyArn"},        
        "TopicName" : "JogDayEmailTopic",      
        "Tags": [
          {
            "Key": "jogday",
            "Value": "jogday"
          }
        ]
      }
    },
    "JogDayQueuePolicy": {
      "Type": "AWS::SQS::QueuePolicy",
      "Description" : "Intergration Queue Policy",
      "DependsOn" : ["Queue","CreatorLambdaFunction"],
      "DeletionPolicy" : "Delete",      
      "Properties": {      
        "Queues": [{ "Ref":"Queue" }],        
        "PolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": "sns.amazonaws.com"
              },
              "Action": [
                "SQS:SendMessage",
                "SQS:ReceiveMessage"
              ],
              "Resource": {
                "Fn::GetAtt": [
                  "Queue",
                  "Arn"
                ]
              },
              "Condition": {
                "ArnEquals": {
                  "aws:SourceArn":{
                    "Fn::GetAtt": [
                      "CreatorLambdaFunction",
                      "Arn"
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    },
    "JogDaySubscription": {
      "Type": "AWS::SNS::Subscription",      
      "DeletionPolicy": "Delete",
      "Properties": {      
        "TopicArn": {
          "Ref": "JogDayTopic"
        },
        "Endpoint": "some.email@gmail.com",
        "Protocol": "email"
      },
      "DependsOn": [
        "JogDayTopic"
      ]
    },
    "CreatorLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "DeletionPolicy": "Delete",    
      "Properties": {
        "Runtime": "dotnetcore3.1",
        "FunctionName": "CreatorFunction",
        "Description": "Function that will take a message and send it to a SQS queue",
        "Code": {
          "S3Bucket": {
            "Ref": "ArtifactBucketName"
          },
          "S3Key": "CreatorFunction.zip"
        },
        "Handler": "CreatorFunction::CreatorFunction.Function::FunctionHandler",
        "Role": { "Ref": "JogDayIAMRoleArn" },
        "DeadLetterConfig": {
          "TargetArn": {
            "Fn::GetAtt": [
              "DLQ",
              "Arn"
            ]
          }
        },        
        "Tags": [
          {
            "Key": "jogday",
            "Value": "jogday"
          }
        ],
        "Timeout": 120,        
        "KmsKeyArn": {
          "Ref": "JogDayKMSKeyArn"
        },
        "Environment": {
          "Variables": {
            "QueueName": {
              "Fn::GetAtt": [
                "Queue",
                "QueueName"
              ]
            },
            "DLQName": {
              "Fn::GetAtt": [
                "DLQ",
                "QueueName"
              ]
            }
          }
        }
      }
    },
    "CreatorLambdaFunctionInvokePermission": {
      "Type": "AWS::Lambda::Permission",
      "DeletionPolicy": "Delete",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "CreatorLambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": {
          "Ref": "JogDayIAMUserArn"
        },
        "SourceAccount": {
          "Ref": "AWS::AccountId"
        }
      }
    },
    "ProcessorLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "DeletionPolicy": "Delete",    
      "DependsOn" : ["Queue","DLQ","JogDayTopic"],
      "Properties": {
        "Runtime": "dotnetcore3.1",
        "FunctionName": "ProcessorFunction",
        "Description": "Function that will receive a message from sqs and send it to sns",
        "Code": {
          "S3Bucket": {
            "Ref": "ArtifactBucketName"
          },
          "S3Key": "ProcessorFunction.zip"
        },
        "Handler": "ProcessorFunction::ProcessorFunction.Function::FunctionHandler",        
        "Role": { "Ref": "JogDayIAMRoleArn" },
        "DeadLetterConfig": {
          "TargetArn": {
            "Fn::GetAtt": [
              "DLQ",
              "Arn"
            ]
          }
        },        
        "Tags": [
          {
            "Key": "jogday",
            "Value": "jogday"
          }
        ],
        "Timeout": 120,
        "KmsKeyArn": {
          "Ref": "JogDayKMSKeyArn"
        },
        "Environment": {
          "Variables": {
            "QueueName": {
              "Fn::GetAtt": [
                "Queue",
                "QueueName"
              ]
            },
            "DLQName": {
              "Fn::GetAtt": [
                "DLQ",
                "QueueName"
              ]
            },
            "JogDayTopicArn": {"Ref" : "JogDayTopic" },         
            "JogDayTopicName": {
              "Fn::GetAtt": [
                "JogDayTopic",
                "TopicName"
              ]
            }       
          }
        }
      }
    },
    "ProcessorLambdaFunctionInvokePermission": {
      "Type": "AWS::Lambda::Permission",
      "DeletionPolicy": "Delete",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "ProcessorLambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": {
          "Ref": "JogDayIAMUserArn"
        },
        "SourceAccount": {
          "Ref": "AWS::AccountId"
        }
      }
    },
    "ProcessorLambdaEventSourceMapping": {
      "Type": "AWS::Lambda::EventSourceMapping",
      "DeletionPolicy": "Delete",
      "DependsOn": ["ProcessorLambdaFunction","Queue"],
      "Properties": {
       "BatchSize" : 10,
       "Enabled" : true,
       "EventSourceArn" : {
          "Fn::GetAtt": [
            "Queue",
            "Arn"
          ]
        },
        "FunctionName" : {
          "Fn::GetAtt": [
            "ProcessorLambdaFunction",
            "Arn"
          ]
        }     
      }
    }
  },
  "Outputs": {
    "QueueArn": {
      "Value": {
        "Fn::GetAtt": [
          "Queue",
          "Arn"
        ]
      }
    },
    "DLQArn": {
      "Value": {
        "Fn::GetAtt": [
          "DLQ",
          "Arn"
        ]
      }
    },
    "JogDayTopicName":{
     "Value": {
        "Fn::GetAtt": [
          "JogDayTopic",
          "TopicName"
        ]
      }
    }
  }
}

0 个答案:

没有答案
相关问题