This guide walks you through creating an AWS Lambda app that can be deployed to a customer’s AWS account. This app will provide an HTTP API that you can use to create and read “widgets” from a DynamoDB table.

The component code used in this guide can be found in our Guides repo.

Prerequisites

What You Will Create

This tutorial will walk you through creating the following:

Configure App

To configure the app, you will create a TOML config file using our CLI. In each section below we will provide you with configuration snippets for the app itself as well as it’s components.

If you would prefer to use Terraform, see our Terraform Configuration Management guide. We provide Terraform sample code you can use throughout this guide.

Create App

Define the app itself and give it a name. This will create the app in Nuon and generate a config file named nuon.<your-app>.toml. This file will be populated with sample config, which we will update in this guide.

nuon apps create --name=<your-app>

Installer

Update the installer config. Installers provide an out-of-the-box installation flow your customers can use to install your app. You will use it later in this guide to create an install yourself.

[installer]
name               = "My AWS Lambda App"
description        = "A demo AWS Lambda app."
documentation_url  = "https://docs.nuon.co/"
community_url      = "https://join.slack.com/t/nuoncommunity/shared_invite/zt-1q323vw9z-C8ztRP~HfWjZx6AXi50VRA"
github_url         = "https://github.com/nuonco"
homepage_url       = "https://www.nuon.co/"
demo_url           = "https://www.nuon.co/"
logo_url           = "https://assets-global.website-files.com/62a2c1332b518a9eedc6de2f/651df2030c43865b9b16046b_Group%2048.png"
og_image_url       = "https://assets-global.website-files.com/62a2c1332b518a9eedc6de2f/651df2030c43865b9b16046b_Group%2048.png"
favicon_url        = "https://assets-global.website-files.com/62a2c1332b518a9eedc6de2f/651df2030c43865b9b16046b_Group%2048.png"
copyright_markdown = """
© 2024 Nuon.
"""
footer_markdown = """
[Terms of Service](https://nuon.co/terms)
"""
post_install_markdown = """
# My AWS Lambda App

My AWS Lambda App is being deployed.
"""
app_ids = ["<your-app-id>"]

Sandbox

Update the sandbox config.

[sandbox]
terraform_version = "1.5.4"
[sandbox.public_repo]
directory = "aws-ecs"
repo = "nuonco/sandboxes"
branch = "main"

Runner

Update the runner config. The aws-ecs sandbox requires that we use the aws-ecs runner. The runner manages the sandbox, provisioning and deprovisioning AWS resources during deploys.

The aws-ecs runner is so named because it runs on ECS, but it can be used to manage any AWS resources. Since it runs on ECS Fargate, no resources beyond what is needed for the runner are provisioned by default. You can use our ECS sandbox and runner to manage Lambda or EC2 deployments without worrying about extraneous ECS costs.

[runner]
runner_type = "aws-ecs"

Sync App Config to Nuon

You now have a complete Nuon app config. This is a good place to stop and sync it to Nuon.

nuon apps sync

Once the config is synced, select the newly created app using the CLI. This will scope CLI commands to the new app.

nuon apps select

Connect Components

This app consists of five components:

  • A DynamoDB table to store widgets in.
  • A docker image containing the widgets service.
  • A Lambda function that will run the docker image.
  • An API Gateway that will execute the Lambda function
  • An ACM Certificate for the API Gateway to use to support HTTPS

Docker Image

This is a Docker Build component that will build the widgets service and create a Docker image containing it. When released, it will sync the image to each install’s ECR so Lambda can pull it when executing.

[[components]]
name   = "docker_image"
type = "docker_build"
dockerfile = "Dockerfile"
[components.public_repo]
repo      = "nuonco/guides"
directory = "aws-lambda-tutorial/components/api"
branch    = "main"

DynamoDB Table

This component will create a DynamoDB table to store widgets in.

