Deploy an AWS Lambda function in your Development Environment
This tutorial will show you how to deploy a development environment in Okteto that contains an AWS Lambda function with Okteto.
Prerequisites
- Access to an Okteto instance
- Access to an AWS account with the permissions to create IAMs
Step 1: Create an IAM user
The IAM user that you use with AWS SAM must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
The following permissions will enable you to complete this tutorial:
- AWSCloudFormationFullAccess
- IAMFullAccess
- AWSLambda_FullAccess
- AmazonAPIGatewayAdministrator
- AmazonS3FullAccess
Once the user has been created, generate a set of access keys, and save them file locally.
In order for the installer to be able to use this keys, we need to add them as Okteto Variables. Use AWS_ACCESS_KEY_ID
as the name for the variable that contains the access ID, and AWS_SECRET_ACCESS_KEY
as the name for the variable that contains the access key.
You can add them as User Variables, or Admin Variables if you want everyone in your Okteto instance to be able to use them.
Step 2: Define a Dockerfile for your Deploy image
We are going to use the SAM CLI to build and deploy our AWS Lambda function. SAM is a open-source framework that you can use to build serverless applications in AWS.
You can directly install the SAM CLI in your Okteto manifest. However, this will make every deployment slower than it needs to be. Instead, we recommend that you create your own installer image with sam
(and any other tool you might need) preinstalled.
To do this, create the following Dockerfile file:
FROM python:3.9-buster
RUN apt-get update && \
apt-get install unzip curl && \
curl -o /tmp/awscli-exe-linux-x86_64.zip -L https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip && \
unzip /tmp/awscli-exe-linux-x86_64.zip -d /tmp/aws-cli && \
sh /tmp/aws-cli/aws/install && \
aws --version && \
curl -o /tmp/aws-sam-cli-linux-x86_64.zip -L https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip && \
unzip /tmp/aws-sam-cli-linux-x86_64.zip -d /tmp/sam-installation && \
sh /tmp/sam-installation/install && \
sam --version
Step 3: Create your Okteto manifest
In order to deploy a function in Okteto, you need to create an Okteto manifest similar to the one below:
build:
sam:
context: .
dockerfile: Dockerfile
deploy:
image: ${OKTETO_BUILD_SAM_IMAGE}
commands:
- name: build function
command: sam build
- name: deploy function
command: |
sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --s3-prefix "${OKTETO_NAMESPACE}" --stack-name "${OKTETO_NAMESPACE}-okteto-lambda" --resolve-s3
FUNCTION_URL=$(aws cloudformation describe-stacks --region us-east-1 --stack-name ${OKTETO_NAMESPACE}-okteto-lambda --query "Stacks[0].Outputs[0].OutputValue" --output text)
# use external resources to display the URL the Okteto UI
echo "OKTETO_EXTERNAL_LAMBDA_ENDPOINTS_FUNCTION_URL=$FUNCTION_URL" >> $OKTETO_ENV
destroy:
image: ${OKTETO_BUILD_SAM_IMAGE}
commands:
- name: destroy function
command: sam delete --no-prompts --stack-name "${OKTETO_NAMESPACE}-okteto-lambda" --region us-east-1
external:
lambda:
icon: function
endpoints:
- name: function
This manifest takes advantage of our External Resources feature manage the lifecycle of external resources as part of your Okteto development environment. Learn more about External Resources here.
Details:
- The
deploy.image
field tells Okteto to use the image we defined on thebuild
section. - The
deploy.commands
section includes all the commands needed to build and deploy the function. - The
destroy
section includes the commands needed to destroy the Lambda function. This will be invoked when the environment or the namespace are destroyed.
Step 4: Deploy your Development Environment
Deploy your Development Environment by running the following command:
okteto deploy
After a few seconds, your Development Environment will be fully deployed and your AWS Lambda function will be up and running. Every time you redeploy the Development Environment, the AWS Lambda function will be recreated with your latest code.
The source code used on this tutorial is available here.
Next steps
Congratulations, you just deployed your first AWS Lambda function in Okteto 🚀.
Head over to our getting started guides for Go, ASP.NET, Java, Node.js, PHP, Python, or Ruby to see how to integrate it with the rest of your applications.