【AWS SQS】

本文档详细介绍了如何在AWS中创建一个SQS队列,并配置该队列以触发Lambda函数。当有消息进入SQS时,Lambda会接收到消息并进行处理。步骤包括创建SQS队列,设置允许Lambda读写的消息权限,创建Lambda函数,以及配置Lambda触发器。最后,通过发送消息到SQS来测试整个流程,并在CloudWatch中检查Lambda的运行情况。
摘要由CSDN通过智能技术生成

Create a SQS in AWS and trigger a Lamdba function(AWS 中创建SQS,配置Lambda Trigger)

Follow the steps to create AWS SQS and trigger a Lambda function.

Purpose

To create a AWS SQS, then configure the queue to trigger a Lambda function.
And when you send a message in SQS, Lambda will recieve that message and print.

Steps

1. Create a queue and configure that queue.
Choose simple queue service in AWS, then click create queue.
​​​Typing the queue name. Typing the queue name
Change the access policy, let the lambda function have access to receive and send message from this queue, here is my needed policy ( Also, you can user the policy generator to generate policy that you needed). And other configuration can be default unless you need to change it. Then click create the queue button.
{
  "Version": "2012-10-17",
  "Id": "Policy1669600519451",
  "Statement": [
    {
      "Sid": "Stmt1669600358052",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "sqs:DeleteMessage",
        "sqs:GetQueueAttributes",
        "sqs:ReceiveMessage"
      ],
      "Resource": "arn:aws:sqs:ap-northeast-1:160071257600:firstQueue.fifo"
    },
    {
      "Sid": "Stmt1669600517533",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::160071257600:root"
      },
      "Action": "sqs:*",
      "Resource": "arn:aws:sqs:ap-northeast-1:160071257600:firstQueue.fifo"
    }
  ]
}
2. Create a lambda function
Choose Lambda service in AWS. Click create function. And Typing the function name.
Choose a runtime Language, Node.js is used here. And the other settings can be default. Then click the create function button. That’s done.
3. Config lambda function trigger
choose the queue that you have created in step 1, click Lambda triggers, and then configure lambda function trigger.
configure
Next, you need choose a lambda function that you wanted to triggered and save.
That’s about the setting up.
4. Send message to trigger lambda
In your created queue, there is a button called send and receive message, click it and enter message that you want to send, then give it a fake message group ID and Message deduplication ID. That’s the way to seed message and trigger the lambda function.
在这里插入图片描述
5. Check the lambda function if it has been triggered
you can check your lambda function monitor in cloudWatch. By click the specific log stream, you can see the details of that event.
在这里插入图片描述
Also, you can change the display contents of this log event in lambda code source. And remember to deploy the changes
在这里插入图片描述

That’s all about how to create a SQS and configure a lambda trigger to receive the message that SQS send.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AWS SQS(Amazon Simple Queue Service)是亚马逊提供的一项分布式消息队列服务,用于在应用程序之间进行可靠、可伸缩的消息传递。Python是一种流行的编程语言,有丰富的库和框架支持。 使用Python操作AWS SQS可以通过Boto3库来实现。Boto3是亚马逊提供的AWS SDK for Python,可以使开发者更方便地与AWS服务进行交互。 首先,我们需要安装Boto3库,可以通过pip命令进行安装。 ``` $ pip install boto3 ``` 接下来,我们可以创建一个SQS客户端,通过提供访问密钥和密码等安全凭证来连接到AWS。 ```python import boto3 # 创建SQS客户端 sqs = boto3.client('sqs',region_name='us-west-2', aws_access_key_id='your_access_key', aws_secret_access_key='your_secret_key') ``` 现在,我们可以使用SQS客户端来发送消息到队列中。 ```python # 发送消息 response = sqs.send_message( QueueUrl='your_queue_url', MessageBody='Hello, AWS SQS' ) # 打印响应中的消息ID print(response['MessageId']) ``` 另外,我们也可以从队列中接收消息。 ```python # 接收消息 response = sqs.receive_message( QueueUrl='your_queue_url', MaxNumberOfMessages=1, VisibilityTimeout=10, WaitTimeSeconds=0 ) # 获取消息内容 message = response['Messages'][0] print(message['Body']) # 删除已接收的消息 sqs.delete_message( QueueUrl='your_queue_url', ReceiptHandle=message['ReceiptHandle'] ) ``` 以上是使用Python操作AWS SQS的简单示例。通过Boto3库,我们可以轻松地使用Python发送和接收消息,实现分布式应用程序之间的可靠消息传递。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值