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.
Related
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.
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
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.
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.
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