Skip to content

Instantly share code, notes, and snippets.

@nicholasrq
Last active January 25, 2021 21:48
Show Gist options
  • Save nicholasrq/d769a8b10c8267547530d9f0885d3288 to your computer and use it in GitHub Desktop.
Save nicholasrq/d769a8b10c8267547530d9f0885d3288 to your computer and use it in GitHub Desktop.
Lamda Directory structure
const { exec, execSync } = require('child_process');
const FS = require('fs');
const Path = require('path');
const execCommand = (command) => new Promise((resolve, reject) => {
exec(command, (err, stdout) => {
if (err) return reject(err);
console.log(stdout);
return resolve(stdout);
}, {stdio: 'inherit'});
});
// List all the files and folders in current directory
const listDirs = FS.readdirSync(__dirname)
// Add any folders that you want to ignore
const ignored = ['node_modules'];
// Loop over directories
listDirs.forEach(async (dirPath) => {
if (ignored.includes(dirPath)) return;
// Full path to a function dir
const fullPath = Path.resolve(__dirname, dirPath)
if (FS.statSync(fullPath).isDirectory()) {
// Name for the zip archive
const zipName = `${dirPath}.zip`
// Full path to the archive
const zipPath = Path.resolve(fullPath, zipName);
// Install modules
await execCommand(`cd ${fullPath} && npm ci`);
// Pack everything into the zip
await execCommand(`cd ${fullPath} && zip -rqX ${zipName} *`);
// Upload to aws
await execCommand(`aws lambda update-function-code --function-name ${dirPath} --zip-file fileb://${zipPath}`);
// Remove the archive
await execCommand(`rm ${zipPath}`);
}
})
. App root
├── some other files and folders
└── lambda
├── [lambda-function-1]
├── [lambda-function-2]
├── [lambda-function-3]
├── lambda-deployment-script.js
└── package.json
# Name of the workflow
name: Deploy lambda functions to AWS
# Triggers that invoke the action
# We're triggering on every commit to `lambda` branch
on:
push:
branches: [ lambda ]
# Actual jobs to run during the workflow
jobs:
deploy:
# Here we're specifying the platform on
# which we're gonna run our actions
runs-on: ubuntu-latest
steps:
# First checkout the branch
- name: Checkout
uses: actions/checkout@v2
# Next we need Node.js to run the deployment script
- name: Setting up Node.js environment
# This is a special action that installs a
# specified version(s) of Node.js
uses: actions/setup-node@v1
# In our case we're using Node.js v12.x
with:
node-version: '12'
# Every github action goes with pre-installed aws-cli
# so we don't need to install it manually. Yet, we need to
# tell it how to authenticate us in the system. To do so
# aws-cli supports special environment variables:
env:
# Here you should set a region where your Lambda live within AWS
# You can store it in secrets as well if you don't want to expose the region
AWS_DEFAULT_REGION: eu-west-1
# These come from Github Secrets that we've added earlier
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY_ID }}
# Now let's install all the node modules
- run: cd ./lambda && npm ci
# And execute the deployment script
- run: cd ./lambda && node ./deploy-lambda.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment