我可以在状态机定义文件中使用全局变量吗?
Can I use Global variables in State Machine definition files?
我正在使用 AWS SAM 开发一个无服务器项目,它由 lambda 和状态机资源组成(请参阅下面的 SAM 模板)。我想知道是否有一种方法可以在我的状态机定义文件中定义一个“全局”变量,就像我们如何为 SAM 模板使用 Globals 一样。
我想这样做的原因是为了缩短我的状态机定义。现在有状态使用完全相同的属性,例如我的 Fargate 任务的 NetworkConfiguration
块:
definition.asl.json
{
"StartAt": "Generate Config",
"States": {
"Generate Config": {
"Type": "Task",
"Resource": "${GenerateConfigFunctionArn}",
"Next": "ETL"
},
"ETL": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Next": "ETL",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
"TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-00d610225d369486e"
],
"SecurityGroups": [
"sg-016650ff9ecd28a9a"
],
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "Phase-1-start",
"Environment": [
{
"Name": "COMMAND",
"Value": "etl"
}
]
}
]
}
}
},
"Well_Calc": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"End": true,
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
"TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-00d610225d369486e"
],
"SecurityGroups": [
"sg-016650ff9ecd28a9a"
],
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "Phase-1-start",
"Environment": [
{
"Name": "COMMAND",
"Value": "calculation"
}
]
}
]
}
}
}
}
}
SAM template
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
MySAMPOC:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: stateMachine/definition.asl.json
DefinitionSubstitutions:
GenerateConfigFunctionArn: !GetAtt GenerateConfigFunction.Arn
GenerateConfigFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/generate_config
Handler: app.lambda_handler
Runtime: python3.8
状态机定义是否支持使用全局变量(或类似变量)?
AWS::Serverless::StateMachine
目前不支持全局变量。您可以考虑的一些备选方案:
- 对某些值使用
DefinitionSubstitutions
:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionsubstitutions
- Step Function 现在在开发工具中支持 YAML,因此从 JSON 切换到 YAML 至少会节省一些缩进和大括号。 https://aws.amazon.com/about-aws/whats-new/2021/03/aws-step-functions-adds-tooling-support-for-yaml/
- 使用 CDK 定义您的状态机。您可以创建封装通用属性的自定义结构。显然这不是 SAM,但您可以将 SAM 资源与 CDK 应用程序集成,或者考虑将您的部分应用程序迁移到 CDK。 https://docs.aws.amazon.com/cdk/api/latest/docs/aws-stepfunctions-readme.html
我正在使用 AWS SAM 开发一个无服务器项目,它由 lambda 和状态机资源组成(请参阅下面的 SAM 模板)。我想知道是否有一种方法可以在我的状态机定义文件中定义一个“全局”变量,就像我们如何为 SAM 模板使用 Globals 一样。
我想这样做的原因是为了缩短我的状态机定义。现在有状态使用完全相同的属性,例如我的 Fargate 任务的 NetworkConfiguration
块:
definition.asl.json
{
"StartAt": "Generate Config",
"States": {
"Generate Config": {
"Type": "Task",
"Resource": "${GenerateConfigFunctionArn}",
"Next": "ETL"
},
"ETL": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Next": "ETL",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
"TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-00d610225d369486e"
],
"SecurityGroups": [
"sg-016650ff9ecd28a9a"
],
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "Phase-1-start",
"Environment": [
{
"Name": "COMMAND",
"Value": "etl"
}
]
}
]
}
}
},
"Well_Calc": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"End": true,
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
"TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"subnet-00d610225d369486e"
],
"SecurityGroups": [
"sg-016650ff9ecd28a9a"
],
"AssignPublicIp": "ENABLED"
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "Phase-1-start",
"Environment": [
{
"Name": "COMMAND",
"Value": "calculation"
}
]
}
]
}
}
}
}
}
SAM template
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
MySAMPOC:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: stateMachine/definition.asl.json
DefinitionSubstitutions:
GenerateConfigFunctionArn: !GetAtt GenerateConfigFunction.Arn
GenerateConfigFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/generate_config
Handler: app.lambda_handler
Runtime: python3.8
状态机定义是否支持使用全局变量(或类似变量)?
AWS::Serverless::StateMachine
目前不支持全局变量。您可以考虑的一些备选方案:
- 对某些值使用
DefinitionSubstitutions
:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionsubstitutions - Step Function 现在在开发工具中支持 YAML,因此从 JSON 切换到 YAML 至少会节省一些缩进和大括号。 https://aws.amazon.com/about-aws/whats-new/2021/03/aws-step-functions-adds-tooling-support-for-yaml/
- 使用 CDK 定义您的状态机。您可以创建封装通用属性的自定义结构。显然这不是 SAM,但您可以将 SAM 资源与 CDK 应用程序集成,或者考虑将您的部分应用程序迁移到 CDK。 https://docs.aws.amazon.com/cdk/api/latest/docs/aws-stepfunctions-readme.html