使用CodeWhisperer开发Python无服务器应用

https://aws.amazon.com/blogs/devops/develop-a-serverless-application-in-python-using-amazon-codewhisperer/

开发者在日常中需要使用多种语言、框架和代码库,或者与云上的服务交互,在平时需要到处查找这些代码片段,这是一个分散注意力的事情。

Amazon CodeWhisperer 是基于机器学习的代码aide,开发者只需要写一个注释,例如"upload a file to S3",基于此CodeWhisperer会自动检查哪个公开库是最适合的,并自动在IDE里生成代码。它并不是仅仅简单的从网上复制粘帖代码,而是根据代码文件的上下文(例如使用了哪个版本的代码库)来生成代码。它也可以与VS Code和JetBrains IDE无缝集成,所以用户不必离开自己的开发环境,目前它已经支持Java,Python, JavaScript, C#和TypeScript。

实验介绍

本实验将搭建一个事件驱动的图片识别无服务器应用,我们将使用Lambda与 Amazon Rekognition , Amazon DynamoDB , Amazon Simple Notification Service (Amazon SNS) , Amazon Simple Queue Service (Amazon SQS) , Amazon Simple Storage Service (Amazon S3) 交互

我们的应用分为三部分:

  1. 图片下载:用户提供图片URL,Lambda从这个URL下载图片,保存到S3, S3自动发送通知给SNS,SNS将消息传送到SQS

  2. 图片识别:第二个Lambda处理图片,它从SQS收到消息后,将图片发给Amazon Rekognition来处理,将识别的结果保存到DynamoDB表里,并将JSON结果发送到SNS,SNS再发到SQS,用于第三部分处理。通过查DynamoDB,用户可以调用API查看图片中的物体

  3. 三方集成:最后一个Lambda从第二个SQS里读取消息,将消息发送到外部email服务器

整个流程如下:

Architecture diagram depicting the application architecture. It contains the service icons with the component explained on the text above

环境配置

从这个仓库下载CDK代码:https://github.com/aws-samples/amazon-codewhisperer-workshop, 这里面会创建基础的AWS资源(API Gateway, DynamoDB, SNS, SQS...),但是应用的代码逻辑并没有提供,我们将使用CodeWhisperer来完成。

进入代码库的python目标下,先安装依赖:

python3 -m pip install -r requirements.txt

执行cdk bootstrap命令:

cdk bootstrap aws://{AWS_ACCOUNT_ID}/{AWS_REGION}

最后一步就是在你自己的IDE上开启CodeWhisperer,VS Code / Jetbrains参考:

图片下载

我们先实现第一个Lambda函数:下载URL里的图片并保存到S3。打开python/api/runtime/get_save_image.py文件, 里面包含一个空的Lambda handler:

  • url 是用户提供的图片下载地址
  • name是用户提供的图片名称
  • S3_BUCKET 是保存图片的桶

使用自然语言写一句注释,描述需要的功能:

# Function to get a file from url

为了触发CodeWhisperer,写完注释后按下回车,并等待建议;如果想手动触发,可以在Mac上按Option + C,Windows上按Alt + C, 使用方向键查看不同的建议,并按Tab来接受某个建议。更多使用技巧参考: Working with CodeWhisperer in VS CodeWorking with Amazon CodeWhisperer from JetBrains

上面的注释,CodeWhisperer会给出以下类似的代码:

Screenshot of the code generated by CodeWhisperer on VS Code. It has a function called get_file_from_url with the implementation suggestion to download a file using the requests lib

由于CodeWhisperer使用AI来提供代码,所以每次运行可能有不同的结果,但用户可以据此框架来做一些改动

让我们来实现上传图片到S3的逻辑:

# Function to upload image to S3

CodeWhisperer会生成类似建议代码:

Screenshot of the code generated by CodeWhisperer on VS Code. It has a function called upload_image with the implementation suggestion to download a file using the requests lib and upload it to S3 using the S3 client

图像识别

现在让我们实现第二个Lambda逻辑:将图片发给Amazon Rekognition来处理,将识别的结果保存到DynamoDB表里,并将JSON结果发送到SNS。打开python/recognition/runtime/image_recognition.py,这个文件接收三个参数:

  • queue_url是Lambda所订阅SQS的URL
  • table_name 是DynamoDB表名称
  • topic_arn是SNS的ARN

使用CodeWhisperer来写下我们的需求:

# Detect labels from image with Rekognition

CodeWhisperer会给出类似以下代码片段:

Screenshot of the code generated by CodeWhisperer on VS Code. It has a function called detect_labels with the implementation suggestion to use the Rekognition SDK to detect labels on the given image

继续实现其他逻辑,例如:

  • # Save labels to DynamoDB

  • # Publish item to SNS

  • # Delete message from SQS

遵循一样的流程,打开python/recognition/runtime/list_images.py, 实现列出DynamoDB表图片标签的逻辑:

# Function to list all items from a DynamoDB table

JSON转换XML

CodeWhisperer不仅仅用于与AWS交互,使用它可以来实现重复性的任务,例如单元测试、实现排序和字符串匹配算法…

最后我们要实现的Lambda就是要把JSON转成XML,并发送到邮件服务器

打开python/integration/runtime/send_email.py,里面包含一个空的handler。输入以下注释:

# Transform json to xml

CodeWhisperer会生成类似如下代码:

Screenshot of the code generated by CodeWhisperer on VS Code. It has a function called json_to_xml with the implementation suggestion to transform JSON payload into XML payload

重复以上内容,输入 # Send XML string with HTTP POST 来实现最后的逻辑

部署及测试应用

为了部署应用,运行cdk deploy --all,部署完成后会输出API Gateway的地址:

Outputs:
...
APIStack.RESTAPIEndpoint01234567 = https://examp1eid0.execute-
api.{your-region}.amazonaws.com/prod/

第一个API Gateway URL需要传入两个参数,url(要下载图片的地址)和name(保存到S3里的图片名称),传入image URL时要先encode,然后执行curl命令进行测试:

curl -X GET 'https://examp1eid0.execute-api.eu-east-
2.amazonaws.com/prod?url={encoded-image-URL}&name={file-name}'

几秒过后,整个流程处理完成。我们可以调用list images API来查询:

curl -X GET 'https://examp1eid7.execute-api.eu-east-2.amazonaws.com/prod'

在测试完成后,记得执行cdk destroy来删除整个CDK,避免产生额外的帐单

总结

在本文中,我们看到了ML的力量,帮我们大大提高生产力。开发者的精力集中在IDE上面,减少了到网上查找代码片段的时间。除此之外,CodeWhisperer还可以扫描你的代码,检查是否有安全问题。

在preview阶段,CodeWhisperer对所有开发者免费,可以在VS Code, JetBrains, Cloud9上来使用它。