# S3 as a Serverless Service

**URL:** <https://discourse.sst.dev/t/s3-as-a-serverless-service/407>\
**Category:** Chapter Comments\
**Created:** [August 18, 2018, 1:06pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407 "2018-08-18T13:06:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [August 18, 2018, 1:06pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/1 "2018-08-18T13:06:27Z")

</div>

Link to chapter - [https://serverless-stack.com/chapters/s3-as-a-serverless-service.html](https://serverless-stack.com/chapters/s3-as-a-serverless-service.html)

---

<div class="post-metadata">

**Author:** ![smokeyblues](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/smokeyblues/32/154_2.png) [@smokeyblues](https://discourse.sst.dev/u/smokeyblues)\
**Post date:** [April 29, 2019, 7:26pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/2 "2019-04-29T19:26:18Z")

</div>

Hi All,

I am trying to use this s3 serverless service by modifying the note taking app so that it compiles a users notes into a csv file and uploads it into the bucket created when this service was deployed. I have this feature defined as a new service in which the lambda function makes the query call defined in the list function from part I, iterates over the items in the query to push them into an array, joins the array with a “\n” separator, and then should take that string created by joining the elements of the array as the body of an s3.upload() call. The only problem is that it is not making that call.

Here is my function:

```auto
async function uploadNotes(csvBody) {
        const csvBuffer = Buffer.from(csvBody);
        let params = {Bucket: 'notes-app-mono-uploads-dev-mycsvbucket', Key: 'private/' + event.requestContext.identity.cognitoIdentityId + '/notes.csv', Body: csvBuffer};
        console.log("s3");
        console.log("csvBuffer: \n" + csvBuffer);
        s3.upload(params, function(err, data) {
            console.log("s3.upload function should be happening here");
            if (err) console.log(err, err.stack); // an error occurred
            else console.log(data); // successful response
        })
    }

```

I set up a mock like the list mock from part I to test this. The output returns shows the “s3” log and the csvBuffer output with all the notes from my test user but the “s3.upload function should be happening here” console.log, the err console.log and the data console.log do not show up on the output.

My try block:

```auto
try {
         const result = await dynamoDbLib.call("query", dynamoParams);
         const csvString = await gatherNotes(result.Items);
        uploadNotes(csvString);
        return success({ status: true });
    } catch (e) {
        console.log(e);
        return failure({ status: false });
    }

```

After running the invoke local command to test this, the output also shows that it has return “status: true” object indicating that the lambda call was successful.

I am somewhat new to serverless but I feel like this should work. My auth service is set up to allow the authorized user to upload to the bucket just like it allows the same user to query the notes table. The query works but the upload doesn’t.

Anyone have any idea what I am missing? Any help would be much appreciated.

All the best,

Aaron

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [May 6, 2019, 1:08am UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/3 "2019-05-06T01:08:27Z")

</div>

I’m not entirely sure what is going on here but don’t you need an `await` on the `s3.upload` call?

---

<div class="post-metadata">

**Author:** ![smokeyblues](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/smokeyblues/32/154_2.png) [@smokeyblues](https://discourse.sst.dev/u/smokeyblues)\
**Post date:** [May 29, 2019, 5:52pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/4 "2019-05-29T17:52:32Z")

</div>

Hey Jay!

Sorry about the word soup. Guess I should proofread before I post, amirite? Anyway, you were correct. That was the problem. I needed to add async in front of s3.upload. I also needed to add `.promise()` after `s3.upload(params)` call.

I ended up writing the bad code as follows:

```auto
async function uploadNotes(csvBody) {
    let csvBuffer = Buffer.from(csvBody);
    let params = {
        ACL: "public-read",
        Bucket: process.env.notesBucket,
        Key: 'private/' + event.requestContext.identity.cognitoIdentityId + '/notes.csv',
        Body: csvBuffer
    };
    try {
        const result = await s3.upload(params).promise();
        console.log(result);
    } catch (e) {
        console.log(e);
    }
}

```

I was able to find more help at [forum.serverless.com](https://forum.serverless.com/t/how-to-query-dynamodb-transform-results-and-upload-transformed-results-to-s3/8372/4).

Anyway, thanks again!

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [June 17, 2019, 5:58pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/5 "2019-06-17T17:58:12Z")

</div>

Glad you figured it out!

---

<div class="post-metadata">

**Author:** ![HHammerite](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/hhammerite/32/821_2.png) [@HHammerite](https://discourse.sst.dev/u/HHammerite)\
**Post date:** [January 13, 2020, 10:25am UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/6 "2020-01-13T10:25:08Z")

</div>

Are you able to dynamically set environment variables in serverless? For example setting an S3\_BUCKET variable where the value is Fn::ImportValue: ${self:custom.stage}-mybucket

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 26, 2020, 6:14pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/7 "2020-01-26T18:14:26Z")

</div>

Yeah AFAIK, you should be able to?

---

<div class="post-metadata">

**Author:** ![HHammerite](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/hhammerite/32/821_2.png) [@HHammerite](https://discourse.sst.dev/u/HHammerite)\
**Post date:** [January 27, 2020, 11:47am UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/8 "2020-01-27T11:47:53Z")

</div>

Hi @jayair

I’ve tried doing what I described but the variables end up being undefined.

For example I wanted to access an imported SNS topic name in my code by setting it as an environment variable like this:

```
environment:
    UPLOAD_NOTIFICATION_TOPIC_ARN:
         Fn::ImportValue: ${self:custom.stage}-UploadsProcessingNotificationArn

```

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![HHammerite](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/hhammerite/32/821_2.png) [@HHammerite](https://discourse.sst.dev/u/HHammerite)\
**Post date:** [January 27, 2020, 12:10pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/9 "2020-01-27T12:10:54Z")

</div>

I’m going to answer my own question. I was using the wrong ‘envrionment’ section of the serverless yml file. To use imported values in your envrionment variables you need to be under provider/envrionment.

```
provider:
   name: aws
   region: eu-central-1
   environment:
       UPLOAD_NOTIFICATION_TOPIC_ARN:
            Fn::ImportValue: ${self:custom.stage}-UploadsProcessingNotificationArn

```

Hope this helps someone 🙂

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [February 10, 2020, 6:51pm UTC](https://discourse.sst.dev/t/s3-as-a-serverless-service/407/10 "2020-02-10T18:51:04Z")

</div>

Glad you figured it out!
