Update cloudformation stack from aws cli with SAM transform - aws-lambda

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

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.

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?

AWS: Help setting up CodeDeploy in a Codepipeline

It looks like it's impossible to get Codedeploy to work in a CodePipeline project with a CodeBuild.
First I set up a Pipeline with 3 stages: Source, Build and Deploy, the first 2 stages work perfectly but the 3th (CodeDeploy) throws this error:
CodeBuild pushes the output artifacts to s3 in a .zip file, which is not supported by CodeDeploy.
For this, I tried to set up a Lambda function between CodeBuild and CodeDeploy like this: (Source -> CodeBuild -> Invoke Lambda -> CodeDeploy), The Lambda function uploads the appspec.yml file to s3 and calls putJobSuccessResult, But I still get the same error.
BundleType must be either YAML or JSON
There is a known limitation where the deployment of a Lambda using CodePipeline, with CodeDeploy as the Deployment Provider is not supported as of yet.
This is because CodePipeline will always zip the bundle/artifact, whereas CodeDeploy expects a YAML/JSON file as the source (appspec.yaml file) for Lambda Function deployment.
In order to work around this limitation, you have two options:
Run AWS CLI commands inside your CodeBuild Stage to update/deploy your lambda function
OR
Use CodeBuild to package your lambda function Code and push the artifact to a CloudFormation stage, which will update or create your Lambda Function Resource. You should find the reference documentation at [1] useful for getting the required information about packaging your SAM application.
Ref:
[1] SAM Packaging - https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-deploying.html#serverless-sam-cli-using-package-and-deploy

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

Resources