本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 AWS CDK 在 Step Functions 中创建快速工作流程
在本教程中,您将学习如何使用同步 Express 状态机作为后端集成来创建API网关 RESTAPI,使用 AWS Cloud Development Kit (AWS CDK) 基础设施即代码 (IAC) 框架。
您将使用该StepFunctionsRestApi
构造将状态机连接到API网关。该StepFunctionsRestApi
构造将设置默认的输入/输出映射和API网关 RESTAPI,具有所需的权限和 HTTP “ANY” 方法。
随着 AWS CDK 是一个基础设施即代码 (IAC) 框架,你定义 AWS 使用编程语言的基础架构。您使用支持的语言之一定义应用程序,然后将代码合成为 CDK AWS CloudFormation 模板,然后将基础架构部署到您的 AWS account。
你将使用 AWS CloudFormation 要定义与同步 Express 状态机集成为后端的API网关 RESTAPI,然后使用 AWS Management Console 启动执行。
在开始本教程之前,请先设置您的 AWS CDK 开发环境,如入门中所述 AWS CDK -先决条件,然后安装 AWS CDK 通过发行:
npm install -g aws-cdk
第 1 步:设置你的 AWS CDK 项目
首先,为你的新建一个目录 AWS CDK 应用程序并初始化项目。
- TypeScript
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language typescript
- JavaScript
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language javascript
- Python
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language python
项目初始化后,激活项目的虚拟环境并安装 AWS CDK的基线依赖关系。
source .venv/bin/activate
python -m pip install -r requirements.txt
- Java
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language java
- C#
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language csharp
- Go
-
mkdir stepfunctions-rest-api
cd stepfunctions-rest-api
cdk init --language go
请确保将目录命名为 stepfunctions-rest-api
。这些区域有: AWS CDK 应用程序模板使用目录的名称为源文件和类生成名称。如果您使用其他名称,则您的应用将与本教程不匹配。
现在安装构造库模块 AWS Step Functions 和 Amazon API Gateway
- TypeScript
-
npm install @aws-cdk/aws-stepfunctions @aws-cdk/aws-apigateway
- JavaScript
-
npm install @aws-cdk/aws-stepfunctions @aws-cdk/aws-apigateway
- Python
-
python -m pip install aws-cdk.aws-stepfunctions
python -m pip install aws-cdk.aws-apigateway
- Java
-
编辑项目的 pom.xml
将以下依赖项添加到现有 <dependencies>
容器。
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>stepfunctions</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigateway</artifactId>
<version>${cdk.version}</version>
</dependency>
Maven 会在下次构建应用程序时自动安装这些依赖关系。要构建、发出mvn compile
或使用 Java IDE 的 “构建” 命令。
- C#
-
dotnet add src/StepfunctionsRestApi package Amazon.CDK.AWS.Stepfunctions
dotnet add src/StepfunctionsRestApi package Amazon.CDK.AWS.APIGateway
您也可以使用 Visual Studio 安装指定的软件包 NuGetGUI,该软件包可通过 “工具” > “NuGet 包管理器” > “管理解决方案 NuGet 包” 获得。
一旦安装了模块,就可以在自己的模块中使用它们 AWS CDK 通过导入以下软件包来实现应用程序。
- TypeScript
-
@aws-cdk/aws-stepfunctions
@aws-cdk/aws-apigateway
- JavaScript
-
@aws-cdk/aws-stepfunctions
@aws-cdk/aws-apigateway
- Python
-
aws_cdk.aws_stepfunctions
aws_cdk.aws_apigateway
- Java
-
software.amazon.awscdk.services.apigateway.StepFunctionsRestApi
software.amazon.awscdk.services.stepfunctions.Pass
software.amazon.awscdk.services.stepfunctions.StateMachine
software.amazon.awscdk.services.stepfunctions.StateMachineType
- C#
-
Amazon.CDK.AWS.StepFunctions
Amazon.CDK.AWS.APIGateway
- Go
-
将以下内容添加到 stepfunctions-rest-api.go
内的 import
中
"github.com/aws/aws-cdk-go/awscdk/awsapigateway"
"github.com/aws/aws-cdk-go/awscdk/awsstepfunctions"
第 2 步:使用 AWS CDK
使用RESTAPI同步 Express St API ate Machine 后端集成创建网关
首先,我们将介绍定义同步 Express State Machine 和 API Gateway 的各个代码 RESTAPI,然后解释如何将它们组合到你的 AWS CDK 应用程序。然后,您将了解如何合成和部署这些资源。
我们将在此处展示的状态机将是一个带有 Pass
状态的简单状态机。
创建快速状态机
这是 AWS CDK 定义带有状态的简单Pass
状态机的代码。
- TypeScript
-
const machineDefinition = new stepfunctions.Pass(this, 'PassState', {
result: {value:"Hello!"},
})
const stateMachine = new stepfunctions.StateMachine(this, 'MyStateMachine', {
definition: machineDefinition,
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
- JavaScript
-
const machineDefinition = new sfn.Pass(this, 'PassState', {
result: {value:"Hello!"},
})
const stateMachine = new sfn.StateMachine(this, 'MyStateMachine', {
definition: machineDefinition,
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
- Python
-
machine_definition = sfn.Pass(self,"PassState",
result = sfn.Result("Hello"))
state_machine = sfn.StateMachine(self, 'MyStateMachine',
definition = machine_definition,
state_machine_type = sfn.StateMachineType.EXPRESS)
- Java
-
Pass machineDefinition = Pass.Builder.create(this, "PassState")
.result(Result.fromString("Hello"))
.build();
StateMachine stateMachine = StateMachine.Builder.create(this, "MyStateMachine")
.definition(machineDefinition)
.stateMachineType(StateMachineType.EXPRESS)
.build();
- C#
-
var machineDefinition = new Pass(this, "PassState", new PassProps
{
Result = Result.FromString("Hello")
});
var stateMachine = new StateMachine(this, "MyStateMachine", new StateMachineProps
{
Definition = machineDefinition,
StateMachineType = StateMachineType.EXPRESS
});
- Go
-
var machineDefinition = awsstepfunctions.NewPass(stack, jsii.String("PassState"), &awsstepfunctions.PassProps
{
Result: awsstepfunctions.NewResult(jsii.String("Hello")),
})
var stateMachine = awsstepfunctions.NewStateMachine(stack, jsii.String("StateMachine"), &awsstepfunctions.StateMachineProps
{
Definition: machineDefinition,
StateMachineType: awsstepfunctions.StateMachineType_EXPRESS,
})
您可以在这个简短的片段中看到:
RESTAPI使用StepFunctionsRestApi
构造创建API网关
我们将使用StepFunctionsRestApi
构造来创建RESTAPI具有所需权限和默认输入/输出映射的API网关。
- TypeScript
-
const api = new apigateway.StepFunctionsRestApi(this,
'StepFunctionsRestApi', { stateMachine: stateMachine });
- JavaScript
-
const api = new apigateway.StepFunctionsRestApi(this,
'StepFunctionsRestApi', { stateMachine: stateMachine });
- Python
-
api = apigw.StepFunctionsRestApi(self, "StepFunctionsRestApi",
state_machine = state_machine)
- Java
-
StepFunctionsRestApi api = StepFunctionsRestApi.Builder.create(this, "StepFunctionsRestApi")
.stateMachine(stateMachine)
.build();
- C#
-
var api = new StepFunctionsRestApi(this, "StepFunctionsRestApi", new StepFunctionsRestApiProps
{
StateMachine = stateMachine
});
- Go
-
awsapigateway.NewStepFunctionsRestApi(stack, jsii.String("StepFunctionsRestApi"), &awsapigateway.StepFunctionsRestApiProps
{
StateMachine = stateMachine,
})
要构建和部署 AWS CDK 应用程序
在 AWS CDK 您创建的项目,编辑包含堆栈定义的文件,使其看起来像下面的代码。你将从上面认出 Step Functions 状态机和API网关的定义。
- TypeScript
-
更新 lib/stepfunctions-rest-api-stack.ts
读取如下信息。
import * as cdk from 'aws-cdk-lib';
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions'
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class StepfunctionsRestApiStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const machineDefinition = new stepfunctions.Pass(this, 'PassState', {
result: {value:"Hello!"},
});
const stateMachine = new stepfunctions.StateMachine(this, 'MyStateMachine', {
definition: machineDefinition,
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
const api = new apigateway.StepFunctionsRestApi(this,
'StepFunctionsRestApi', { stateMachine: stateMachine });
- JavaScript
-
更新 lib/stepfunctions-rest-api-stack.js
读取如下信息。
const cdk = require('@aws-cdk/core');
const stepfunctions = require('@aws-cdk/aws-stepfunctions');
const apigateway = require('@aws-cdk/aws-apigateway');
class StepfunctionsRestApiStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const machineDefinition = new stepfunctions.Pass(this, "PassState", {
result: {value:"Hello!"},
})
const stateMachine = new sfn.StateMachine(this, 'MyStateMachine', {
definition: machineDefinition,
stateMachineType: stepfunctions.StateMachineType.EXPRESS,
});
const api = new apigateway.StepFunctionsRestApi(this,
'StepFunctionsRestApi', { stateMachine: stateMachine });
}
}
module.exports = { StepStack }
- Python
-
更新 stepfunctions_rest_api/stepfunctions_rest_api_stack.py
读取如下信息。
from aws_cdk import App, Stack
from constructs import Construct
from aws_cdk import aws_stepfunctions as sfn
from aws_cdk import aws_apigateway as apigw
class StepfunctionsRestApiStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
machine_definition = sfn.Pass(self,"PassState",
result = sfn.Result("Hello"))
state_machine = sfn.StateMachine(self, 'MyStateMachine',
definition = machine_definition,
state_machine_type = sfn.StateMachineType.EXPRESS)
api = apigw.StepFunctionsRestApi(self,
"StepFunctionsRestApi",
state_machine = state_machine)
- Java
-
更新 src/main/java/com.myorg/StepfunctionsRestApiStack.java
读取如下信息。
package com.myorg;
import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.stepfunctions.Pass;
import software.amazon.awscdk.services.stepfunctions.StateMachine;
import software.amazon.awscdk.services.stepfunctions.StateMachineType;
import software.amazon.awscdk.services.apigateway.StepFunctionsRestApi;
public class StepfunctionsRestApiStack extends Stack {
public StepfunctionsRestApiStack(final Construct scope, final String id) {
this(scope, id, null);
}
public StepfunctionsRestApiStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Pass machineDefinition = Pass.Builder.create(this, "PassState")
.result(Result.fromString("Hello"))
.build();
StateMachine stateMachine = StateMachine.Builder.create(this, "MyStateMachine")
.definition(machineDefinition)
.stateMachineType(StateMachineType.EXPRESS)
.build();
StepFunctionsRestApi api = StepFunctionsRestApi.Builder.create(this, "StepFunctionsRestApi")
.stateMachine(stateMachine)
.build();
}
}
- C#
-
更新 src/StepfunctionsRestApi/StepfunctionsRestApiStack.cs
读取如下信息。
using Amazon.CDK;
using Amazon.CDK.AWS.StepFunctions;
using Amazon.CDK.AWS.APIGateway;
namespace StepfunctionsRestApi
{
public class StepfunctionsRestApiStack : Stack
{
internal StepfunctionsRestApi(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var machineDefinition = new Pass(this, "PassState", new PassProps
{
Result = Result.FromString("Hello")
});
var stateMachine = new StateMachine(this, "MyStateMachine", new StateMachineProps
{
Definition = machineDefinition,
StateMachineType = StateMachineType.EXPRESS
});
var api = new StepFunctionsRestApi(this, "StepFunctionsRestApi", new StepFunctionsRestApiProps
{
StateMachine = stateMachine
});
}
}
}
- Go
-
更新 stepfunctions-rest-api.go
读取如下信息。
package main
import (
"github.com/aws/aws-cdk-go/awscdk"
"github.com/aws/aws-cdk-go/awscdk/awsapigateway"
"github.com/aws/aws-cdk-go/awscdk/awsstepfunctions"
"github.com/aws/constructs-go/constructs/v3"
"github.com/aws/jsii-runtime-go"
)
type StepfunctionsRestApiGoStackProps struct {
awscdk.StackProps
}
func NewStepfunctionsRestApiGoStack(scope constructs.Construct, id string, props *StepfunctionsRestApiGoStackProps) awscdk.Stack {
var sprops awscdk.StackProps
if props != nil {
sprops = props.StackProps
}
stack := awscdk.NewStack(scope, &id, &sprops)
// The code that defines your stack goes here
var machineDefinition = awsstepfunctions.NewPass(stack, jsii.String("PassState"), &awsstepfunctions.PassProps
{
Result: awsstepfunctions.NewResult(jsii.String("Hello")),
})
var stateMachine = awsstepfunctions.NewStateMachine(stack, jsii.String("StateMachine"), &awsstepfunctions.StateMachineProps{
Definition: machineDefinition,
StateMachineType: awsstepfunctions.StateMachineType_EXPRESS,
});
awsapigateway.NewStepFunctionsRestApi(stack, jsii.String("StepFunctionsRestApi"), &awsapigateway.StepFunctionsRestApiProps{
StateMachine = stateMachine,
})
return stack
}
func main() {
app := awscdk.NewApp(nil)
NewStepfunctionsRestApiGoStack(app, "StepfunctionsRestApiGoStack", &StepfunctionsRestApiGoStackProps{
awscdk.StackProps{
Env: env(),
},
})
app.Synth(nil)
}
// env determines the AWS environment (account+region) in which our stack is to
// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html
func env() *awscdk.Environment {
// If unspecified, this stack will be "environment-agnostic".
// Account/Region-dependent features and context lookups will not work, but a
// single synthesized template can be deployed anywhere.
//---------------------------------------------------------------------------
return nil
// Uncomment if you know exactly what account and region you want to deploy
// the stack to. This is the recommendation for production stacks.
//---------------------------------------------------------------------------
// return &awscdk.Environment{
// Account: jsii.String("123456789012"),
// Region: jsii.String("us-east-1"),
// }
// Uncomment to specialize this stack for the AWS Account and Region that are
// implied by the current CLI configuration. This is recommended for dev
// stacks.
//---------------------------------------------------------------------------
// return &awscdk.Environment{
// Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
// Region: jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
// }
}
保存源文件,然后在应用程序的主目录中发出 cdk synth
。这些区域有: AWS CDK 运行应用程序并合成一个 AWS CloudFormation 来自它的模板,然后显示该模板。
要实际部署 Amazon API Gateway 和 AWS Step Functions 状态机到你的AWS账户,问题cdk deploy
。你会被要求批准这些IAM政策 AWS CDK 已生成。
步骤 3:测试网API关
RESTAPI使用同步 Express St API ate Machine 作为后端集成创建网关后,您可以测试API网关。
使用API网关控制台测试已部署的API网关
-
打开 Amazon API Gateway 控制台并登录。
-
选择你的RESTAPI名字StepFunctionsRestApi
。
-
在资源窗格中,选择 ANY
方法。
-
选择测试选项卡。您可能需要选择右箭头按钮,以显示该选项卡。
-
对于方法,选择 POST。
-
在请求正文中,复制以下请求参数。
{
"key": "Hello"
}
-
选择测试。此时将显示以下信息:
响应正文输出应如下所示:
"Hello"
使用不同的方法和无效的输入尝试API网关,以查看错误输出。您可能希望更改状态机来查找特定的键,并在测试期间提供错误的键,以使状态机执行失败,并在响应正文输出中生成错误消息。
API使用 c 测试已部署的 URL
-
打开终端窗口。
-
复制以下 c URL 命令并将其粘贴到终端窗口中,<api-id>
替换为您APIAPI的 API ID 和<region>
您的部署区域。
curl -X POST\
'https://<api-id>
.execute-api.<region>
.amazonaws.com/prod' \
-d '{"key":"Hello"}' \
-H 'Content-Type: application/json'
响应正文输出应如下所示:
"Hello"
使用不同的方法和无效的输入尝试API网关,以查看错误输出。您可能希望更改状态机来查找特定的键,并在测试期间提供错误的键,以使状态机执行失败,并在响应正文输出中生成错误消息。
第 4 步:清除
试用完API网关后,你可以使用拆除状态机和API网关AWSCDK。在您的应用程序的主目录中发布 cdk destroy
。