Publishing aws lambda version using jenkins and aws cli - aws-lambda

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

Related

Dynamically change version number of Lambda Layer using a shell executable

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.

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?

How to use AWS CLI to deploy lambda function to specific alias or version?

Before I have Lambda version and alias, based on the API update-function-code I can successfully deploy by
aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip
After I added version and alias, I have Lambda version
$LATEST (default)
1
2
and alias
staging (point to version 1)
prod (point to version 2)
I try to deploy to staging (version 1),
1st Try
aws lambda update-function-code --function-name arn:aws:lambda:us-west-2:123456789000:function:myFunction:staging --zip-file fileb://archive.zip
gave error
Current operation does not support
versions other than $LATEST. Please set the version to $LATEST or do
not set a version in your request.
2nd Try
aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip --s3-object-version 1
or
aws lambda update-function-code --function-name myFunction --zip-file fileb://archive.zip --s3-object-version staging
gave error
Please do not provide other FunctionCode
parameters when providing a ZipFile.
How to use AWS CLI to deploy lambda function to specific alias or version correctly? Thanks
Okay so assuming you already managed to deploy your lambda with this command :
aws lambda update-function-code --function-name $FUNCTION_NAME --zip-file fileb://lambda.zip
And that you have created 2 versions with 2 alias (staging an prod).
Now you just need to publish to the rigth version :
VERSION=1
aws lambda update-alias --function-name $FUNCTION_NAME --name staging --function-version $VERSION
VERSION=2
aws lambda update-alias --function-name $FUNCTION_NAME --name prod --function-version $VERSION
If you want to go one step further you can bind the last deployment to the latest version. So first you need to retrieve the latest version and for this I use jq but feel free to use whatever you want, and then update with this version.
VERSION=$(aws lambda publish-version --function-name $FUNCTION_NAME | jq -r .Version)
aws lambda update-alias --function-name $FUNCTION_NAME --name staging --function-version $VERSION
Here is the update-alias documentation.
And here is the publish-version documention.
based on AWS documentations
A published version is immutable. That is, you can't change the code or configuration information.
so you need to publish a new version of your function and then update the alias to point to the newly created 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.

Update cloudformation stack from aws cli with SAM transform

When attempting to update a cloudformation stack in the aws cli:
aws --profile dev cloudformation update-stack --stack-name mystackname --template-body file://events-list.yaml
I get the following error
An error occurred (ValidationError) when calling the UpdateStack operation: UpdateStack cannot be used with templates containing Transforms.
Because I am using the AWS Serverless transform for lambda function deployments
Transform: 'AWS::Serverless-2016-10-31'
Is there a CLI way to execute this stack update or am I going to have to work on my APM in the GUI.
You can use deploy instead of update-stack:
aws cloudformation deploy \
--template-file serverless-output.yaml \
--stack-name new-stack-name \
--capabilities CAPABILITY_IAM
This command is necessary because Transforms need to be applied using change sets, which the deploy command automates for you. Refer to Working with Stacks that Contain Transforms for further discussion:
To create or update a stack with transforms, you must create a change set, and then execute it. A change set describes the actions AWS CloudFormation will take based on the processed template. During processing, AWS CloudFormation translates AWS SAM syntax into syntax that is defined by the transform. Processing can add multiples resources that you might not be aware of. For example, the specialized AWS::Serverless::Function resource adds an AWS Identity and Access Management (IAM) execution role and a Lambda function.
To ensure that you're aware of all of the changes introduced by transforms, AWS CloudFormation requires you to use change sets. [...]
If you use the AWS CLI, you can use the package and deploy commands to reduce the number of steps for launching stacks with transforms.
Try with deploy instead of update-stack
aws cloudformation deploy \
--template-file serverless-output.yaml \
--stack-name new-stack-name \
--capabilities CAPABILITY_IAM

Resources