Skip to content

Commit

Permalink
Merge pull request #9 from irvinlim/aws-creds
Browse files Browse the repository at this point in the history
Pass AWS environment variables to Docker Lambda test
  • Loading branch information
irvinlim authored Nov 27, 2017
2 parents c3ff812 + 763fc89 commit 09e72d5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/
artifact.zip
artifact.zip
**.env
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"docker-lambda": "^0.13.4",
"dotenv": "^4.0.0",
"jest": "^21.2.1",
"rimraf": "^2.6.2"
},
Expand All @@ -36,7 +37,7 @@
"spec:functional": "jest spec/functional",
"test": "npm run test:build && npm run test:run",
"test:build": "npm run test:build:init && npm run test:build:js && npm run test:build:install",
"test:build:init": "cd test && npm run build:init",
"test:build:init": "cd test && rm -rf dist && mkdir dist",
"test:build:js": "cd src && babel . -d ../test/dist",
"test:build:install": "cp package.json test/dist/ && cd test/dist && npm install --production",
"test:run": "jest test/"
Expand Down
23 changes: 20 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
* Feel free to modify your Lambda function here!
*/

import AWS from 'aws-sdk';
import request from 'jsonrequest';

const lambda = async (event, context) => {
const { user, repo } = event;
const router = (event, context) => {
switch (event.type) {
case 'rest-api-example':
return restApiExample(event.data, context);
case 'aws-sdk-example':
return awsSdkExample(event.data, context);
}
};

const restApiExample = async (data, context) => {
const { user, repo } = data;

// Make a REST API call to the GitHub API.
const options = {
Expand All @@ -17,4 +27,11 @@ const lambda = async (event, context) => {
return await request(options);
};

module.exports = lambda;
const awsSdkExample = async (data, context) => {
const EC2 = new AWS.EC2();

// Sample EC2 call.
return await EC2.describeInstances().promise();
};

module.exports = router;
18 changes: 16 additions & 2 deletions test/example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ import run from './util/runner';

it('should fetch a valid repo from GitHub API successfully', function() {
const event = {
user: 'irvinlim',
repo: 'es2017-lambda-boilerplate',
type: 'rest-api-example',
data: {
user: 'irvinlim',
repo: 'es2017-lambda-boilerplate',
},
};

const result = run(event);

expect(result.owner.login).toEqual('irvinlim');
});

it('should successfully call AWS SDK', function() {
const event = {
type: 'aws-sdk-example',
};

const result = run(event);

expect(typeof result).toEqual('object');
expect(Array.isArray(result.Reservations)).toEqual(true);
});
15 changes: 12 additions & 3 deletions test/util/runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const dockerLambda = require('docker-lambda');
const fs = require('fs');
const path = require('path');
import dockerLambda from 'docker-lambda';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';

// Read environment variables from .env file.
dotenv.config();

function run(event) {
// Run the Lambda function.
Expand All @@ -13,6 +17,11 @@ function run(event) {
taskDir: path.join(__dirname, '../dist'),
// Pass an event to the Lambda function.
event,
// Pass AWS credentials from environment.
addEnvVars: {
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
},
});
} catch (err) {
// Throw errors back to test runner.
Expand Down

0 comments on commit 09e72d5

Please sign in to comment.