我使用API Gateway暴露了2个端点,一个用于启动执行,另一个用于检查执行状态。
这些操作不需要使用lambda,也不需要通过参数指示步骤函数arn。
解决方案的CloudFormation 模板如下:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: >
The turtle calculator, a dummy slow calculator for comprehensive code example
Parameters:
OpenAPIS3File:
Description: '"openapi.yaml" file location'
Default: ./openapi.yaml
Type: String
Resources:
Workflow:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName: !Sub ${AWS::StackName}-state-machine
RoleArn: !GetAtt StateExecutionRole.Arn
DefinitionString: !Sub |
{
...
}
Api:
Type: AWS::Serverless::Api
Properties:
StageName: random-workflow
Name: !Sub ${AWS::StackName}-api
DefinitionBody:
'Fn::Transform':
Name: AWS::Include
Parameters:
Location: !Ref OpenAPIS3File
而 OpenAPI 配置如下:
openapi: 3.0.1
info:
title: Api Mocker
description: Automated testing application for TGR
contact:
name: Patagonia-IT
url: http://www.patagonia-it.com
email: contacto@patagonia-it.com
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
x-amazon-apigateway-request-validators:
params:
validateRequestParameters: true
validateRequestBody: false
x-amazon-apigateway-request-validator: params
paths:
/workflow:
post:
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ ApiGatewayStepFunctionsRole, Arn ]
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/StartExecution
httpMethod: POST
type: aws
responses:
default:
statusCode: 200
responseTemplates:
application/json: |
'{ "executionId": "$input.json('executionArn').split(':').get(7) }'
requestTemplates:
application/json:
Fn::Sub: |-
{
"input": "$util.escapeJavaScript($input.json('$'))",
"name": "$context.requestId",
"stateMachineArn": "${Workflow}"
}
summary: Start workflow instance
responses:
200:
$ref: '#/components/responses/200Execution'
403:
$ref: '#/components/responses/Error'
/workflow/{executionId}:
get:
x-amazon-apigateway-integration:
credentials:
Fn::GetAtt: [ ApiGatewayStepFunctionsRole, Arn ]
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/DescribeExecution
httpMethod: POST
type: aws
responses:
default:
statusCode: 200
responseTemplates:
application/json: |-
#set ($status = $input.json('status'))
{
#if($status == '"SUCCEEDED"')
"output": $util.parseJson($input.json('output')),
#end
"status": $status
}
requestTemplates:
application/json:
Fn::Sub: |-
{
"executionArn": "arn:aws:states:${AWS::Region}:${AWS::AccountId}:execution:${Workflow.Name}:$input.params().path.get('executionId')"
}
summary: Workflow execution status
responses:
200:
$ref: '#/components/responses/200ExecutionDetails'
我在 Github 上提交了一个可工作的示例,网址为 https://github.com/jvillane/aws-sam-step-functions-lambda。
$context.accountId
获取帐户 ID,但是找不到任何可以提供当前区域名称的东西。在我的模板中,stateMachineArn
现在看起来像这样:"arn:aws:states:us-east-1:$context.accountId:stateMachine:AbcStateMachine"
。有什么办法可以动态获取当前区域吗? - rockyPeoplesChamp