Skip to content

Instantly share code, notes, and snippets.

@arashout
Created January 28, 2022 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arashout/c020a942daa6edb9f11056fec0f676b7 to your computer and use it in GitHub Desktop.
Save arashout/c020a942daa6edb9f11056fec0f676b7 to your computer and use it in GitHub Desktop.
A simple bash script to demonstrate the steps required in the CI
# 1. Build zip file of compiled source code
zip_path="PATH_TO_COMPILED_ZIPFILE"
# 2. Upload zip file to AWS S3 so that we can point to it when calling `update-function-code`
bucket="lambda-zips"
function_name="test-function"
commit=(git rev-parse HEAD)
# This destination key is important because it will determine where we look for code when calling `update-function-code`
# You want to include this information about the current state of the code in it. a git SHA isn't a bad idea
dst_key="${function_name}/${commit}.zip"
s3_uri="s3://${bucket}/${dst_key}"
# The "progress" lines are SUPER noisy.
aws s3 cp --no-progress "${zip_path}" "${s3_uri}"
apiVersion: lambda-deployment.keeptruckin.com/v1
kind: LambdaDeployment
metadata:
name: lambda-deployment-test-function-preview
spec:
# We control the lambda environment using the ARN
functionARN: arn:aws:lambda:us-east-1:123:function:test-function-preview
# These will be used to construct the key for the zip file uploaded to S3
functionName: test-function
# The SHA of the current HEAD commit
commit: 8cc06dbec558fd94d9b40da92e347a34f454df61
// LambdaDeploymentSpec defines the desired state of LambdaDeployment
type LambdaDeploymentSpec struct {
// The ARN of the function that is to be updated
FunctionARN string `json:"functionARN"`
// The function name, this is used purely for building the S3 path
FunctionName string `json:"functionName"`
// The commit used to build the zip file uploaded to S3
// Note that the controller expects the zip file to be in the following locations
// s3://lambda-zip/FUNCTION_NAME>/<COMMIT>.zip
Commit string `json:"commit"`
}
// LambdaDeployment is the Schema for the lambdadeployments API
type LambdaDeployment struct {
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec LambdaDeploymentSpec `json:"spec,omitempty"`
Status LambdaDeploymentStatus `json:"status,omitempty"`
}
func (r *LambdaDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ld := &LambdaDeployment{}
err := r.Get(ctx, req.NamespacedName, ld)
switch e := err; {
case apierrors.IsNotFound(e):
// This corresponds to a DELETE action so we can ignore it
log.Println("LambdaDeployment is deleted... Doing nothing?")
return ctrl.Result{}, nil
case e != nil:
log.Printf("Could not GET the LambdaDeployment: %s", err)
return ctrl.Result{}, nil
}
// This is the "lambda.Client" from "github.com/aws/aws-sdk-go-v2/service/lambda"
_, err = r.LambdaClient.UpdateFunctionCode(ctx, &lambda.UpdateFunctionCodeInput{
FunctionName: &ld.Spec.FunctionARN, // The ARN is the input to "FunctionName" parameter confusingly
S3Bucket: "lambda-zips",
S3Key: fmt.Sprintf("%s/%s.zip", ld.Spec.FunctionName, ld.Spec.Commit),
})
return ctrl.Result{}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment