Skip to main content

AWS Lambda serverless deployments

This tutorial demonstrates how to deploy on AWS Lambda using Harness Continuous Delivery (CD). We will guide you through deploying a sample function using a Harness pipeline.

Before you begin

Verify the following:

  1. Obtain GitHub personal access token with the repo scope. For the GitHub documentation, go to creating a personal access token.

  2. Docker. For this tutorial ensure that you have the Docker runtime installed on your Harness Delegate host. If not, use one of the following options to install Docker:

  3. Fork the harnesscd-example-apps repository through the GitHub website.

    • For more information on forking a GitHub repository, go to GitHub docs.
  4. AWS user account with required policy: Serverless deployments require an AWS user account with specific AWS permissions, as described in AWS Credentials from Serverless.com. To create the AWS user, do the following in AWS IAM:

    • Select Users, and then Add user. Enter a name. Enable Programmatic access by selecting the checkbox. Select Next to go to the Permissions page. Do one of the following:

      • Full Admin Access: select Attach existing policies directly. Search for and select AdministratorAccess then select Next: Review. Check to make sure everything looks good and select Create user.
      • Limited Access: select Create policy. Select the JSON tab, and add the JSON from the Serverless gist: IAMCredentials.json.

Supported platforms and versions

  • Harness supports Serverless framework 1.82 and later.
  • Harness supports Serverless framework CLI versions 2.x.x and 3.x.x.
  • Harness supports all language runtimes that Serverless supports.
  • Harness supports ZIP files and Docker image artifacts only.
    • ZIP files are supported with JFrog Artifactory.
    • Docker images are supported with AWS ECR.

Getting Started with Harness CD

  1. Log into Harness.
  2. Select Projects and choose Default Project.
warning

Going forward, follow all the steps as they are, including the naming conventions, for the pipeline to run successfully.

Delegate

What is the Harness Delegate?

The Harness Delegate is a service that runs in your local network or VPC to establish connections between the Harness Manager and various providers such as artifacts registries, cloud platforms, etc. The delegate is installed in the target infrastructure, for example, a Kubernetes cluster, and performs operations including deployment and integration. Learn more about the delegate in the Delegate Overview.

  1. Under Project Setup, select Delegates.
  2. In Delegates Setup, select Install new Delegate. The delegate wizard appears.
  3. In New Delegate, in Select where you want to install your Delegate, select Docker.
  4. Enter the delegate name, harness-aws-lambda-delegate.

Now you can install the delegate by using the command that appears on your installation wizard. The command is prefilled with the information for the environment variables in the example below.

docker run --cpus=1 --memory=2g \
-e DELEGATE_NAME=harness-serverless-delegate \
-e NEXT_GEN="true" \
-e DELEGATE_TYPE="DOCKER" \
-e ACCOUNT_ID= YOUR_HARNESS_ACCOUNTID \
-e DELEGATE_TOKEN=YOUR_DELEGATE_TOKEN \
-e LOG_STREAMING_SERVICE_URL=YOUR_MANAGER_HOST_AND_PORT/log-service/ \
-e MANAGER_HOST_AND_PORT=YOUR_MANAGER_HOST_AND_PORT \
DELEGATE_IMAGE:TAG
info

Replace DELEGATE_IMAGE:TAG with the custom delegate image harnesscommunity/serverless-delegate:latest. This image has the Serverless.com framework installed.

Verify delegate connectivity

  1. Select Continue. After the health checks passes, your delegate is available to use.
  2. Select Done and verify your new delegate is listed.

Secrets

What are Harness secrets?

Harness offers built-in secret management for encrypted storage of sensitive information. Secrets are decrypted when needed, and only the private network-connected Harness Delegate has access to the key management system. You can also integrate your own secret manager. To learn more about secrets in Harness, go to Harness Secret Manager Overview.

Github secret

  1. Under Project Setup, select Secrets.
  2. Select New Secret, and then select Text.
  3. Enter the secret name harness_gitpat.
  4. For the secret value, paste the GitHub personal access token you saved earlier.
  5. Select Save.

