|
1 | 1 | # rust-lambda-starter
|
| 2 | + |
| 3 | +This is a template for starting serverless projects with AWS Lambda, Rust, and Tofu. |
| 4 | + |
| 5 | +## Components |
| 6 | + |
| 7 | +The project contains: |
| 8 | +- a Cargo project with an empty library and a **hello_world** binary representing an example Lambda function; |
| 9 | +- a Python script for building all the Lambda funtions present in the `./src/bins` directory; |
| 10 | +- a base infrastructure written in OpenTofu with IAM, Lambda, and API Gateway resources; |
| 11 | +- a CI pipeline that triggers on any PR against the `main` branch or any commit to the `main` branch; |
| 12 | +- a CD pipeline that triggers on any push to the `main` branch. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Getting the project |
| 17 | + |
| 18 | +To get a local copy of the template, either use [pont](https://github.com/soupdevsolutions/pont): |
| 19 | + |
| 20 | +```bash |
| 21 | +pont build --name <your-project-name> --from https://github.com/soupdevsolutions/rust-lambda-starter |
| 22 | +``` |
| 23 | + |
| 24 | +or clone the repository and change the occurences of the template name: |
| 25 | + |
| 26 | +```bash |
| 27 | +git clone https://github.com/soupdevsolutions/rust-lambda-starter ./<your-project-name> |
| 28 | +cd <your-project-name> |
| 29 | + |
| 30 | +# rename the occurences using bash or your IDE tooling |
| 31 | +TEMPLATE_NAME_KEBAB_CASE=rust-lambda-starter |
| 32 | +TEMPLATE_NAME_SNAKE_CASE=rust_lambda_starter |
| 33 | +PROJECT_NAME=<your-project-name> |
| 34 | + |
| 35 | +find . \( -name \*.rs -o -name \*.tf -o -name \*.toml -o -name \*.md -o -name \*.lock \) -exec sed -i '' "s#$TEMPLATE_NAME_KEBAB_CASE#$PROJECT_NAME#g" {} \; |
| 36 | +find . \( -name \*.rs -o -name \*.tf -o -name \*.toml -o -name \*.md -o -name \*.lock \) -exec sed -i '' "s#$TEMPLATE_NAME_SNAKE_CASE#$PROJECT_NAME#g" {} \; |
| 37 | +``` |
| 38 | + |
| 39 | +### Deployment |
| 40 | + |
| 41 | +The recommended way for deploying the project is to use GitHub Actions; however, you can also do it from your local machine. |
| 42 | + |
| 43 | +For deploying with GitHub Actions, push your project to a new repository, go to the repository's **Settings** -> **Secrets and variables** -> **Actions**, and add secrets for your IAM user credentials: |
| 44 | +- AWS_ACCESS_KEY_ID -> the access key id |
| 45 | +- AWS_SECRET_ACCESS_KEY -> the secret access key |
| 46 | +Any push to the `main` branch will trigger both the CI and CD pipelines. |
| 47 | + |
| 48 | +If you choose to deploy from your local machine, make sure to follow the guide for [configuring the AWS CLI](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-configure.html), so OpenTofu can deploy to your AWS account. Then, you can run all the steps from the CD pipeline locally: |
| 49 | + |
| 50 | +```bash |
| 51 | +# init OpenTofu (only needs to be done once locally) |
| 52 | +tofu -chdir=infrastructure init |
| 53 | + |
| 54 | +# build the Lambda functions |
| 55 | +python3 ./scripts/build.py |
| 56 | + |
| 57 | +# plan and apply the changes |
| 58 | +tofu -chdir=infrastructure plan |
| 59 | +tofu -chdir=infrastructure apply |
| 60 | +``` |
0 commit comments