title: Mock AWS API Gateway request and DynamoDB for Golang Lambda function unit Test
body: <p><strong>Setup</strong>  </p>
  
  <ul>
  <li>Windows 10</li>
  <li>go v1.10.3</li>
  <li>aws cli v1.16.67</li>
  </ul>
  
  <p><strong>What I'm trying to do</strong></p>
  
  <p>Test an AWS Lambda function written using <code>golang</code>. The function accepts a request from the <code>API Gateway</code> and then does some stuff with <code>DynamoDB</code>. Most of the below has been taken from <a href="https://www.alexedwards.net/blog/serverless-api-with-go-and-aws-lambda" rel="nofollow noreferrer">this</a> article (I'm a newbie with Go)</p>
  
  <pre class="lang-go prettyprint-override"><code>package main
  
  import (
      "encoding/json"
      "log"
      "net/http"
      "os"
      "regexp"
  
      "github.com/aws/aws-lambda-go/events"
      "github.com/aws/aws-lambda-go/lambda"
  )
  
  var uuidRegexp = regexp.MustCompile(`\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b`)
  var errorLogger = log.New(os.Stderr, "ERROR ", log.Llongfile)
  
  type job struct {
      ID                string `json:"id"`
      ClientID          string `json:"clientId"`
      Title             string `json:"title"`
      Count             int    `json:"count"`
  }
  
  // CreateJobCommand manages interactions with DynamoDB
  func CreateJobCommand(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
  
      if req.Headers["Content-Type"] != "application/json" {
          return clientError(http.StatusNotAcceptable) //406
      }
  
      newJob := new(job)
      err := json.Unmarshal([]byte(req.Body), newJob)
  
      // Ensure request has deserialized correctly
      if err != nil {
          return clientError(http.StatusUnprocessableEntity) //422
      }
  
      // Validate ID and ClientID attributes match RegEx pattern
      if !uuidRegexp.MatchString(newJob.ID) || !uuidRegexp.MatchString(newJob.ClientID) {
          return clientError(http.StatusBadRequest)
      }
  
      // Mandatory field check
      if newJob.Title == "" {
          return clientError(http.StatusBadRequest)
      }
  
      // Put item in database
      err = putItem(newJob) // putItem is defined in another file
      if err != nil {
          return serverError(err)
      }
  
      return events.APIGatewayProxyResponse{
          StatusCode: 201,
      }, nil
  }
  
  // Add a helper for handling errors. This logs any error to os.Stderr
  // and returns a 500 Internal Server Error response that the AWS API
  // Gateway understands.
  func serverError(err error) (events.APIGatewayProxyResponse, error) {
      errorLogger.Println(err.Error())
  
      return events.APIGatewayProxyResponse{
          StatusCode: http.StatusInternalServerError,
          Body:       http.StatusText(http.StatusInternalServerError),
      }, nil
  }
  
  // Similarly add a helper for send responses relating to client errors.
  func clientError(status int) (events.APIGatewayProxyResponse, error) {
      return events.APIGatewayProxyResponse{
          StatusCode: status,
          Body:       http.StatusText(status),
      }, nil
  }
  
  func putItem(job *job) error {
  
      // create an aws session
      sess := session.Must(session.NewSession(&amp;aws.Config{
          Region:   aws.String("us-east-1"),
          Endpoint: aws.String("http://localhost:8000"),
      }))
  
      // create a dynamodb instance
      db := dynamodb.New(sess)
  
      // marshal the job struct into an aws attribute value object
      jobAVMap, err := dynamodbattribute.MarshalMap(job)
      if err != nil {
          return err
      }
  
      input := &amp;dynamodb.PutItemInput{
          TableName: aws.String("TEST_TABLE"),
          Item:      jobAVMap,
      }
  
      _, err = db.PutItem(input)
      return err
  }
  
  func main() {
      lambda.Start(CreateJobCommand)
  }
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>I want to write a set of unit tests to test this function.  In my mind, the first thing I need to do is mock the API Gateway request and the DynamoDB table, but I've no idea how to do this.  </p>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>Is there a mocking framework I should be using?</li>
  <li>If anyone knows of any documentation that would help on this topic could you point it out please? (My Google skills haven't revealed any yet)</li>
  </ol>
  
  <p>Thanks</p>