Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Deploy and update lambda configuration with github actions
https://github.com/diegofcornejo/lambda-github-actions

Last synced: about 7 hours ago
JSON representation

Deploy and update lambda configuration with github actions

Awesome Lists containing this project

README

        

[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
![License](https://img.shields.io/github/license/diegofcornejo/lambda-github-actions?style=for-the-badge)
![Build](https://img.shields.io/github/actions/workflow/status/diegofcornejo/lambda-github-actions/main.yml?branch=master&style=for-the-badge)
[![LinkedIn][linkedin-shield]][linkedin-url]





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](https://github.com/new?template_name=lambda-github-actions&template_owner=diegofcornejo)

### Prerequisites

* Create github secrets

```sh
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)
```yml
lambda:CreateFunction
lambda:UpdateFunctionCode
lambda:UpdateFunctionConfiguration
iam:CreateRole
iam:AttachRolePolicy
iam:PassRole
```

### Installation

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

## Usage
Modify the `lambda.config.json` file with your lambda function configuration
```json
{
"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
```js
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](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) and [sam-cli](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)

* `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:
```sh
AWS_PROFILE=dev npm run deploy
```

## Roadmap

See the [open issues](https://github.com/diegofcornejo/lambda-github-actions/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](https://twitter.com/diegofcornejo) - [email protected]

Project Link: [https://github.com/diegofcornejo/lambda-github-actions](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:
```js
import { STSClient } from "@aws-sdk/client-sts"
```
is treated as a CommonJS module, and try to load using `require`:
```js
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:
```yml
npx @vercel/ncc build ./src/index.mjs -e @aws-sdk/client-sts
```
- [Vercel ncc on npm](https://www.npmjs.com/package/@vercel/ncc): ESM + Relative imports without .js extension causes "ReferenceError: require is not defined in ES module scope" [#1123](https://github.com/vercel/ncc/issues/1123)

[contributors-shield]: https://img.shields.io/github/contributors/diegofcornejo/lambda-github-actions.svg?style=for-the-badge
[contributors-url]: https://github.com/diegofcornejo/lambda-github-actions/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/diegofcornejo/lambda-github-actions.svg?style=for-the-badge
[forks-url]: https://github.com/diegofcornejo/lambda-github-actions/network/members
[stars-shield]: https://img.shields.io/github/stars/diegofcornejo/lambda-github-actions.svg?style=for-the-badge
[stars-url]: https://github.com/diegofcornejo/lambda-github-actions/stargazers
[issues-shield]: https://img.shields.io/github/issues/diegofcornejo/lambda-github-actions.svg?style=for-the-badge
[issues-url]: https://github.com/diegofcornejo/lambda-github-actions/issues
[license-shield]: https://img.shields.io/github/license/diegofcornejo/lambda-github-actions.svg?style=for-the-badge
[license-url]: https://github.com/diegofcornejo/lambda-github-actions/blob/master/LICENSE.txt
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
[linkedin-url]:https://www.linkedin.com/in/diego-cornejo-devops-sre/