Gather Amazon S3 bucket info using Amazon SDK service integrations
This tutorial shows you how to perform an Amazon SDK integration with Amazon Simple Storage Service. The state machine you create in this tutorial gathers information about your Amazon S3 buckets, then list your buckets along with version information for each bucket in the current region.
Topics
Step 1: Create the state machine
Using the Step Functions console, you'll create a state machine that includes a Task
state to list all the Amazon S3 buckets in the current account and region. Then, you'll add another Task
state that invokes the HeadBucket
API to verify if the returned bucket is accessible in the current region. If the bucket isn't accessible, the HeadBucket
API call returns the S3.S3Exception
error. You'll include a Catch
block to catch this exception and a Pass
state as the fallback state.
Open the Step Functions console
and choose Create state machine. -
On the Define state machine page, choose Write your workflow in code and keep the default selection for Type as Standard.
After you've run the Standard state machine, you can create another state machine of type Express and run it.
-
Copy and paste the following state machine definition into the Definition section.
{ "Comment": "A description of my state machine", "StartAt": "ListBuckets", "States": { "ListBuckets": { "Type": "Task", "Parameters": {}, "Resource": "arn:aws-cn:states:::aws-sdk:s3:listBuckets", "Next": "Map" }, "Map": { "Type": "Map", "ItemsPath": "$.Buckets", "ItemProcessor": { "ProcessorConfig": { "Mode": "INLINE" }, "StartAt": "HeadBucket", "States": { "HeadBucket": { "Type": "Task", "ResultPath": null, "Parameters": { "Bucket.$": "$.Name" }, "Resource": "arn:aws-cn:states:::aws-sdk:s3:headBucket", "Catch": [ { "ErrorEquals": [ "S3.S3Exception" ], "ResultPath": null, "Next": "Pass" } ], "Next": "GetBucketVersioning" }, "GetBucketVersioning": { "Type": "Task", "End": true, "Parameters": { "Bucket.$": "$.Name" }, "ResultPath": "$.BucketVersioningInfo", "Resource": "arn:aws-cn:states:::aws-sdk:s3:getBucketVersioning" }, "Pass": { "Type": "Pass", "End": true, "Result": { "Status": "Unknown" }, "ResultPath": "$.BucketVersioningInfo" } } }, "End": true } } }
-
Choose Next.
-
On the Specify details page, specify details for the new state machine, such as a name and logging level. For example, enter the state machine name as
Gather-S3-Bucket-Info-Standard
.In Step 2, you'll add the missing permissions to the state machine role.
-
Choose Create state machine
Step 2: Add the necessary IAM role permissions
To gather information about the Amazon S3 buckets in your current region, you must provide your state machine the necessary permissions to access the Amazon S3 buckets.
-
On the state machine page, choose IAM role ARN to open the Roles page for the state machine role.
-
Choose Add permissions and then choose Create inline policy.
Choose the JSON tab, and then paste the following permissions into the JSON editor.
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "s3:ListBucket", "s3:GetBucketVersioning" ], "Resource": "*" } ] }
Choose Review policy.
Under Review policy, for the policy Name, enter
s3-bucket-permissions
.Choose Create policy.
Step 3: Run a Standard state machine execution
-
In the Step Functions console
, on the State machines page, choose Gather-S3-Bucket-Info-Standard. -
Choose Start execution.
-
On the Start execution dialog box, choose Start execution.
The Gather-S3-Bucket-Info-Standard state machine's execution starts.
-
After the execution finishes, you can view the results in the Execution input and output tab of the Execution summary section.
Step 4: Run an Express state machine execution
-
Create an Express state machine using the state machine definition provided in Step 1. Make sure that you also include the necessary IAM role permissions as explained in Step 2.
Tip
To distinguish from the Standard machine you created earlier, name the Express state machine as
Gather-S3-Bucket-Info-Express
. -
On the State machines page, choose Gather-S3-Bucket-Info-Express.
-
Choose Start execution.
-
On the Start execution dialog box, choose Start execution.
The Gather-S3-Bucket-Info-Express state machine's execution starts.
-
After the execution finishes, you can view the results in the Execution input and output tab of the Execution summary section.