Deploy a Serverless Application using Crossplane on EKS1. Build a docker image and push to ECR2 min

In this module, we will be building a Docker image with a sample application, creating an ECR repository, and uploading the image to it. This container image contains the source code for the Lambda function that is part of the serverless application we will be deploying later on in this tutorial.

Implementation

Clone the crossplane-on-eks repository to your local machine.

git clone https://github.com/awslabs/crossplane-on-eks.git

Navigate to the function code folder

cd crossplane-on-eks/examples/upbound-aws-provider/composite-resources/serverless-examples/object-processor-app 

Set AWS_REGION, ECR_URL, and IMAGE_NAME as environment variables.

export AWS_REGION=<replace-me-with-aws-region> # example `us-east-1`
export ECR_URL=$(aws sts get-caller-identity --output json | jq -r ".Account" | tr -d '[:space:]').dkr.ecr.$AWS_REGION.amazonaws.com
export IMAGE_NAME=<replace-me-with-image-name> # example `lambda-test`

Log in to ECR

aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_URL

Create ECR repository

aws ecr create-repository --repository-name $IMAGE_NAME

Build the Go binary

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go

Set the DOCKER_IMAGE env var. This step is necessary to properly escape the ':' for the subsequent docker commands

export DOCKER_IMAGE=$ECR_URL/$IMAGE_NAME\:latest

Build the docker image

docker build -t $DOCKER_IMAGE . 

Expected output

[+] Building 0.5s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                             0.0s
 => => transferring dockerfile: 37B                                                                              0.0s
 => [internal] load .dockerignore                                                                                0.0s
 => => transferring context: 2B                                                                                  0.0s
 => [internal] load metadata for public.ecr.aws/lambda/go:1                                                      0.3s
 => [internal] load build context                                                                                0.1s
 => => transferring context: 13.18MB                                                                             0.1s
 => [1/2] FROM public.ecr.aws/lambda/go:1@sha256:ea8389f1a5e7e9d29c4d65008da143985bef131cf901db63ab57e4124f7f66  0.0s
 => CACHED [2/2] COPY main /var/task                                                                             0.0s
 => exporting to image                                                                                           0.0s
 => => exporting layers                                                                                          0.0s
 => => writing image sha256:0f3ea389fa4f3761f25b2b666a8f49b458e825f1180b9c2181ecadd55fd2cc9a                     0.0s
 => => naming to 111122223333.dkr.ecr.us-east-1.amazonaws.com/lambda-test:latest

Upload the docker image to ECR

docker push $DOCKER_IMAGE

Expected output

The push refers to repository [111122223333.dkr.ecr.us-east-1.amazonaws.com/lambda-test]
8ce4b9b80622: Pushed
02c5912fad21: Pushed
2d244e0816c6: Pushed
2b58bd9b1feb: Pushed
d79a81893db4: Pushed
2ad9f6dfcb67: Pushed
latest: digest: sha256:86bb30cbf14d9ac538946cdd9565f09b7a4a0b9846e28a73cd27116280c0a58e size: 1579

Conclusion

In this module, we covered how to build a Docker image, create an ECR repo, and upload an image to it. In the next module, we will learn how to deploy the serverless application.


Created by:

David Boyne
David BoyneHelping people learn about event-driven architectures and serverless