aws(学习笔记第二十四课) 使用sam开发step functions

embedded/2025/1/15 8:46:00/

aws_0">aws(学习笔记第二十四课)

  • 使用sam开发step functions

学习内容:

  • 生成samstep functions实例程序
  • 什么是SAM amazon Serverless Application Model
  • SAM程序结构
  • SAM执行程序

1. 生成samstep functions实例程序

  1. 参照文档
    这里参照AWS的官方文档SAM amazon Serverless Application Model
  2. 什么是SAM amazon Serverless Application Model
    • 整体架构
      SAM就是一个基于Cloudformation的应用程序框架,主要目的正如名字(Serverless Application Model),方便进行Serverless Application的开发。
      开发的一般步骤如下:
      • 开发lambdaserverless application
      • 利用step functionsserverless application。当然,其中可以调用lambda
      • 之后利用上面的serverless application,进行Cloudformationtemplate定义。
      • 最后经过sam buildsam deploy部署到AWS的环境中。
        在这里插入图片描述

3. SAM程序结构

  1. 开始使用SAM
    • 这里使用SAM提供的实例程序进行练习
      • HourlyTradingSchedule是一个AWS EventBridge的规则,这里定义了股票投资程序的调用周期。类似于linuxcron job
      • StockTradingStateMachine就是股票交易的StateMachine
      • 股票交易的StateMachine里面包括三个lambda
        • StockCheckerFunction这里随机产生股票的价格(进行简单的股票市场的模拟)
        • 中间其实有一个choice state,进行判断。这里没有画出来
        • 之后根据判断,如果股票高过某个固定价格,那么进行StockBuyerFunction的调用
        • 如果股票高过某个固定价格,那么进行StockSellerFunction的调用
        • 最后,不管买还是卖的操作,都进行TransactionTable的写入(使用DynamoDB记录交易)
          在这里插入图片描述
    • 进行实际代码的实验
      • 实验环境
        这里还是使用非常给力的工具CloudShell
        在这里插入图片描述
      • 构建代码
        • 创建代码的父路径
          mkdir demo-sam
          cd demo-sam
          
        • 使用sam生成股票实例代码(这个代码是sam自带的)
          在这里插入图片描述
          之后进行一些runtime的相关设定。
          在这里插入图片描述
          到这里,代码就会被生成出来,而且cloudformationtemplate文件都是yaml格式的。
          在这里插入图片描述
    • cloudshell环境中的代码通过S3取到本地
      • 创建传输文件的S3 bucket
        因为cloudshell不是很容易和本地传输文件,所以使用S3 bucket
        在这里插入图片描述
      • SAM代码打包,copyS3 bucket
        tar zcvf demo-sam.tar.gz demo-sam/
        aws s3 cp demo-sam.tar.gz s3://finlay-cloudshell/
        
        在这里插入图片描述
      • SAM init生成的实例程序代码,下载到本地
        在这里插入图片描述
        本地文件夹如下所示。
        在这里插入图片描述
      • SAM init生成的实例程序代码使用vscode打开(这里单纯的可以更加容易编辑代码)
        • template文件
          AWSTemplateFormatVersion: '2010-09-09'
          Transform: AWS::Serverless-2016-10-31
          Description: |demo-samSample SAM Template for demo-samResources:StockTradingStateMachine:Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.htmlProperties:DefinitionSubstitutions:StockCheckerFunctionArn: !GetAtt StockCheckerFunction.ArnStockSellerFunctionArn: !GetAtt StockSellerFunction.ArnStockBuyerFunctionArn: !GetAtt StockBuyerFunction.ArnDDBPutItem: !Sub arn:${AWS::Partition}:states:::dynamodb:putItemDDBTable: !Ref TransactionTableEvents:HourlyTradingSchedule:Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.htmlProperties:Description: Schedule to run the stock trading state machine every hourEnabled: false # This schedule is disabled by default to avoid incurring charges.Schedule: rate(1 hour)Policies:# Find out more about SAM policy templates: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html- LambdaInvokePolicy:FunctionName: !Ref StockCheckerFunction- LambdaInvokePolicy:FunctionName: !Ref StockSellerFunction- LambdaInvokePolicy:FunctionName: !Ref StockBuyerFunction- DynamoDBWritePolicy:TableName: !Ref TransactionTableDefinitionUri: statemachine/stock_trader.asl.jsonStockCheckerFunction:Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.htmlProperties:CodeUri: functions/stock_checker/Handler: app.lambda_handlerRuntime: python3.9Architectures:- x86_64StockSellerFunction:Type: AWS::Serverless::FunctionProperties:CodeUri: functions/stock_seller/Handler: app.lambda_handlerRuntime: python3.9Architectures:- x86_64StockBuyerFunction:Type: AWS::Serverless::FunctionProperties:CodeUri: functions/stock_buyer/Handler: app.lambda_handlerRuntime: python3.9Architectures:- x86_64TransactionTable:Type: AWS::Serverless::SimpleTable # More info about SimpleTable Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-simpletable.htmlProperties:PrimaryKey:Name: IdType: StringProvisionedThroughput:ReadCapacityUnits: 1WriteCapacityUnits: 1ApplicationResourceGroup:Type: AWS::ResourceGroups::GroupProperties:Name: !Sub ApplicationInsights-SAM-${AWS::StackName}ResourceQuery:Type: CLOUDFORMATION_STACK_1_0ApplicationInsightsMonitoring:Type: AWS::ApplicationInsights::ApplicationProperties:ResourceGroupName: !Ref ApplicationResourceGroupAutoConfigurationEnabled: 'true'
          Outputs:# StockTradingStateMachineHourlyTradingSchedule is an implicit Schedule event rule created out of Events key under Serverless::StateMachine# Find out more about other implicit resources you can reference within SAM# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources.htmlStockTradingStateMachineArn:Description: Stock Trading State machine ARNValue: !Ref StockTradingStateMachineStockTradingStateMachineRoleArn:Description: IAM Role created for Stock Trading State machine based on thespecified SAM Policy TemplatesValue: !GetAtt StockTradingStateMachineRole.Arn
          
          使用vscode打开template文件,可以看到AWS service的图形化结构。
          在这里插入图片描述
        • stock_checker文件 随机产生stock的价格
          from random import randintdef lambda_handler(event, context):"""Sample Lambda function which mocks the operation of checking the current price of a stock.For demonstration purposes this Lambda function simply returns a random integer between 0 and 100 as the stock price.Parameters----------event: dict, requiredInput event to the Lambda functioncontext: object, requiredLambda Context runtime methods and attributesReturns------dict: Object containing the current price of the stock"""# Check current price of the stockstock_price = randint(0, 100)  # Current stock price is mocked as a random integer between 0 and 100return {"stock_price": stock_price}
          
        • stock_buyer文件 如果价格低,选择买进
          from datetime import datetime
          from random import randint
          from uuid import uuid4def lambda_handler(event, context):"""Sample Lambda function which mocks the operation of buying a random numberof shares for a stock.For demonstration purposes, this Lambda function does not actually perform any actual transactions. It simply returns a mocked result.Parameters----------event: dict, requiredInput event to the Lambda functioncontext: object, requiredLambda Context runtime methods and attributesReturns------dict: Object containing details of the stock buying transaction"""# Get the price of the stock provided as inputstock_price = event["stock_price"]# Mocked result of a stock buying transactiontransaction_result = {"id": str(uuid4()),  # Unique ID for the transaction"price": str(stock_price),  # Price of each share"type": "buy",  # Type of transaction (buy/sell)"qty": str(randint(1, 10)),  # Number of shares bought/sold (We are mocking this as a random integer between 1 and 10)"timestamp": datetime.now().isoformat(),  # Timestamp of the when the transaction was completed}return transaction_result
          
        • stock_seller文件 如果价格高,选择抛出
          from datetime import datetime
          from random import randint
          from uuid import uuid4def lambda_handler(event, context):"""Sample Lambda function which mocks the operation of selling a random numberof shares for a stock.For demonstration purposes, this Lambda function does not actually perform any actual transactions. It simply returns a mocked result.Parameters----------event: dict, requiredInput event to the Lambda functioncontext: object, requiredLambda Context runtime methods and attributesReturns------dict: Object containing details of the stock selling transaction"""# Get the price of the stock provided as inputstock_price = event["stock_price"]# Mocked result of a stock selling transactiontransaction_result = {"id": str(uuid4()),  # Unique ID for the transaction"price": str(stock_price),  # Price of each share"type": "sell",  # Type of transaction (buy/sell)"qty": str(randint(1, 10)),  # Number of shares bought/sold (We are mocking this as a random integer between 1 and 10)"timestamp": datetime.now().isoformat(),  # Timestamp of the when the transaction was completed}return transaction_result
          
        • step functions文件 将上面的lambda函数穿插起来,形成一个工作流程编排
          {"Comment": "A state machine that does mock stock trading.","StartAt": "Check Stock Value","States": {"Check Stock Value": {"Type": "Task","Resource": "${StockCheckerFunctionArn}","Retry": [{"ErrorEquals": ["States.TaskFailed"],"IntervalSeconds": 15,"MaxAttempts": 5,"BackoffRate": 1.5}],"Next": "Buy or Sell?"},"Buy or Sell?": {"Type": "Choice","Choices": [{"Variable": "$.stock_price","NumericLessThanEquals": 50,"Next": "Buy Stock"}],"Default": "Sell Stock"},"Sell Stock": {"Type": "Task","Resource": "${StockSellerFunctionArn}","Retry": [{"ErrorEquals": ["States.TaskFailed"],"IntervalSeconds": 2,"MaxAttempts": 3,"BackoffRate": 1}],"Next": "Record Transaction"},"Buy Stock": {"Type": "Task","Resource": "${StockBuyerFunctionArn}","Retry": [{"ErrorEquals": ["States.TaskFailed"],"IntervalSeconds": 2,"MaxAttempts": 3,"BackoffRate": 1}],"Next": "Record Transaction"},"Record Transaction": {"Type": "Task","Resource": "${DDBPutItem}","Parameters": {"TableName": "${DDBTable}","Item": {"Id": {"S.$": "$.id"},"Type": {"S.$": "$.type"},"Price": {"N.$": "$.price"},"Quantity": {"N.$": "$.qty"},"Timestamp": {"S.$": "$.timestamp"}}},"Retry": [{"ErrorEquals": ["States.TaskFailed"],"IntervalSeconds": 20,"MaxAttempts": 5,"BackoffRate": 10}],"End": true}}
          }
          
        检查vscodesam预览功能
        在这里插入图片描述
    • 回到cloudshell执行sam buildsam deploy部署到AWS
      sam build
      sam deploy
      
      可以看到,本质上这个sam application还是使用Cloudformation进行部署。
      在这里插入图片描述
      查看Cloudformation进一步验证了想定结果。在这里插入图片描述

