Deploying AWS Lambda (Typescript) with Github Actions

Deploying AWS Lambda (Typescript) with Github Actions

This articles looks at using Github Actions for deployment of a Typescript Lambda by using the aws-cli. In this example the the Lambda function deployed is also pre built using the vercel/ncc npm package. This builds the Lambda code and dependency code into a single Javascript file. This gives the advantage of reduced overall size of the deployment package.

The aws-cli is available out of the box with Github containers, but we still need to configure credentials. In order to do this we can use a github actions template from aws called aws-actions/configure-aws-credentials@v1. You will also need create an IAM user that has the rights to deploy the Lambda function and add the Access Key ID and Secret Access Key to Github secrets.

There are 3 main steps in the the CI deploy process. The code will be built to Javascript using the Vercel/ncc dependency. Then the built code is zipped up and the zip is uploaded using the aws-cli.

The final deploy yml file should go in a folder at the root called .github with the subfolder workflows. Below is an example of how that file should look:

name: Deploy

      - main

    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
          node-version: "12"
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-west-2
      - name: npm install
          CI: true
        run: |
          npm ci
      - name: deploy
        run: |
          npm run build
          npm run zip
          npm run update

Other options

It may be worth using a framework such as AWS SAM that makes the in intitial creation of the Lambda easier. An example of doing this is available here:

You can also use a framework such as AWS CDK. AWS CDK allows the user to use a programming language like Typescript to create the resources. CDK converts the code to CloudFormation before being deployed.