Gather Amazon S3 bucket info using Amazon SDK service integrations - Amazon Step Functions
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

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.

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.

  1. Open the Step Functions console and choose Create state machine.

  2. 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.

  3. 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 } } }
  4. Choose Next.

  5. 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.

  6. 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.

  1. On the state machine page, choose IAM role ARN to open the Roles page for the state machine role.

  2. Choose Add permissions and then choose Create inline policy.

  3. 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": "*" } ] }
  4. Choose Review policy.

  5. Under Review policy, for the policy Name, enter s3-bucket-permissions.

  6. Choose Create policy.

Step 3: Run a Standard state machine execution

  1. In the Step Functions console, on the State machines page, choose Gather-S3-Bucket-Info-Standard.

  2. Choose Start execution.

  3. On the Start execution dialog box, choose Start execution.

    The Gather-S3-Bucket-Info-Standard state machine's execution starts.

  4. 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

  1. 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.

  2. On the State machines page, choose Gather-S3-Bucket-Info-Express.

  3. Choose Start execution.

  4. On the Start execution dialog box, choose Start execution.

    The Gather-S3-Bucket-Info-Express state machine's execution starts.

  5. After the execution finishes, you can view the results in the Execution input and output tab of the Execution summary section.