4. 执行samstep functions实例程序

  1. 打开默认的aws eventbridge的规则设置
    默认是禁用的,编辑这里,打开禁用。
    在这里插入图片描述

  2. 查看执行结果
    可以看出,已经成功执行一次

    在这里插入图片描述
    股票价格为2,执行了buy stock lambda
    在这里插入图片描述


http://www.ppmy.cn/embedded/154066.html

相关文章

AI数字人PPT课件视频——探索新一代教学视频生成工具

引言 随着互联网技术的迅猛发展,在线教育已经从早期的电视教学,历经多媒体课程、微课和精品课的迭代。如今,面对AI技术的飞速进步,我们正站在一个新时代的门槛上——一种全新的内容生成工具正在革新在线教育的内容制作方式&#…

《拉依达的嵌入式\驱动面试宝典》—计算机网络篇(二)

《拉依达的嵌入式\驱动面试宝典》—计算机网络篇(二) 你好,我是拉依达。 感谢所有阅读关注我的同学支持,目前博客累计阅读 27w,关注1.5w人。其中博客《最全Linux驱动开发全流程详细解析(持续更新)-CSDN博客》已经是 Linux驱动 相关内容搜索的推荐首位,感谢大家支持。 《…

【前端】自学基础算法 -- 24.动态规划-变态青蛙蛙跳台阶

