Skip to content

Commit 65e7944

Browse files
committed
Add SFN Hello example
1 parent 67798ff commit 65e7944

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
do {
37+
let output = try await sfnClient.listStateMachines(
38+
input: ListStateMachinesInput(
39+
maxResults: 10
40+
)
41+
)
42+
43+
guard let stateMachines = output.stateMachines else {
44+
print("*** No state machines found.")
45+
return
46+
}
47+
48+
print("Found \(stateMachines.count) state machines (capped to 10)...")
49+
for machine in stateMachines {
50+
print(" \(machine.name ?? "<unnamed>"): \(machine.stateMachineArn ?? "<unknown>")")
51+
}
52+
} catch {
53+
print("*** Error fetching state machine list: \(error.localizedDescription)")
54+
}
55+
}
56+
}
57+
58+
/// The program's asynchronous entry point.
59+
@main
60+
struct Main {
61+
static func main() async {
62+
let args = Array(CommandLine.arguments.dropFirst())
63+
64+
do {
65+
let command = try ExampleCommand.parse(args)
66+
try await command.runAsync()
67+
} catch {
68+
ExampleCommand.exit(withError: error)
69+
}
70+
}
71+
}
72+
// snippet-end:[swift.sfn.hello]

0 commit comments

Comments
 (0)