Automating Serverless Deployment of Lambda Function with Terraform using  Gitlab

Automating Serverless Deployment of Lambda Function with Terraform using Gitlab

Agenda

  1. Push terraform and infra code and python code into repo, in this case it is Gitlab repo..

  2. Import your code to Gitlab.

  3. Add . gitlab-ci.yml file in the workspace.

  4. Update your main.yml file as per need, and run the pipeline.

Python code we are using here

# python_files/python.py
import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello Terraform !!! LAMBDA here, How you Doing? for Gitlab')
    }

Terraform main.tf file:

  • This is the most important file for the CI/CD deployment using terraform.

  • Location terraform_aws_lambda/main.tf.

  • Important code snippets:

# Creating a lambda function with code from Zip files, and giving [REQUIRED] role to it
resource "aws_lambda_function" "test_lambda" {
  filename         = "${path.module}/zip/python_files.zip"
  function_name    = var.lambda_function_name.lambda
  role             = aws_iam_role.iam_for_lambda_tf.arn
  handler          = var.handler_name.handle # this is required since package type is a zip
  source_code_hash = data.archive_file.python_zip.output_base64sha256
  runtime          = var.runtime.version
}

Potential problems you might face:

  • In “resource "aws_lambda_function" "test_lambda" {...}”

“handler” is an important tag, the value of which determines whether your code runs on Lambda function or not.

So, stay attentive with it. “handler_name.handle” has a value stored in variable.tf.

How to write “handle” value:

Tips: always write, “filename.function_name” format to make your handler work. In our case it was “python.lambda_handler”.

“python” python file name, “lambda_handler” function name used in the file.

Context

In this captivating blog post, our main focus shines on a remarkable aspect of Gitlab: the ability to effortlessly deploy serverless infrastructure VIA Gitlab pipelines. By combining the powers of Gitlab pipeline and Terraform on AWS, we can swiftly create resources, such as an AWS Lambda, in just a matter of minutes. It's a truly impressive and efficient feat!

Prepare for an exhilarating journey through this blog as we embark on an enthralling quest to craft and deploy our "Hello World" Lambda Function with the seamless aid of Gitlab CI/CD prowess.

Let’s Look at the prerequisite you would need.

Pre-Requisites

Before embarking on our journey, let's ensure we have the essential prerequisites in place:

  • AWS Account: Our expedition involves deploying resources on AWS, so having an AWS account is paramount. If you don't have one yet, you can easily create it by visiting the AWS website.

  • Code Repository: This tutorial assumes that you possess a repository housing your precious Terraform scripts. For this guide, we'll utilize the "AutoScaleupInfra Code Repository" specifically designed for the Lambda Function.

  • Gitlab Account: Finally, to unlock the full potential of our adventure, you'll need a Gitlab account Worry not, Gitlab offers a freemium account that's more than sufficient to begin your journey. You can sign up for a Gitlab account here, and you also have the option to use your Google credentials for seamless registration.

Our Deployment Goal

Welcome to this informative guide! Our goal is to use Terraform scripts to deploy an AWS Lambda function, and we'll make this happen using the capabilities of Gitlab's CI/CD pipelines. By the time you finish reading this tutorial, you'll have the ability to easily deploy your Lambda to your AWS account by simply pushing your code to your repository.

In the upcoming sections, we'll lead you through the process of setting up your Gitlab project smoothly. Then, we'll show you how to create your own CI/CD pipeline. Lastly, we'll share in the joy of successfully deploying your Lambda function. Get ready for an empowering journey ahead!

Now, it's time to embark on our journey. Let's begin!

For how to create your Gitlab account and Importing a Gitlab repo, you can refer here.

Inserting a .gitlab-ci.yml file in the project.

stages:
  - validate
  - apply
validate:
  stage: validate
  script:
    - echo "Validating Terraform code"
    - terraform validate
apply:
  stage: apply
  script:
    - echo "Applying Terraform code"
    - terraform apply -auto-approve -var "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" -var "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}"
    - terraform destroy -auto-approve -var "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" -var "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}"

The above code will validate your terraform code and apply it.
For a complete .yml file, visit our Gitlab repo.

Don’t Forgot to add and change your Secrets in Gitlab Variables.

Here’s how you can add secrets.

Running the Pipeline

Open your Gitlab project and navigate to the Build section form sidebar.

  • Next, click on ‘Pipelines’.

  • Click on ‘Run pipeline’, it will run from code written in the . Gitlab-ci.yml file in the repository.

  • You will be prompt to this page:

  • Here you can add Variables for the file.

  • Next, click ‘Run pipeline’.

Successful Run

Tree ->

  • validate

    • init

    • validate

  • build

    • plan
  • deploy

    • apply

  • Check your AWS account.

  • Navigate to Lambda functions.

Click on the lambda function.

  • Click on the public URL.

You can see the resultant python app output.

Pipeline Run Successful

On complete run, you can see, your lambda function has been destroyed and everything is cleaned-up, you can change clean-up whenever you need from .yml file.

  • In our situation, we performed a cleanup because it was a test environment, and we wanted to avoid incurring costs for resources not actively in use.

Looking for complete code repository visit

To sum it up, we have experienced the fusion of the Gitlab pipeline with Terraform, effortlessly automating the deployment of Lambda Function. This dynamic alliance enhances efficiency, minimizes errors, and propels software delivery to new heights.

We pushed our local code to the repository of Gitlab.

  1. We created a .yml file and pushed it to the same Gitlab repository.

  2. Spin up the lambda function and access the public url for the output.

For more of these pre-built terraform recipes, visit Infrasity

Did you find this article valuable?

Support Infrasity Blog by becoming a sponsor. Any amount is appreciated!