0
votes

Anyone have any ideas why I'm getting "Access Denied" when trying to put object into S3 inside a lambda function? I have the serverless AWS user with AdministorAccess and allow access to s3 resource inside serverless.yml:

  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
      Resource: "arn:aws:s3:::*"

Edit - here are the files

serverless.yml

service: testtest
app: testtest
org: workx

provider:
  name: aws
  runtime: nodejs12.x

  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:PutObject
      Resource: "arn:aws:s3:::*/*"

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: users/create
          method: get

handler.js

'use strict';
const AWS = require('aws-sdk');

// get reference to S3 client
const S3 = new AWS.S3();

// Uload the content to s3 and allow download
async function uploadToS3(content) {
  console.log('going to upload to s3!');

  const Bucket = 'mtest-exports';
    const key = 'testtest.csv';

  try {
    const destparams = {
        Bucket,
        Key: key,
        Body: content,
        ContentType: "text/csv",
    };

    console.log('going to put object', destparams);
    const putResult = await S3.putObject(destparams).promise(); 

    return putResult;
  } catch (error) {
    console.log(error);
    throw error;
  } 
}

module.exports.hello = async event => {
  const result = await uploadToS3('hello world');

  return {
    statusCode: 200,
    body: JSON.stringify(result),
  };
};
1
Try "Resource": "*"? If that doesn't help, please edit your question to include the code that is causing the error. - John Rotenstein
try this Resource: "arn:aws:s3:::*/*" - Thanh Nguyen Van
@JohnRotenstein I tried that and it didn't work - vito huang
@ThanhNguyenVan also tried your method and it didn't work - vito huang
Please edit your question to include the code that is causing the error. - John Rotenstein

1 Answers

0
votes

Turns out I was an idiot and have the custom config in the wrong place and ruin the serverless.yml file!