Automate Rust Lambda deploy using GitHub Actions

We are open-sourcing a GitHub Action that will let you deploy AWS Lambdas written in Rust. This helps us automate deploying Rust AWS Lambda functions using GitHub Actions. If you are interested in the implementation details of this GitHub Action, you can look at the source code here. You can also file issues in that repository to ask for any modifications you might want.


Context

At Qxf2, we have a GitHub workflow for deploying all our AWS Lambdas written in Python. Now that we are moving to Rust, we wanted to have a similar workflow for all the Lambdas we will be developing in Rust. To achieve this, I created a workflow that uses Cargo Lambda, a Cargo plugin, or subcommand that provides several commands to run, build and deploy Rust functions on Lambda. I will not go into the details of how I created the action; pretty much followed the steps provided here. Rather, I would want to provide a short summary of how to use the GitHub Action I have put up.


How to use rust-lambda-action

The rust-lambda-action GitHub Action will help to build and deploy AWS Lambda functions written in Rust. It uses Amazon Linux 2 runtime for building the Lambda functions. To use this GitHub Action, you would need to call it from another GitHub workflow. In this section, I will describe what goes into creating the workflow.

Pre-requisites

It is expected that the code should be structured normally as Lambda would expect it and be present in a directory. At Qxf2, we have a repo to host all our lambdas and each lambda sits in its own directory. However, this will also work if you are calling this action from a repo which has a single lambda. In that case, for lambda_directory parameter, use ‘.’

Inputs

The workflow takes the following inputs:
– lambda_directory – The directory which has the Lambda code
– iam_role – The AWS IAM role required to deploy this Lambda.
Will be in the format – arn:aws:iam::*********:role/
– AWS_ACCESS_KEY_ID
– AWS_SECRET_ACCESS_KEY
– AWS_DEFAULT_REGION

The AWS credentials are passed as inputs to the rust-lambda-action which is required for the deploy step.

Create workflow step

Create a workflow step which calls the rust-lambda-action by passing the above inputs.

- uses: actions/checkout@master
- name: Deploy code to Lambda
  uses: qxf2/[email protected]
Implementation

The rust-github-action consists of two steps:
1. Uses cargo lambda build to first build for Amazon Linux 2 runtime.
2. Once the code is built, uses cargo lambda deploy to upload function to AWS. This step requires IAM role and AWS credentials which are input taken from the calling workflow.


Example Workflow

Here is the complete code snippet for an example workflow. It calls the rust-lambda-action to deploy dummy_lambda, which is a Lambda function written in Rust. This assumes you are running the workflow from a parent directory where the rust lambda that needs to be deployed is placed in a separate directory inside the parent directory.

name: deploy-dummy-lambda
on: 
  push:
    branches:
      - master
    paths:
      - 'dummy_lambda/**'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Deploy code to Lambda
      uses: qxf2/[email protected]
      with:
        lambda_directory: 'dummy_lambda'
        iam_role: ${{ secrets.AWS_IAM_ROLE }}
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}

Note: If you have one lambda per repo, you can simply provide ‘.’ as the lambda_directory.


Hire Qxf2 for your testing needs

Looking for testers with strong engineering fundamentals? Hire Qxf2. We are one of those rare testing firms that still contribute to the world of Free and Open Source Software. Our engineers will help you lay the technical foundation for testing at your startup. You can get in touch with us by filling out a simple contact form.


Leave a Reply

Your email address will not be published. Required fields are marked *