Skip to content

Deploy a Lambda Function

Tutorial

In this example, the source code in the directory Lambda.CreateBooking/ is deployed to your AWS account:

GitHub repository containing your source code
.
└── Lambda.CreateBooking/
    ├── ...
    └── Dockerfile

This Lambda function is deployed under the product name Kattehotell, and the function name create-booking.

Step 1: Prepare an ECR repository

  1. Navigate to repository SharedKontoInfrastruktur
  2. Navigate to ./Shared/kattehotell/eu-west-1/ecr/main.tf
  3. Update locals with new entries:
    ./Shared/kattehotell/eu-west-1/ecr/main.tf
    locals {
      kattehotell_repos = toset([
        ...
        "create-booking",
      ])
    
      account_access = [
        local.accounts["BYM-DP-Kattehotell-Test"].id, 
        local.accounts["BYM-DP-Kattehotell-Prod"].id 
      ]
    }
    
    module "kattehotell_ecr" {
      for_each         = local.kattehotell_repos                                    
      source = "git@github.com:BYM-IKT/terraform-aws-ecr.git?ref=v3"
      application_name = "kattehotell"
      service          = each.value
      account_access   = local.account_access 
    }
    
    output "kattehotell_ecr_repo" {
      value = module.kattehotell_ecr
    }
    
  4. Create a Pull Request to Terraform apply this change. Terraform provisions a new ECR repository with the name ${product_name}-${function_name}. In this example, the resulting ECR repository receives the name kattehotell-create-booking.

Step 2: Build and push Docker Image

Before you can proceed the ECR repository cannot be empty.

  1. Create a GitHub Actions workflow file .github/workflows/deploy-to-test.yml:

    .
    ├── .github/
    │   └── workflows/
    │       └── deploy-to-test.yml
    └── Lambda.CreateBooking/
        ├── ...
        └── Dockerfile
    
    with the following content:
    ./.github/workflows/deploy-to-test.yml
    name: Deploy Lambda Function to TEST
    
    on:
      push:
        branches: [master]
      workflow_dispatch:
    
    jobs:
      build_push_image_to_shared:
        name: Build image and publish to ECR
        runs-on: ubuntu-latest
        permissions:
          id-token: write
          contents: read
        steps:
          - name: "Lambda.CreateBooking"
            uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
            with:
              dockerfile-path: Lambda.CreateBooking/Dockerfile
              aws-account-id:  "<<AWS_ACCOUNT_ID>>"        
              ecr-name:        kattehotell-create-booking
              image-tags:      latest
    
      deploy_image_to_lambda_function_test:
        needs: [build_push_image_to_shared]
        name: "Deploy to TEST"
        uses: BYM-IKT/github-actions/.github/workflows/deploy-image-to-lambda.yml@master
        with:
          environment:              testing
          aws-account-id-target:    <<AWS_ACCOUNT_ID>>
          ecr-name:                 kattehotell-create-booking
          image-tag-target:         latest
          image-tag-new:            test
          lambda-name:              test-kattehotell-create-booking
    
    where <<AWS_ACCOUNT_ID>> is replaced with the Id of your AWS Account.

  2. Run the pipeline, which builds and pushes your Docker image to your ECR repository.

Why did deploy_image_to_lambda_function_test crash?

This pipeline crashes when running deploy_image_to_lambda_function_test as the Lambda function does not exist yet. This is fixed in the next steps.

Step 3: Create Lambda Function

Provision your Lambda Function referencing the ECR repository's URI as follows:

locals.tf
1
2
3
locals {
  ecr = { for k, v in data.terraform_remote_state.shared_kattehotell_ecr.outputs.ecr: k => v.ecr_url }
}
main.tf
module "application" {
  source = "git@github.com:BYM-IKT/terraform-byks-module.git?ref=v12"
  team             = var.team
  account_id       = var.account_id
  environment      = var.environment
  region           = var.region
  application_name = var.application_name

  lambda_functions = {
    create-booking = {
      ecr_uri   = local.ecr["create-booking"]
      image_tag = var.environment

      environment_variables = {
        MY_ENV_VAR = "hello world!"
      }
    }
  }

  providers = {
    aws.route53   = aws.route53
    aws.us-east-1 = aws.us-east-1
    aws.ses       = aws.ses
  }
}

Create a Pull Request to Terraform apply this change.

Terraform provisions a new Lambda function named ${environment}-${product_name}-${function_name}. In this example, the resulting name of the Lambda function is test-kattehotell-create-booking.

Step 4: Retry application pipeline

The whole pipeline created in Step 2 should work now. Try rerunning it.