[[components]]
name   = "dynamodb_table"
type = "terraform_module"
terraform_version = "1.5.3"
[components.public_repo]
repo      = "nuonco/guides"
directory = "aws-lambda-tutorial/components/dynamodb-table"
branch    = "main"
[[components.var]]
name  = "name"
value = "widgets"
[[components.var]]
name  = "hash_key"
value = "ID"

Lambda Function

This component will create a Lambda function to run the docker image.

[[components]]
name   = "lambda_function"
type = "terraform_module"
terraform_version = "1.5.3"
[components.public_repo]
repo      = "nuonco/guides"
directory = "aws-lambda-tutorial/components/lambda-function"
branch    = "main"
[[components.var]]
name  = "function_name"
value = "widgets"
[[components.var]]
name  = "image_uri"
value = "{{.nuon.components.docker_image.image.repository.uri}}:{{.nuon.components.docker_image.image.tag}}"

ACM Certificate

This component will create an ACM Certificate, so the API Gateway can handle HTTPS traffic.

[[components]]
name   = "certificate"
type = "terraform_module"
terraform_version = "1.5.3"
[components.public_repo]
repo      = "nuonco/guides"
directory = "aws-lambda-tutorial/components/certificate"
branch    = "main"
[[components.var]]
name  = "domain_name"
value = "api.{{.nuon.install.sandbox.outputs.public_domain.name}}"
[[components.var]]
name  = "zone_id"
value = "{{.nuon.install.sandbox.outputs.public_domain.zone_id}}"

API Gateway

This component will create an API Gateway, so we can call the lambda function via an HTTP API.

[[components]]
name   = "api_gateway"
type = "terraform_module"
terraform_version = "1.5.3"
[components.public_repo]
repo      = "nuonco/guides"
directory = "aws-lambda-tutorial/components/api-gateway"
branch    = "main"
[[components.var]]
name  = "name"
value = "api"
[[components.var]]
name  = "domain_name"
value = "api.{{.nuon.install.sandbox.outputs.public_domain.name}}"
[[components.var]]
name  = "domain_name_certificate_arn"
value = "{{.nuon.components.certificate.outputs.public_domain_certificate_arn}}"
[[components.var]]
name  = "lambda_function_arn"
value = "{{.nuon.components.lambda_function.outputs.lambda_function_arn}}"
[[components.var]]
name  = "zone_id"
value = "{{.nuon.install.sandbox.outputs.public_domain.zone_id}}"

Sync Component Configs to Nuon

Now that you have the components, sync the updated config to Nuon.

nuon apps sync

Just like the app, you can use the CLI to verify they were synced successfully.

nuon components list

Initial builds for each component will also have been created. Verify with the CLI that they were successful.

nuon builds list

Create an Install

Creating an install requires two steps: granting access to the AWS account via an IAM role, and then provisioning the install in that account. There are a few ways to do this, but the easiest is to use the installer you configured earlier, via our installer UI template.

You can find the template at https://github.com/nuonco/installer. Clone that and run it locally following the instructions in the README.

For other approaches, see our guides Install Access Permissions and Create Installs.

Monitor Install Creation

To monitor the install’s status, log into the Dashboard and select your org. You should see a card for the install.

Installs on Dashboard

Click on the card, and use the History to verify that the install is being provisioned. You should see events for the sandbox being provisioned and the components being deployed.

Install Details on Dashboard

Inspect the Install

When the install has provisioned, you should be able to create a widget.

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"id":"1"}' \
  https://api.{install_id}.nuon.run/widgets

Then, you should be able to read the widget you just created.

curl https://api.{install_id}.nuon.run/widgets/1

Wrapping Up and Next Steps

Congratulations, you just deployed an AWS Lambda app to AWS! A few suggestions for where to go next:

  • Check out our Release Management guide to learn how to update installs.
  • Dig into our App Configuration guide to learn how to configure more complex apps.
  • Share your installer with a friend and have them install your app in their AWS account.