github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/docs/lambda/aws.md (about)

     1  Interacting with AWS Services
     2  =============================
     3  
     4  The node.js and Python stacks include SDKs to interact with other AWS services.
     5  For Java you will need to include any such SDK in the JAR file.
     6  
     7  ## Credentials
     8  
     9  Running Lambda functions outside of AWS means that we cannot automatically get
    10  access to other AWS resources based on Lambda subsuming the execution role
    11  specified with the function. Instead, when using the AWS APIs inside your
    12  Lambda function (for example, to access S3 buckets), you will need to pass
    13  these credentials explicitly.
    14  
    15  ### Using environment variables for the credentials
    16  
    17  The easiest way to do this is to pass the `AWS_ACCESS_KEY_ID` and
    18  `AWS_SECRET_ACCESS_KEY` environment while creating or importing the lambda function from aws.
    19  
    20  This can be done as follows:
    21  
    22  ```sh
    23  export aws_access_key_id=<access-key>
    24  export aws_secret_access_key=<secret_key>
    25  
    26  ./fn lambda create-function <user>/s3 nodejs example.run examples/s3/example.js examples/s3/example-payload.json --config aws_access_key_id --config aws_secret_access_key
    27  ```
    28  
    29  or
    30  
    31  ```sh
    32  ./fn lambda create-function <user>/s3 nodejs example.run ../../lambda/examples/s3/example.js ../../lambda/examples/s3/example-payload.json --config aws_access_key_id=<access-key> --config aws_secret_access_key=<secret_key>
    33  ```
    34  
    35  The various AWS SDKs will automatically pick these up.
    36  
    37  ## Example: Reading and writing to S3 Bucket
    38  
    39  This example demonstrates modifying S3 buckets and using the included
    40  ImageMagick tools in a node.js function. Our function will fetch an image
    41  stored in a key specified by the event, resize it to a width of 1024px and save
    42  it to another key.
    43  
    44  The code for this example is located [here](../../examples/s3/example.js).
    45  
    46  The event will look like:
    47  
    48  ```js
    49  {
    50      "bucket": "iron-lambda-demo-images",
    51      "srcKey": "waterfall.jpg",
    52      "dstKey": "waterfall-1024.jpg"
    53  }
    54  ```
    55  
    56  The setup, imports and SDK initialization.
    57  
    58  ```js
    59  var im = require('imagemagick');
    60  var fs = require('fs');
    61  var AWS = require('aws-sdk');
    62  
    63  exports.run = function(event, context) {
    64    var bucketName = event['bucket']
    65    var srcImageKey = event['srcKey']
    66    var dstImageKey = event['dstKey']
    67  
    68    var s3 = new AWS.S3();
    69  }
    70  ```
    71  
    72  First we retrieve the source and write it to a local file so ImageMagick can
    73  work with it.
    74  
    75  ```js
    76  s3.getObject({
    77      Bucket: bucketName,
    78      Key: srcImageKey
    79    }, function (err, data) {
    80  
    81    if (err) throw err;
    82  
    83    var fileSrc = '/tmp/image-src.dat';
    84    var fileDst = '/tmp/image-dst.dat'
    85    fs.writeFileSync(fileSrc, data.Body)
    86  
    87  });
    88  ```
    89  
    90  The actual resizing involves using the identify function to get the current
    91  size (we only resize if the image is wider than 1024px), then doing the actual
    92  conversion to `fileDst`. Finally we upload to S3.
    93  
    94  ```js
    95  im.identify(fileSrc, function(err, features) {
    96    resizeIfRequired(err, features, fileSrc, fileDst, function(err, resized) {
    97      if (err) throw err;
    98      if (resized) {
    99        s3.putObject({
   100          Bucket:bucketName,
   101          Key: dstImageKey,
   102          Body: fs.createReadStream(fileDst),
   103          ContentType: 'image/jpeg',
   104          ACL: 'public-read',
   105        }, function (err, data) {
   106          if (err) throw err;
   107          context.done()
   108        });
   109      } else {
   110        context.done();
   111      }
   112    });
   113  });
   114  ```