Skip to content

Commit cb35a94

Browse files
authored
Merge pull request #13 from irvinlim/add-deploy-script
Add deployment npm script (#13)
2 parents 6d2bf5a + 5162f8f commit cb35a94

6 files changed

Lines changed: 257 additions & 4 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ before_script:
1313
script:
1414
- npm run spec
1515
- npm run test
16+
- npm run deploy
1617
after_script:
1718
- greenkeeper-lockfile-upload

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _Note: Only features which are not normally available on AWS Lambda Node.js 6.10
2626

2727
Edit your Lambda function under `src/main.js`, and run:
2828

29-
```js
29+
```sh
3030
npm run package
3131
```
3232

@@ -36,7 +36,7 @@ This will create an `artifact.zip` file which you can upload to AWS Lambda.
3636

3737
You can run automated tests for your Lambda function inside of a Docker container using [docker-lambda](https://github.com/lambci/docker-lambda):
3838

39-
```js
39+
```sh
4040
npm run test
4141
```
4242

@@ -55,13 +55,32 @@ You can find the spec tests under `spec/functional` and `spec/snapshot` respecti
5555

5656
If you are not going to modify `.babelrc`, you can choose to skip these tests by omitting the `npm run spec` script in `.travis.yml`. This will help to speed up your builds by a bit.
5757

58+
## Deployment
59+
60+
You can automatically deploy to AWS Lambda locally or through CI (e.g. Travis), as long as you provide an access key for an IAM user that has write access to AWS Lambda.
61+
62+
```sh
63+
npm run deploy
64+
```
65+
66+
See [Environment variables](#environment-variables) for the list of environment variables that are required for deployment.
67+
5868
## Using the AWS SDK
5969

6070
You can write Lambda functions that make use of the [AWS SDK](https://github.com/aws/aws-sdk-js) by simply `import`-ing `aws-sdk`. The package is installed globally within the AWS Lambda environment, so you don't need to add it to your `package.json`.
6171

62-
Additionally, you can pass in the access keys for the AWS IAM user to be used during automated tests through the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. This will work if you store it in a `.env` file in the root of the project (see [dotenv](https://github.com/motdotla/dotenv)), or if you define it within Travis CI itself (see [Travis docs](https://docs.travis-ci.com/user/environment-variables/)).
72+
Also make sure that your function has Internet connectivity (i.e. not within a VPC without a NAT gateway). The `internetConnectivityTest.js` utility is included to help to debug such problems early when deploying to AWS Lambda.
73+
74+
## Environment variables
75+
76+
The following environment variables are supported:
77+
78+
* `AWS_ACCESS_KEY_ID`: IAM user access key ID
79+
* `AWS_SECRET_ACCESS_KEY`: IAM user secret access key
80+
* `AWS_REGION`: AWS region where the Lambda function resides in
81+
* `LAMBDA_FUNCTION_NAME`: Name or ARN of the Lambda function
6382

64-
Finally, make sure that your function has Internet connectivity (i.e. not within a VPC without a NAT gateway). The `internetConnectivityTest.js` utility is included to help to debug such problems early when deploying to AWS Lambda.
83+
This will work if you store it in a `.env` file in the root of the project (see [dotenv](https://github.com/motdotla/dotenv)), or if you define it within Travis CI itself (see [Travis docs](https://docs.travis-ci.com/user/environment-variables/)).
6584

6685
## Why?
6786

bin/deploy.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Simple script that deploys the zipped package to AWS Lambda.
3+
*/
4+
5+
import AWS from 'aws-sdk';
6+
import dotenv from 'dotenv';
7+
import fs from 'fs-extra';
8+
import path from 'path';
9+
import print from './utils/print';
10+
11+
const errorExit = msg => {
12+
print.error(msg);
13+
return process.exit(1);
14+
};
15+
16+
(async function() {
17+
// Fetches AWS credentials and Lambda details from the .env file.
18+
dotenv.config();
19+
20+
// Validate if required environment variables are provided.
21+
const requiredEnv = [
22+
'AWS_ACCESS_KEY_ID',
23+
'AWS_SECRET_ACCESS_KEY',
24+
'AWS_REGION',
25+
'LAMBDA_FUNCTION_NAME',
26+
];
27+
28+
for (let env of requiredEnv) {
29+
if (!process.env[env]) {
30+
errorExit(`Missing environment variable: ${env}`);
31+
}
32+
}
33+
34+
const {
35+
AWS_REGION: region,
36+
AWS_ACCESS_KEY_ID: accessKeyId,
37+
AWS_SECRET_ACCESS_KEY: secretAccessKey,
38+
LAMBDA_FUNCTION_NAME: functionName,
39+
} = process.env;
40+
41+
// Update the AWS credentials and region.
42+
AWS.config.update({ region, accessKeyId, secretAccessKey });
43+
44+
const artifactZipPath = path.join(__dirname, '../artifact.zip');
45+
let artifactZipBuf;
46+
47+
try {
48+
artifactZipBuf = await fs.readFile(artifactZipPath);
49+
} catch (err) {
50+
errorExit(err.message);
51+
}
52+
53+
const Lambda = new AWS.Lambda();
54+
const params = {
55+
FunctionName: functionName,
56+
ZipFile: artifactZipBuf,
57+
};
58+
59+
// Update the Lambda function code.
60+
try {
61+
await Lambda.updateFunctionCode(params).promise();
62+
} catch (err) {
63+
errorExit(err.message);
64+
}
65+
66+
print.success('Successfully uploaded to AWS Lambda!');
67+
})();

bin/utils/print.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import colors from 'colors';
2+
3+
colors.setTheme({
4+
error: 'red',
5+
success: 'green',
6+
});
7+
8+
const error = msg => {
9+
console.error(`ERROR: ${msg}`.red);
10+
};
11+
12+
const success = msg => {
13+
console.log(msg.success);
14+
};
15+
16+
export default {
17+
error,
18+
success,
19+
};

package-lock.json

Lines changed: 142 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)