Deploy AWS lambda functions using CI/CD github actions
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Clone the repository or you can use the template
- Create github secrets
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
Note: Your credentials must have permissions to deploy lambda functions and create roles.(needed for the first deploy if you don't have a role)
lambda:CreateFunction
lambda:UpdateFunctionCode
lambda:UpdateFunctionConfiguration
iam:CreateRole
iam:AttachRolePolicy
iam:PassRole
- Clone the repo
git clone https://github.com/diegofcornejo/lambda-github-actions.git
- Install NPM packages
npm install
Modify the lambda.config.json
file with your lambda function configuration
{
"FunctionName": "lambda-github-actions",
"Description": "Lambda CI with github actions",
"Role":"GithubActionsRole",
"Handler":"index.handler",
"Runtime":"nodejs20.x",
"Timeout": 10,
"MemorySize": 256
}
Modify the index.mjs
file with your lambda function code
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
export const handler = async (event) => {
const done = (statusCode, body) => {
return {
statusCode,
body: JSON.stringify(body) // body must be string
};
}
try {
// AWS SDK v3 example
const stsClient = new STSClient({ region: 'us-east-1' });
const stsCommand = new GetCallerIdentityCommand({});
const stsResponse = await stsClient.send(stsCommand);
console.log("🚀 ~ file: index.mjs:9 ~ handler ~ stsResponse:", stsResponse)
// NodeJS 20 native support for fetch API - POKEAPI example
const pokeResponse = await fetch('https://pokeapi.co/api/v2/pokemon/ditto');
const ditto = await pokeResponse.json();
console.log("🚀 ~ file: index.mjs:14 ~ handler ~ ditto:", ditto)
// Return all examples in response
const res = {
message: 'AWS Lambda CI/CD with Github Actions',
event,
awsSdk: stsResponse,
pokeApi: ditto
}
return done(200, res)
} catch (error) {
console.error("🚀 ~ file: index.mjs:27 ~ handler ~ error", error)
return done(500, error)
}
};
And finally push your changes to the repository, the github action will deploy your lambda function, you can see the logs in the actions tab.
If you need you can modify the github action workflow file .github/workflows/main.yml
with your own configuration.
npm run build
- Build the lambda function using@vercel/ncc
packagenpm run build:min
- Build the lambda function using@vercel/ncc
package with minify optionnpm run build:zip
- Build the lambda function using@vercel/ncc
package and create a zip filenpm run deploy
- Deploy the lambda function usingaws-cli
, in case you need to deploy the lambda function manually without github actionsnpm run invoke
- Invoke the lambda function usingaws-cli
npm run invoke:local
- Invoke the lambda function usingsam-cli
npm run get:info
- Get the lambda function info usingaws-cli
npm run get:url
- Get the lambda function url usingaws-cli
If you need to execute any of these (aws) scripts with a differente aws profile, you can use the AWS_PROFILE
environment variable, for example:
AWS_PROFILE=dev npm run deploy
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Diego Cornejo - @diegofcornejo - diegof.cornejo@gmail.com
Project Link: https://github.com/diegofcornejo/lambda-github-actions
- Vercel ncc (@vercel/ncc)
-
@vercel/ncc
does not work correctly with preinstalledaws-sdk
package, for example:import { STSClient } from "@aws-sdk/client-sts"
is treated as a CommonJS module, and try to load using
require
:module.exports = eval("require")("@aws-sdk/client-sts");
To avoid this issue, modify the
main.yml
and add any aws-sdk package with the-e
flag to exclude it from the build.For example, if you need to use
@aws-sdk/client-sts
, you can use the following command:npx @vercel/ncc build ./src/index.mjs -e @aws-sdk/client-sts
-
Vercel ncc on npm: ESM + Relative imports without .js extension causes "ReferenceError: require is not defined in ES module scope" #1123
-