Dynamically change version number of Lambda Layer using a shell executable - bash

I am using a shell script that calls for some custom packages to be zipped and layered on lambda.
After deploying the layer via aws lambda publish-layer-version the layer version, obviously, goes up. The next command in my .sh script is something like
aws lambda update-function-configuration --function-name myfunc --layers arn:aws:lambda:<region>:273846758499:layer:<layer_name>:<version>
Since I am new to scripting in general I am open to any workable solutions but I am looking to iterate the <version> to the most recent version available on Lambda. How can this be written in this language?

You can simply parse the response from the first command:
For example, I'm using here jq, which parses jsons in bash.
version=$(aws lambda publish-layer-version --layer-name <your name> --zip-file <zip> --region "us-east-1" | jq -r '.LayerVersionArn')
Then, you can upload with:
aws lambda update-function-configuration --function-name <name> --layers $version
Disclosure: I work for Lumigo, a company that provides serverless monitoring.

Related

How to update a Lambda function with new code (without CodeDeploy)?

I have SAM installed on my Linux machine.
I updated some code in my infrastructure and need the new code updated in the Lambda.
I'm not using CodeDeploy, and I don't want to use it. Not yet anyway.
What is the proper way to propagate this updated code to my Lambda function?
As mentioned in this documentation: https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html
aws lambda update-function-code \
--function-name my-function \
--zip-file fileb://my-function.zip
You can use the above syntax to update your lambda code directly.

AWS-CLI Upload only one file in lambda (update-function-code)

I am using AWS-CLI to my files into my lambda (because i want to ship with my own boto3).
The problem is that I have to upload the whole project (my files + boto3) in my lambda.
I have to wait ~5min each time (my connection is kinda bad)
The question is: can i upload only the files that i want (as git)?
Currently I use this command:
zip -r function.zip . && aws lambda update-function-code --function-name MYFUNC --zip-file fileb://function.zip && rm function.zip
Thanks
Create a lambda layer for your common files and attach it to your lambda.
Where you can make direct upload for frequently changing files.
Lambda layers

How to avoid AWS SAM rebuild and reupload a gradle function with unchanged code?

I'm developing an application with micronaut using SAM CLI to deploy it on AWS Lambda. As I was including dependencies and developing new features, the function packages got bigger an bigger (now they are around 250MB). This makes deployment take a while.
On top of that every time I edit template.yaml and then run sam build && sam deploy to try a new configuration on S3, RDS, etc... I have to wait for gradle to build the function again (even though it's unchanged since the last deployment) and upload the whole package to S3.
As I'm trying to configure this application with many trials and errors on SAM, waiting for this process to complete just to get an error because of some misconfiguration is getting quite counterproductive.
Also my SAM s3 bcuket is at 10GB size after just a single day of work. This may get expensive on the long run.
Is there a way to avoid those gradle rebuilds and reuploads when teh function code is unchanged?
If you are only updating the template.yml file, you could copy the new version to ./.aws-sam/build folder and then run sam deploy
$ cp template.yml ./.aws-sam/build/template.yml
$ sam deploy
If you are editing a lambda you could try to update the function code by itself (after you create it in the template and deploy of course). That can be done via the AWS CLI update-function-code command:
rm index.zip
cd lambda
zip –X –r ../index.zip *
cd ..
aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://index.zip
more info can be found here:
Alexa Blogs - Publishing Your Skill Code to Lambda via the Command Line Interface
AWS CLI Command Reference - lambda - update-function-code
my SAM s3 bcuket is at 10GB size
Heh. Yea start deleting stuff. Maybe you can write a script using aws s3?

Publishing aws lambda version using jenkins and aws cli

I want to create a jenkins job that will publish a new AWS lambda version and update existing alias to new version just created.
I already have prod alias created in AWS Lambda.
Now i would i like to publish a new version and the update prod alias to point to new version.
There is walkthrough shows how to do it using AWS CLI. So here are the corresponding steps
1>Publish a new version of the Lambda function.
aws lambda publish-version --function-name helloworld
2>Update prod alias to latest version.
aws lambda update-alias --function-name helloworld function-version 2 --name prod
ISSUE
In Jenkins i will have to execute these commands as windows batch command
But i am not able to understand how do i dynamically pass the version number that was created by publish-version command in step 1, to update-alias command in step 2?
You can use below aws CLI command in the execute shell.
VERSION=$(aws lambda publish-version --function-name helloworld | jq -r .Version)
aws lambda update-alias --function-name helloworld --name prod --function-version $VERSION

AWS CLI doesn't find config file when running in batch file after maven command

When I just run the aws lambda update-function-code command in the cmd with the appropriate parameters everything works fine. It also works when I run the command in a batch file. But when I want to run mvn package before aws lambda update-function-code in a batch file I get the following error:
'You must specify a region. You can also configure your region by running "aws configure"'
I already configured it and I know it is correctly configured, otherwise by just running the aws lambda command it would also throw an error.
The config file is also at the location Amazon suggest it.
My batch file looks like this:
call mvn package
call aws lambda update-function-code --function-name <functionName> --zip-file fileb://<path/to/jar>
(Of course the words in brackets are just placeholder)
You could specify the AWS region as a command line option in the batch file
call aws --region us-east-1 lambda update-function-code --function-name <functionName> --zip-file fileb://<path/to/jar>
Any kind of switching of regions could be handled by logic in the batch file
I solved the problem!
Maven sets some local variables which affects in some way the aws lambda command. Because of call, these variables persist till the batch file is completly executed. To avoid that these variables are set till the end I had to add #SETLOCAL and #ENDLOCAL as follows:
#SETLOCAL
call mvn package
#ENDLOCAL
call aws lambda update-function-code --function-name <functionName> --zip-file fileb://<path/to/jar>
Now everything works like a charm.

Resources