Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/diegofcornejo/sdk-storage-s3-presigned-post-v1
SDK - Storage - S3 Upload
https://github.com/diegofcornejo/sdk-storage-s3-presigned-post-v1
Last synced: about 7 hours ago
JSON representation
SDK - Storage - S3 Upload
- Host: GitHub
- URL: https://github.com/diegofcornejo/sdk-storage-s3-presigned-post-v1
- Owner: diegofcornejo
- License: mit
- Created: 2023-12-06T00:52:51.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-12-07T06:43:37.000Z (12 months ago)
- Last Synced: 2024-03-15T11:11:07.545Z (8 months ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.txt
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]
Lambda Github Actions
Deploy AWS lambda functions using CI/CD github actions
Explore the docs ยป
View Demo
ยท
Report Bug
ยท
Request Feature
Table of Contents
## 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.
## S3 Upload HTML Form
```htmlS3 Upload - HTML Form
File:
```
## S3 Upload Javascript Example
```js
// This is a simple example of how to upload a file to S3 using a presigned URL// First, we need to get a presigned URL from the API
const resp = await fetch(
`https://${SDK_API_URL}/v1/storage/s3/presigned`,
{
method: "POST",
body: JSON.stringify({
bucket: "bucket-name",
path: "path/to/file", //without leading slash
file: file.name,
contentType: file.type,
}),
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
}
);// The response will contain a presigned URL and a set of fields
const presigned = await resp.json();// Then, we need to upload the file to S3 using the presigned URL
const form = new FormData();
Object.entries(presigned.fields).forEach(([k, v]) => {
form.append(k, v);
})
form.append("file", file); // file is a File object
return axios({
method: "POST",
url: presigned.url,
data: form,
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (progressEvent) => {
const percentCompleted = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
files[index].progress = percentCompleted;
setFiles([...files]); // update the state using react hooks
},
})
```## 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/