动态规划-变态青蛙跳台阶 变态青蛙跳台阶 一只青蛙,一次只能跳1级台阶、2级台阶、3级台阶、…、n级台阶 问:这只青蛙跳上n级台阶,有多少种跳法 递推公式: f(n) f(n -1) f(n-2) f(n-3) … f(1) f(0) 实现方法 还是基于斐波那…

Thc-Ipv6攻击工具包 全参数详细解析!Kali Linux入门教程!黑客渗透测试!

简介 用于测试 IPv6 和 ICMPv6 协议弱点的攻击工具包。 其中一些工具包括: alive6:有效的活体扫描。denial6:尝试针对某个对象进行一系列拒绝服务测试目标。detector-new-ip6:检测加入网络的新 ip6 设备。dnsdict6:…

Visual Studio Code (VSCode)为当前项目设置保存时自动格式化

在 Visual Studio Code (VSCode) 中,你可以为单个项目设置特定的配置,而不会影响全局设置。这可以通过创建项目级别的设置文件来实现。以下是具体步骤: 为当前项目设置保存时自动格式化 打开命令面板: 使用快捷键 CtrlShiftP&…

本地服务器Docker搭建个人云音乐平台Splayer并实现远程访问告别烦人广告

前言 大家好!今天我要给大家分享的是如何在Ubuntu上用Docker快速搭建高颜值无广告的某抑云音乐播放器Splayer的详细流程,并且结合cpolar内网穿透工具实现远程访问。如果你是音乐爱好者,经常需要在外办公或旅行,这个教程绝对能让你…

基于springboot果蔬供应链信息管理平台

基于Spring Boot的果蔬供应链信息管理平台是一种集成了先进信息技术和果蔬供应链管理理念的综合性系统。 一、背景与意义 随着人们生活水平的提高和对健康饮食的重视,果蔬市场需求不断增长。然而,果蔬供应链涉及多个环节,包括种植、采摘、加…

面向对象分析与设计Python版 创建者原则与信息专家原则

文章目录 前言一、创建者原则二、信息专家原则 前言 通用职责分配软件原则 GRASP(General Responsibility Assignment Software Principles),是一组用于指导软件设计,尤其是在面向对象设计中的原则。包括以下九个主要原则&#x…