0
votes

I am sure there are multiple ways AWS Lambda can be versioned/published, but I am trying to do it in a particular way and need some help.

I have a dotnet core Lambda project as "MyTTL".

Now in gitlab YML script I have code which will push the Lambda to S3 bucket like below (Pseudo Script).

GITLAB SCRIPT

variables:
  OUTPUT_FILE_PATH: '$CI_PROJECT_DIR/bin/Release/netcoreapp3.1/MyTTL.zip'

 - dotnet lambda package

 - aws s3 cp $OUTPUT_FILE_PATH s3://$S3_BUCKET/

Now above script works fine and upload MyTTL.zip to S3 bucket.

Now in the terraform I have below script to reference that Lambda

resource "aws_lambda_function" "lambda" {
  s3_bucket        = "My S3 BUCKET"
  s3_key           = "protected/sample/${var.artifact_version}.zip"
  source_code_hash = "${filebase64sha256("${var.artifact_version}.zip")}"
}

As you can see I want to pass a version (artifact_version) to this module, so that I can tell which Lambda version a particular client is running on.

Question - I am not sure how do I make sure on every dotnet lambda package a new zip version is created so that old terraform can still point to the old version of Lambda code and I can make terraform modifcation to new version of lambda for different clients at will?

Manual Lame Solution - I make the code change in my dotnet core project let the gitlab script publish it to S3 then i download it rename that zip to version I want and then upload it to S3 and then later reference it in terraform

1
your question is... how to not replace the last MyTTL.zip in S3? In that way you can still point to old versión of lambda but you can switch to a specific version when you want?Derek Menénedez
@DerekMenénedez So at this point no matter what I do I always end up with one zip file in S3. So how do I make sure I have different version of Zip getting created in S3 bucket after each code change. So I think you got my question correclty. A lame way would be I make the code change let the gitlab script publish it to S3 then i download it rename that zip to version I want and then upload it back to S3 and then later reference it in terraformUnbreakable

1 Answers

1
votes
variables:
  OUTPUT_FILE_PATH: '$CI_PROJECT_DIR/bin/Release/netcoreapp3.1/MyTTL.zip'

 - dotnet lambda package

 - aws s3 cp $OUTPUT_FILE_PATH s3://$S3_BUCKET/MyTTL${CI_COMMIT_SHORT_SHA }.zip

Now you have diferent versions of your lambda proyect... that with the hash of your commit... and now you no need to download only change the hash in the name. That hash always be unique on every commit.