Skip to content

Deploy and update lambda configuration with github actions

License

Notifications You must be signed in to change notification settings

diegofcornejo/lambda-github-actions

Repository files navigation

Contributors Forks Stargazers Issues License Build LinkedIn


Logo

Lambda Github Actions

Deploy AWS lambda functions using CI/CD github actions
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents

  1. Getting Started
  2. Usage
  3. Scripts
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

Getting Started

Clone the repository or you can use the template

Prerequisites

  • 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

Installation

  1. Clone the repo
    git clone https://github.com/diegofcornejo/lambda-github-actions.git
  2. Install NPM packages
    npm install

Usage

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.

Scripts

This assumes you have installed and configured the aws-cli and sam-cli

  • npm run build - Build the lambda function using @vercel/ncc package
  • npm run build:min - Build the lambda function using @vercel/ncc package with minify option
  • npm run build:zip - Build the lambda function using @vercel/ncc package and create a zip file
  • npm run deploy - Deploy the lambda function using aws-cli, in case you need to deploy the lambda function manually without github actions
  • npm run invoke - Invoke the lambda function using aws-cli
  • npm run invoke:local - Invoke the lambda function using sam-cli
  • npm run get:info - Get the lambda function info using aws-cli
  • npm run get:url - Get the lambda function url using aws-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

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Diego Cornejo - @diegofcornejo - diegof.cornejo@gmail.com

Project Link: https://github.com/diegofcornejo/lambda-github-actions

Acknowledgements

  • Vercel ncc (@vercel/ncc)
    • @vercel/ncc does not work correctly with preinstalled aws-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

About

Deploy and update lambda configuration with github actions

Resources

License

Stars

Watchers

Forks

Packages

No packages published