Skip to content

Commit ed8b1c4

Browse files
committed
Swift Step Functions Hello and Scenario
1 parent 5ee4ae9 commit ed8b1c4

File tree

8 files changed

+1113
-0
lines changed

8 files changed

+1113
-0
lines changed

swift/example_code/sfn/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Step Functions code examples for the SDK for Swift
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Swift to work with AWS Step Functions.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Step Functions makes it easy to coordinate the components of distributed applications as a series of steps in a visual workflow._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Basics
33+
34+
Code examples that show you how to perform the essential operations within a service.
35+
36+
- [Learn the basics](scenario/Sources/entry.swift)
37+
38+
39+
### Single actions
40+
41+
Code excerpts that show you how to call individual service functions.
42+
43+
- [CreateActivity](scenario/Sources/Activity.swift#L42)
44+
- [CreateStateMachine](scenario/Sources/StateMachine.swift#L133)
45+
- [DeleteActivity](scenario/Sources/Activity.swift#L102)
46+
- [DeleteStateMachine](scenario/Sources/StateMachine.swift#L308)
47+
- [DescribeExecution](scenario/Sources/StateMachine.swift#L263)
48+
- [DescribeStateMachine](scenario/Sources/StateMachine.swift#L180)
49+
- [GetActivityTask](scenario/Sources/StateMachine.swift#L228)
50+
- [ListActivities](scenario/Sources/Activity.swift#L42)
51+
- [ListStateMachines](scenario/Sources/StateMachine.swift#L105)
52+
- [SendTaskSuccess](scenario/Sources/Activity.swift#L115)
53+
- [StartExecution](scenario/Sources/StateMachine.swift#L205)
54+
55+
56+
<!--custom.examples.start-->
57+
<!--custom.examples.end-->
58+
59+
## Run the examples
60+
61+
### Instructions
62+
63+
To build any of these examples from a terminal window, navigate into its
64+
directory, then use the following command:
65+
66+
```
67+
$ swift build
68+
```
69+
70+
To build one of these examples in Xcode, navigate to the example's directory
71+
(such as the `ListUsers` directory, to build that example). Then type `xed.`
72+
to open the example directory in Xcode. You can then use standard Xcode build
73+
and run commands.
74+
75+
<!--custom.instructions.start-->
76+
<!--custom.instructions.end-->
77+
78+
79+
#### Learn the basics
80+
81+
This example shows you how to do the following:
82+
83+
- Create an activity.
84+
- Create a state machine from an Amazon States Language definition that contains the previously created activity as a step.
85+
- Run the state machine and respond to the activity with user input.
86+
- Get the final status and output after the run completes, then clean up resources.
87+
88+
<!--custom.basic_prereqs.sfn_Scenario_GetStartedStateMachines.start-->
89+
<!--custom.basic_prereqs.sfn_Scenario_GetStartedStateMachines.end-->
90+
91+
92+
<!--custom.basics.sfn_Scenario_GetStartedStateMachines.start-->
93+
<!--custom.basics.sfn_Scenario_GetStartedStateMachines.end-->
94+
95+
96+
### Tests
97+
98+
⚠ Running tests might result in charges to your AWS account.
99+
100+
101+
To find instructions for running these tests, see the [README](../../README.md#Tests)
102+
in the `swift` folder.
103+
104+
105+
106+
<!--custom.tests.start-->
107+
<!--custom.tests.end-->
108+
109+
## Additional resources
110+
111+
- [Step Functions Developer Guide](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)
112+
- [Step Functions API Reference](https://docs.aws.amazon.com/step-functions/latest/apireference/Welcome.html)
113+
- [SDK for Swift Step Functions reference](https://sdk.amazonaws.com/swift/api/awssfn/latest/documentation/awssfn)
114+
115+
<!--custom.resources.start-->
116+
<!--custom.resources.end-->
117+
118+
---
119+
120+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
121+
122+
SPDX-License-Identifier: Apache-2.0
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.sfn.hello.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "hello-sfn",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "hello-sfn",
39+
dependencies: [
40+
.product(name: "AWSSFN", package: "aws-sdk-swift"),
41+
.product(name: "ArgumentParser", package: "swift-argument-parser")
42+
],
43+
path: "Sources")
44+
45+
]
46+
)
47+
// snippet-end:[swift.sfn.hello.package]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// snippet-start:[swift.sfn.hello]
5+
// An example that shows how to use the AWS SDK for Swift to perform a simple
6+
// operation using Amazon Elastic Compute Cloud (EC2).
7+
//
8+
9+
import ArgumentParser
10+
import Foundation
11+
12+
// snippet-start:[swift.sfn.import]
13+
import AWSSFN
14+
// snippet-end:[swift.sfn.import]
15+
16+
struct ExampleCommand: ParsableCommand {
17+
@Option(help: "The AWS Region to run AWS API calls in.")
18+
var awsRegion = "us-east-1"
19+
20+
static var configuration = CommandConfiguration(
21+
commandName: "hello-sfn",
22+
abstract: """
23+
Demonstrates a simple operation using AWS Step Functions.
24+
""",
25+
discussion: """
26+
An example showing how to make a call to AWS Step Functions using the
27+
AWS SDK for Swift.
28+
"""
29+
)
30+
31+
/// Called by ``main()`` to run the bulk of the example.
32+
func runAsync() async throws {
33+
let sfnConfig = try await SFNClient.SFNClientConfiguration(region: awsRegion)
34+
let sfnClient = SFNClient(config: sfnConfig)
35+
36+
// snippet-start:[swift.sfn.hello.ListStateMachines]
37+
do {
38+
let output = try await sfnClient.listStateMachines(
39+
input: ListStateMachinesInput(
40+
maxResults: 10
41+
)
42+
)
43+
44+
guard let stateMachines = output.stateMachines else {
45+
print("*** No state machines found.")
46+
return
47+
}
48+
49+
print("Found \(stateMachines.count) state machines (capped to 10)...")
50+
for machine in stateMachines {
51+
print(" \(machine.name ?? "<unnamed>"): \(machine.stateMachineArn ?? "<unknown>")")
52+
}
53+
} catch {
54+
print("*** Error fetching state machine list: \(error.localizedDescription)")
55+
}
56+
// snippet-end:[swift.sfn.hello.ListStateMachines]
57+
}
58+
}
59+
60+
/// The program's asynchronous entry point.
61+
@main
62+
struct Main {
63+
static func main() async {
64+
let args = Array(CommandLine.arguments.dropFirst())
65+
66+
do {
67+
let command = try ExampleCommand.parse(args)
68+
try await command.runAsync()
69+
} catch {
70+
ExampleCommand.exit(withError: error)
71+
}
72+
}
73+
}
74+
// snippet-end:[swift.sfn.hello]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// swift-tools-version: 5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// (swift-tools-version has two lines here because it needs to be the first
6+
// line in the file, but it should also appear in the snippet below)
7+
//
8+
// snippet-start:[swift.sfn.hello.package]
9+
// swift-tools-version: 5.9
10+
//
11+
// The swift-tools-version declares the minimum version of Swift required to
12+
// build this package.
13+
14+
import PackageDescription
15+
16+
let package = Package(
17+
name: "sfn-scenario",
18+
// Let Xcode know the minimum Apple platforms supported.
19+
platforms: [
20+
.macOS(.v13),
21+
.iOS(.v15)
22+
],
23+
dependencies: [
24+
// Dependencies declare other packages that this package depends on.
25+
.package(
26+
url: "https://github.com/awslabs/aws-sdk-swift",
27+
from: "1.0.0"),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
branch: "main"
31+
)
32+
],
33+
targets: [
34+
// Targets are the basic building blocks of a package, defining a module or a test suite.
35+
// Targets can depend on other targets in this package and products
36+
// from dependencies.
37+
.executableTarget(
38+
name: "sfn-scenario",
39+
dependencies: [
40+
.product(name: "AWSIAM", package: "aws-sdk-swift"),
41+
.product(name: "AWSSFN", package: "aws-sdk-swift"),
42+
.product(name: "ArgumentParser", package: "swift-argument-parser")
43+
],
44+
path: "Sources")
45+
46+
]
47+
)
48+
// snippet-end:[swift.sfn.hello.package]

0 commit comments

Comments
 (0)