AWS secret

  1. Select New Secret, and then select Text.
  2. Enter the secret name awssecret.
  3. For the secret value, paste the access token for your AWS user account. The Harness Delegate uses this credential to authenticate Harness with AWS at deployment runtime.
  4. Select Save.
  1. Download and configure the Harness CLI.
curl -LO https://github.com/harness/harness-cli/releases/download/v0.0.24-Preview/harness-v0.0.24-Preview-darwin-amd64.tar.gz
tar -xvf harness-v0.0.24-Preview-darwin-amd64.tar.gz
export PATH="$(pwd):$PATH"
echo 'export PATH="'$(pwd)':$PATH"' >> ~/.bash_profile
source ~/.bash_profile
harness --version
  1. Clone the forked harnesscd-example-apps repo and change directory.

    git clone https://github.com/GITHUB_ACCOUNTNAME/harnesscd-example-apps.git
    cd harnesscd-example-apps
    note

    Replace GITHUB_ACCOUNTNAME with your GitHub account name.

  2. Log in to Harness from the CLI.

    harness login --api-key  --account-id HARNESS_API_TOKEN
    note

    Replace HARNESS_API_TOKEN with Harness API token that you obtained during the prerequisite section of this tutorial.

Connectors

What are connectors?

Connectors in Harness enable integration with 3rd party tools, providing authentication for operations during pipeline runtime. For instance, a GitHub connector facilitates authentication and fetching files from a GitHub repository within pipeline stages. For more details, go to Connectors.

  1. Create the GitHub connector.
    1. Replace DELEGATE_NAME under delegate selectors with the Delegate name.
    2. In projectIdentifier, verify that the project identifier is correct. You can see the Id in the browser URL (after account). If it is incorrect, the Harness YAML editor will suggest the correct Id.
    3. Now create the GitHub connector using the following CLI command:
      harness connector --file "/serverless-lambda/harnesscd-pipeline/github-connector.yml" apply
  2. Create the AWS connector.
    1. In projectIdentifier, verify that the project identifier is correct. You can see the Id in the browser URL (after account). If it is incorrect, the Harness YAML editor will suggest the correct Id.
    2. Now create the AWS connector using the following CLI command:
      harness connector --file "/serverless-lambda/harnesscd-pipeline/aws-connector.yml" apply

Environment

What are Harness environments?

Environments define the deployment location, categorized as Production or Pre-Production. Each environment includes infrastructure definitions for serverless functions, VMs, Kubernetes clusters, or other target infrastructures. To learn more about environments, go to Environments overview.

  1. Use the following CLI command to create an environment in your Harness project:

    harness environment --file "/serverless-lambda/harnesscd-pipeline/environment.yml" apply
  2. In your new environment, add an Infrastructure Definitions using the following CLI command:

    harness infrastructure --file "/serverless-lambda/harnesscd-pipeline/infrastructure-definition.yml" apply

Services

What are Harness services?

In Harness, services represent what you deploy to environments. You use services to configure variables, manifests, functions, and artifacts. The Services dashboard provides service statistics like deployment frequency and failure rate. To learn more about services, go to Services overview.

  1. Use the following CLI command to create a service in your Harness project.

    harness service -file "/serverless-lambda/harnesscd-pipeline/service.yml" apply

Deploy AWS Lambda using Serverless.com Framework

What are Harness pipelines?

A pipeline is a comprehensive process encompassing integration, delivery, operations, testing, deployment, and monitoring. It can utilize CI for code building and testing, followed by CD for artifact/function deployment in production. A CD pipeline is a series of stages where each stage deploys a service to an environment. To learn more about CD pipeline basics, go to CD pipeline basics.

  1. Use this CLI command to create a serverless deployment pipeline:

    harness pipeline --file "/serverless-lambda/harnesscd-pipeline/serverless-deployment.yml" apply
  2. In your Harness pipeline, select Save.

    You can switch to the Visual pipeline editor and confirm the pipeline stage and execution steps as shown below.

  3. Finally, it's time to execute the pipeline. Select Run, and then select Run Pipeline to initiate the deployment.

    Observe the execution logs as Harness deploys the function.

Congratulations!🎉

You've just learned how to use Harness CD to deploy an AWS Lambda function on AWS Lambda using the Serverless.com Framework.