Contact Us
Thought Leadership

Simple CD/CI Pipeline for AWS Lambda - Walkthrough

November 26, 2020

Scenario

Create a CD/CI Pipeline for version control and code deployment of an AWS Lambda using AWS Developer Tools in the simplest way.

Tools

GitHub

Popular version control platform based in git, is used as code repository and version control for software changes, it can be substituted by AWS CodeCommit.

AWS CodePipeline

Is a Continuous delivery tool that provides an orchestration for Code Deployment or infrastructure changes.

AWS CodeBuild

It is a continuous delivery tool for compile, run test, and create deployment packages.

AWS CodeDeploy

It is an automatic system for software deployments, it automatizes the software implementations for Amazon EC2, AWS Fargate, AWS Lambda and local servers.

AWS Lambda

Serverless compute platform to run small pieces of code as functions on demand.

Implementation

Roles

In AWS IAM create a role called “AWSLambdaDeployRole” and attach to it the policy “AWSCodeDeployRoleForLambda”.

Source Code Repository

We need a Git repository in which our code is going to be located and where the source code is taken for the deployment, in this case we use GitHub, but you can easily use AWS CodeCommit for this propose.

In this first we only need your Lambda code in the language of your preference.

Create AWS Lambda

Create a new Lambda function and create a new publish version with a new alias for it, you don’t need to upload the code yet.

Deployment configuration

In AWS CodeDeploy create a new Application, set a name and select “AWS Lambda” as Compute Platform.

Now we need to create a new deployment group, set a name, select “AWSLambdaDeployRole” as service role and let “CodeDeployDefault.LambdaAllAtOnce” as deployment settings.

Create the Pipeline

In AWS CodePipeline create a new Pipeline, set a name for it, as Service Role select “New Service Role” and set “AWSLambdaPipelineRole” as role name, let checked “Allow AWS CodePipeline to create a service role so it can be used with this new pipeline”, click on next.

As source provider select “GitHub” (or AWS CodeCommit if it is your case), click on “Connect to GitHub”, in Repository select the one in which you have the Lambda code to deploy and in Branch, select the branch to deploy automatically when a change is pull on it and in Change detection option let “GitHub Webhooks”, click on next.

In Build Stage select “AWS CodeBuild” as Build provider, verify the region is the same in which you create the AWS CodeDeploy Application and the same for your pipeline.

Click on “Create project” this should launch a new window for AWS CodeDeploy project configuration, set the project name.

In environment section select “Managed Image” as Environment Image, “Ubuntu” as Operating System, “Standard” as Runtime, “aws/codebuild/standard:2.0” as Image and select “Always use the latest image for this runtime version” as Image version.

For Service Role select “New service role”, let the default Role name.

For Buildspect section select “Use a buildspect file” and set “configuration/build.yml” as the buildspect name.

Let everything else as default and click on “Continue to CodePipeline”, back to CodePipeline click on Next.

Now in the Deploy Stage, select “AWS CodeDeploy” as Deploy provider, set the region the same as codepipeline and codebuild, select the previews created Deployment application in Application Name and select the Deployment group as well and go to the next stage.

Review the information is correct and click on “Create Pipeline”.

Note: This action launches the pipeline, it is going to fail in Build stage because we have not the build and deploy configuration files.

Configuration files

Build

This is the most important one, here we are going to download dependencies, build, package and upload the code into the Lambda, see the following file example.

  • Text Boxinstall: Here we download dependencies for our project, in this case I use NodeJS as source language for my application, so I need to run npm install in order to do it.
  • pre_build: here we can set any command that we want to execute after install but before build, this is useful for make a structure or run test scripts.
  • build: this section is for run build commands, in case of Java for example, we can run Maven commands to build and package our application, in this case I run a zip command to create a single zip file with the code and dependencies ready to upload into our Lambda.
  • post_build: this is the trick, post_build is used for scripts you need to run after build, in this case we use this section to upload the zip file as source four our Lambda function using whis command. aws lambda update-function-code --function-name "$LambdaFunctionName" --zip-file fileb://$PATH_TO_CODE and aws lambda publish-version –-function-name "$LambdaFunctionName" for create a new release.
  • Artifacts: here we set the files AWS CodeBuild is going to provide for the next step, in this case, we only need the configuration file for CodeDeploy, because the source code is already in the Lambda and unfortunately CodeDeploy cannot do this part.

You need to create a “build.yml” file like this inside “/configuration/” folder for your git repository, I recommend do it in your local and wait to push your changes until you have built and deployed configuration files.

Deploy

For this case, CodeDeploy is used for update Lambda version and create new releases, for this propose we need to create a “appspect.yml” file inside “/configuration/” folder, please look the following example.

This change the traffic from the current Lambda release to the new one we release each time a change is made in our branch, just remember update the currentversion and targetversion.

Conclusion

This path is very useful, fast and require less maintenance. I highly recommend it for Lambda projects that no require more infrastructure than the source code and API Gateway for example. You just need to be aware of the versions sets in the appspect.yml file and you are going to be good.

Related Insights

We are results-driven with a focus on providing customer service excellence

IO Connect Services is here to help you by offering high-quality cloud technology solutions.
Connect with us
®2024 IO Connect Services
Privacy PolicyCookie Policy
magnifiercrossmenuchevron-down
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram