Building a Pipeline in AzureDevOps for Lambda

AWS Lambda is a popular cloud-based computing service that enables developers to run their code without the need to manage the underlying infrastructure. Azure DevOps is a cloud-based platform that provides tools for continuous integration and continuous deployment (CI/CD) to automate the deployment of code changes to multiple environments. In this article, we will explore how to build a pipeline in Azure DevOps to deploy AWS Lambda functions.

Step 1: Connect Azure DevOps to your AWS Account

o deploy AWS Lambda functions using Azure DevOps, you need to connect your Azure DevOps account to your AWS account. This will allow Azure DevOps to access your AWS environment and perform the necessary deployment operations.

To connect your Azure DevOps account to AWS, follow these steps:

  1. Go to your Azure DevOps project and select Project Settings.
  2. In the Project Settings page, select the “Service connections” section.
  3. Click on the “New service connection” button and select “Amazon Web Services.”
  4. Enter your AWS account information, including your access and secret keys, and click “Save.”

Step 2: Create a New Pipeline

Now that you have connected your Azure DevOps account to your AWS account, you can create a new pipeline to deploy your AWS Lambda function.

  - name: function_name
    type: string
    default: "Your function name"  
- stage: Build
      - job: build_zip
        container: python:3.9
        - script: 'python3 --version'
          displayName: '🔍 Check python version'
        - script: |
            python3 -m venv venv 
            source venv/bin/activate
            pip install --upgrade pip
            pip install aws-sam-cli
            pip install -r src/requirements.txt            
            sam --version
            sam build 
            cd .aws-sam/build/${{ parameters.function_name}} 
            python -c "import shutil; shutil.make_archive('../../../${{parameters.function_name}}', 'zip', '.')"
          displayName: '📦 Build zip file'
        - publish: $(System.DefaultWorkingDirectory)/${{ parameters.function_name}}.zip
          artifact: ${{ parameters.function_name}}

When this jobs complete, I should find a new artifact that holds the zip file, I can download it and check if it contains all the codes and deps.

In the release part, I download the artifact, the destination should be $(System.ArtifactsDirectory). And then, I use the AWS CLI to run a command with sub command update-function-code with parameters --function-name $(LAMBDA_NAME) --zip-file fileb://$(FUNCTION_NAME).zip

If you have a layer to attach to the Lambda you should then run this script with lambda name in args:

LAYER_ARN=$(aws lambda list-layer-versions --layer-name layer_name| jq .LayerVersions[0].LayerVersionArn | sed 's/"//g')
# Update the lambda function
aws lambda update-function-code --function-name $LAMBDA  --zip-file fileb:// 
sleep 5 # This is optional
# Attach layer to the lambda
aws lambda update-function-configuration --function-name $LAMBDA --layers $LAYER_ARN --handler app.lambda_handler

Step 4: Test and Deploy your Pipeline

Once you have configured your pipeline, you can test and deploy it to your AWS environment. To test your pipeline, you can use a test environment in your Azure DevOps project. To deploy your pipeline to production, you can trigger a deployment from your Azure DevOps project or set up an automatic deployment trigger.

In conclusion, building a pipeline in Azure DevOps for AWS Lambda functions is a simple and efficient way to automate the deployment of code changes to your AWS environment. By using Azure DevOps and AWS CloudFormation, you can streamline your deployment process and ensure that your code changes are deployed quickly and reliably to your AWS environment.

You Might Also Like

Leave a Reply