Build an Amazon CodeBuild Project (CodeBuild, Amazon SNS) - 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.

Build an Amazon CodeBuild Project (CodeBuild, Amazon SNS)

This sample project demonstrates how to use Amazon Step Functions to build an Amazon CodeBuild project, run tests, and then send an Amazon SNS notification.

Create the State Machine and Provision Resources

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

  2. Choose Sample Projects, and then choose Start a CodeBuild build.

    The state machine Code and Visual Workflow are displayed.

    
                        Start a workflow.
  3. Choose Next.

    The Deploy resources page is displayed, listing the resources that will be created. For this sample project, the resources include:

    • A CodeBuild project

    • An Amazon SNS topic

  4. Choose Deploy Resources.

    Note

    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.

Start a New Execution

  1. On the New execution page, enter an execution name (optional), and then choose Start Execution.

  2. (Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

  3. (Optional) Go to the newly created state machine on the Step Functions Dashboard, and then choose Start execution.

  4. When an execution is complete, select states on the Visual workflow, and browse the Input and Output under Step details.

Example State Machine Code

The state machine in this sample project integrates with CodeBuild and Amazon SNS.

Browse through this example state machine to see how Step Functions uses a state machine to build a CodeBuild project, and then sends an Amazon SNS topic with a message about whether the job succeeded or failed.

For more information about how Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.

{ "Comment": "An example of using CodeBuild to run tests, get test results and send a notification.", "StartAt": "Trigger CodeBuild Build", "States": { "Trigger CodeBuild Build": { "Type": "Task", "Resource": "arn:aws:states:::codebuild:startBuild.sync", "Parameters": { "ProjectName": "CodeBuildProject-Dtw1jBhEYGDf" }, "Next": "Get Test Results" }, "Get Test Results": { "Type": "Task", "Resource": "arn:aws:states:::codebuild:batchGetReports", "Parameters": { "ReportArns.$": "$.Build.ReportArns" }, "Next": "All Tests Passed?" }, "All Tests Passed?": { "Type": "Choice", "Choices": [ { "Variable": "$.Reports[0].Status", "StringEquals": "SUCCEEDED", "Next": "Notify Success" } ], "Default": "Notify Failure" }, "Notify Success": { "Type": "Task", "Resource": "arn:aws:states:::sns:publish", "Parameters": { "Message": "CodeBuild build tests succeeded", "TopicArn": "arn:aws:sns:sa-east-1:123456789012:StepFunctionsSample-CodeBuildExecution3da9ead6-bc1f-4441-99ac-591c140019c4-SNSTopic-EVYLVNGW85JP" }, "End": true }, "Notify Failure": { "Type": "Task", "Resource": "arn:aws:states:::sns:publish", "Parameters": { "Message": "CodeBuild build tests failed", "TopicArn": "arn:aws:sns:sa-east-1:123456789012:StepFunctionsSample-CodeBuildExecution3da9ead6-bc1f-4441-99ac-591c140019c4-SNSTopic-EVYLVNGW85JP" }, "End": true } } }

For information about how to configure IAM when using Step Functions with other Amazon services, see IAM Policies for integrated services.