Back to Garden
Guides|May 16, 2023

Deploying Golang AWS Lambdas with Ease: A Step-by-Step Guide Using Github Actions

#AWS#GitHubActions#Golang

How to Deploy Golang AWS Lambda using Github Actions

How to Deploy Golang AWS Lambda using Github Actions

In this article, we will be explaining how to deploy your Golang AWS Lambda functions using GitHub Actions. This process will automate the deployment of your functions and make it a lot easier for you to keep your functions up-to-date.

Step 1: Checkout

The first step in our Github Actions workflow is to checkout your repository. This step is performed using the actions/checkout@v3 action. This action checks out the repository so that we can build and deploy the code.

- name: Checkout  
  uses: actions/checkout@v3

Step 2: Set up Go

Next, we will set up the Go programming language. This step is performed using the actions/setup-go@v3 action. In this step, you can specify the version of Go you want to use. For this example, we are using version 1.16.

- name: Set up Go  
  uses: actions/setup-go@v3  
  with:  
    go-version: 1.16

Step 3: Build

In this step, we will build the Golang code that will be deployed to the AWS Lambda function. To do this, we will use the Go build command and specify the main.go file. The working directory should be set to the location of your code.

- name: Build  
  working-directory: ./lambda/<function-name>  
  run: go build -v main.go

Step 4: Zip

Next, we will zip the build file so it can be uploaded to AWS Lambda. We will use the zip command and specify the build file to do this. The working directory should be set to the location of your code.

- name: Zip  
  working-directory: ./lambda/<function-name>  
  run: zip -j deploy.zip main

Step 5: Configure AWS Credentials

Now that our code is ready to be deployed, we will configure the AWS credentials. This step is performed using the aws-actions/configure-aws-credentials@v1 action. In this step, you will need to specify your AWS access key ID, AWS secret access key, and the AWS region. These values can be stored as secrets in your GitHub repository.

- name: Configure AWS Credentials  
  uses: aws-actions/configure-aws-credentials@v1  
  with:  
    aws-access-key-id: {{ secrets.AWS_ACCESS_KEY_ID }}  
    aws-secret-access-key: {{ secrets.AWS_SECRET_ACCESS_KEY }}  
    aws-region: eu-central-1

Step 6: Upload Zip

The final step is to upload the zip file to AWS Lambda. To do this, we will use the AWS CLI command to update the function code. The working directory should be set to the location of your code.

- name: Upload Zip  
  working-directory: ./lambda/<function-name>  
  run: aws lambda update-function-code --function-name <function-name> --zip-file fileb://deploy.zip

Full Code:

jobs:  
  Deploy:  
    name: Deploy  
    runs-on: ubuntu-latest  
    steps:  
      - name: Checkout  
        uses: actions/checkout@v3  
      - name: Set up Go  
        uses: actions/setup-go@v3  
        with:  
          go-version: 1.16  
      - name: Build  
        working-directory: ./lambda/<function-name>  
        run: go build -v main.go  
      - name: Zip  
        working-directory: ./lambda/<function-name>  
        run: zip -j deploy.zip main  
      - name: Configure AWS Credentials  
        uses: aws-actions/configure-aws-credentials@v1  
        with:  
          aws-access-key-id: {{ secrets.AWS_ACCESS_KEY_ID }}  
          aws-secret-access-key: {{ secrets.AWS_SECRET_ACCESS_KEY }}  
        aws-region: eu-central-1  
      - name: Upload Zip  
        working-directory: ./lambda/<function-name>  
        run: aws lambda update-function-code --function-name <function-name> --zip-file fileb://deploy.zip

With these steps, you should now have a working Github Actions workflow that will deploy your Golang AWS Lambda functions. By automating this process, you will be able to focus on writing and testing your code, rather than worrying about deployment.

Note that you need to replace the &lt;function-name> placeholder with the actual name of your AWS Lambda function and the values for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY should be replaced with your actual AWS credentials.

This is just a basic example of deploying Golang AWS Lambda functions using GitHub Actions, but it can be further customized to meet your specific needs.