title: how to fix "ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden" when trying to download file in AWS Lambda function
body: <p>I'm getting <code>"errorMessage": "An error occurred (403) when calling the HeadObject operation: Forbidden"</code> when trying to call <code>s3.download_file</code> (see code below).</p>
  
  <p>I've looked at numerous posts, but none of them seem to be called from AWS Lambda which is where my issue resides.</p>
  
  <p>My code works locally on my machine so I'm guessing the issue involves Lambda. This Lambda function will eventually be triggered by an S3 event, but during testing, it does not work.</p>
  
  <p>My IAM user has full access to S3 and Lambda.</p>
  
  <p>AWS Lambda function code:</p>
  
  <pre><code>import json
  import boto3
  import logging
  
  log = logging.getLogger()
  log.setLevel(logging.INFO)
  
  s3 = boto3.client('s3')
  
  
  def lambda_handler(event, context):
      document_name = event['Records'][0]['s3']['object']['key']
      bucket = event['Records'][0]['s3']['bucket']['name']
  
      log.info('Performing stuff on {}/{}'.format(bucket, document_name))
  
      # download the file
      s3.download_file(
          Bucket=bucket,
          Key=document_name,
          Filename=document_name
      )
  
      log.info('here it is!')
  
      return event
  
  </code></pre>
  
  <p>(I even tried entering the information into <code>s3.download_file</code> explicitly instead of grabbing it from <code>event</code>, but to no avail. The log message confirms that it's the correct path.)</p>
  
  <p>the parameters passed in the test event config:</p>
  
  <pre><code>...
  "s3": {
    ...
    "bucket": {
      "name": "bucket-name",
    ...
    "object": {
      "key": "filename.pdf",
  </code></pre>
  
  <p>Code on local machine:</p>
  
  <pre><code>import boto3
  
  s3 = boto3.client('s3')
  
  s3.download_file(
    Bucket='bucket-name',
    Key='filename.pdf',
    Filename='filename.pdf'
  )
  </code></pre>
  
  <p>I expected the file to be downloaded without error, as it was when I tested the code on my machine.</p>