title: Run AWS EMR Cluster Using Step Functions
body: <p>I am very new to AWS Step Functions and AWS Lambda Functions and could really use some help getting an EMR Cluster running through Step Functions.   A sample of my current State Machine structure is shown by the following code </p>
  
  <pre><code>{
    "Comment": "This is a test for running the structure of the CustomCreate job.",
    "StartAt": "PreStep",
    "States": {
      "PreStep": {
        "Comment": "Check that all the necessary files exist before running the job.",
        "Type": "Task",
        "Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXX:function:CustomCreate-PreStep-Function",
        "Next": "Run Job Choice"
      },
      "Run Job Choice": {
        "Comment": "This step chooses whether or not to go forward with running the main job.",
        "Type": "Choice",
        "Choices": [
          {
            "Variable": "$.FoundNecessaryFiles",
            "BooleanEquals": true,
            "Next": "Spin Up Cluster"
          },
          {
            "Variable": "$.FoundNecessaryFiles",
            "BooleanEquals": false,
            "Next": "Do Not Run Job"
          }
        ]
      },
      "Do Not Run Job": {
        "Comment": "This step triggers if the PreStep fails and the job should not run.",
        "Type": "Fail",
        "Cause": "PreStep unsuccessful"
      },
      "Spin Up Cluster": {
        "Comment": "Spins up the EMR Cluster.",
        "Type": "Pass",
        "Next": "Update Env"
      },
      "Update Env": {
        "Comment": "Update the environment variables in the EMR Cluster.",
        "Type": "Pass",
        "Next": "Run Job"
      },
      "Run Job": {
        "Comment": "Add steps to the EMR Cluster.",
        "Type": "Pass",
        "End": true
      }
    }
  }
  </code></pre>
  
  <p>Which is shown by the following workflow diagram
  <a href="https://i.stack.imgur.com/4Q2nu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Q2nu.png" alt="enter image description here"></a></p>
  
  <p>The <strong>PreStep</strong> and <strong>Run Job Choice</strong> tasks use a simple Lambda Function to check that the files necessary to run this job exist on my S3 Bucket, then go to spin up the cluster provided that the necessary files are found.  These tasks are working properly.</p>
  
  <p>What I am not sure about is how to handle the EMR Cluster related steps.</p>
  
  <p>In my current structure, the first task is to spin up an EMR Cluster. this could be done through directly using the Step Function JSON, or preferably, using a JSON Cluster Config file (titled <code>EMR-cluster-setup.json</code>) I have located on my S3 Bucket. </p>
  
  <p>My next task is to update the EMR Cluster environment variables. I have a <code>.sh</code> script located on my S3 Bucket that can do this.  I also have a JSON file (titled <code>EMR-RUN-Script.json</code>) located on my S3 Bucket that will add a first step to the EMR Cluster that will run and source the <code>.sh</code> script. I just need to run that JSON file from within the EMR Cluster, which I do not know how to do using the Step Functions. The code for <code>EMR-RUN-SCRIPT.json</code> is shown below</p>
  
  <pre><code>[
      {
          "Name": "EMR-RUN-SCRIPT",
          "ActionOnFailure": "CONTINUE",
          "HadoopJarStep": {
              "Jar": "s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar",
              "Args": [
                  "s3://PATH/TO/env_configs.sh"
              ]
          }
      }
  ]
  </code></pre>
  
  <p>My third task is to add a step that contains a spark-submit command to the EMR Cluster. This command is described in a JSON config file (titled <code>EMR-RUN-STEP.json</code>) located on my S3 Bucket that can be uploaded to the EMR Cluster in a similar manner to uploading the environment configs file in the previous step. The code for <code>EMR-RUN-STEP.json</code> is shown below</p>
  
  <pre><code>[
      {
          "Name": "EMR-RUN-STEP",
          "ActionOnFailure": "CONTINUE",
          "HadoopJarStep": {
              "Jar": "command-runner.jar",
              "Args": [
                  "bash", "-c",
                  "source /home/hadoop/.bashrc &amp;&amp; spark-submit --master yarn --conf spark.yarn.submit.waitAppCompletion=false --class CLASSPATH.TO.MAIN s3://PATH/TO/JAR/FILE"
              ]
          }
      }
  ]
  </code></pre>
  
  <p>Finally, I want to have a task that makes sure the EMR Cluster terminates after it completes its run.</p>
  
  <p>I know there may be a lot involved within this question, but I would greatly appreciate any assistance with any of the issues described above.  Whether it be following the structure I outlined above, or if you know of another solution, I am open to any form of help.  Thank you in advance.</p>