gpt4 book ai didi

amazon-web-services - 用于 Lambda 的 AWS 状态机,cloudformation 语法

转载 作者:行者123 更新时间:2023-12-03 07:37:05 26 4
gpt4 key购买 nike

我正在尝试提出一个 CloudFormation 模板,其中包括

  • API 网关
  • 通过 API 网关调用 StateMachine
  • StateMachine 又包含一个 lambda 函数

本质上我想做的是以下

https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-api-gateway.html

但是,我一直在想出将部署它的云形成模板(.yaml)。到目前为止,这就是我所拥有的

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
Post:
Type: AWS::Serverless::Function
Properties:
FunctionName: UserBase-fnUsers
Handler: UsersHandler.getUsers
Runtime: nodejs6.10
Policies: [AmazonDynamoDBReadOnlyAccess, AmazonS3ReadOnlyAccess]
Environment:
Variables:
S3_BUCKET: UserBase-Users-bucket
UsersTable: UserBase-Users-tblUsers
Events:
GetUsers:
Type: Api
Properties:
Path: /UserBase/Users
Method: post
Options:
Type: AWS::Serverless::Function
Properties:
FunctionName: UserBase-fnUsers-Options
Handler: UsersHandler.getOptions
Runtime: nodejs6.10
Events:
GetOptions:
Type: Api
Properties:
Path: /UserBase/Users
Method: options
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: UserBase-Users-tblUsers
AttributeDefinitions:
- AttributeName: Id
AttributeType: S
KeySchema:
- AttributeName: Id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
StreamSpecification:
StreamViewType: KEYS_ONLY
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "lambda:InvokeFunction"
Resource: "*"

UpdateShoppingPath:
Type: "AWS::StepFunctions::StateMachine"
Properties:
DefinitionString:
!Sub
- |-
{
"Comment": "State machine to update the shopping path",
"StartAt": "UpdatePath",
"States": {
"UpdatePath": {
"Type": "Task",
"Resource": "${lambdaArn}",
"End": true
}
}
}
- {lambdaArn: !GetAtt [ Post, Arn ]}
RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
UserBaseUsers:
Type: "AWS::ApiGateway::Resource"

我被最后一部分困住了,主要是关于如何将 ApiGateway 链接到 StateMachine。顺便说一句,我有什么方法可以从 AWS 中的现有部署生成云形成模板(.yaml 或 json)吗?

最佳答案

我不是 yaml 方面的专家,但我使用 json CloudFormation 做了一些配置,据我所知,它很容易翻译。

过去我也像你一样陷入困境,并且here这是我的帖子和我的解决方案

要开始执行 Step Functions,您需要做的是将 HTTP Post 发送到作为 json 对象传递的 arn:aws:apigateway:${region}:states:action/StartExecution [docs] :

{
input: __input__,
stateMachineArn: __arn__
}

简而言之,在您的 AWS::ApiGateway::Method 中,您必须将 HTTP 集成设置为 arn:aws:apigateway:${region}:states:action/StartExecution 和一个构建我提到的 json 对象的 requestTemplate。

作为引用,这里是我的 json cloudformation 示例:

"FooRequest": {
"DependsOn": ["FooStepMachine"],
"Type": "AWS::ApiGateway::Method",
"Properties": {
"HttpMethod": "POST",
"Integration": {
"Type": "AWS",
"Credentials": {
"Fn::GetAtt": ["FooRole",
"Arn"]
},
"IntegrationHttpMethod": "POST",
"Uri": {
"Fn::Join": ["",
["arn:aws:apigateway:",
{
"Ref": "AWS::Region"
},
":states:action/StartExecution"]]
},
"IntegrationResponses": [{
"StatusCode": 200
},
{
"StatusCode": 401
}],
"RequestTemplates": {
"application/json": {
"Fn::Sub": ["{\"input\": \"$util.escapeJavaScript($input.json('$'))\",\"stateMachineArn\": \"${arn}\"}",
{
"arn": {
"Ref": "FooStepMachine"
}
}]
}
}
}
}
}

关于amazon-web-services - 用于 Lambda 的 AWS 状态机,cloudformation 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com