SAM CLI for running Lambda functions locally fails when building docker image with x86_64 problem - aws-lambda

Host Machine: Mac M1
SAM version: 1.70.0
CDK version: 2.60.0 (build 2d40d77)
In our api_stack.py file we have a simple setup of
API Gateway REST endpoints
Lambda functions triggered by API Gateway
The entire codebase was initially setup with cdk init
When we run cdk synth, we get a files generated in the folder cdk.out including the api.template.json which is the CloudFormation template file.
Following commands all work:
cdk synth
cdk deploy
However when we run sam local invoke or sam local start-api, I get an error that looks like below.
% sam local invoke GetUserTemporary -t cdk.out/api.template.json -e events/get_user/prod-user.json
Invoking get_user_temporary.handler.handle (python3.9)
DependenciesLayerDF300E31 is a local Layer in the template
Local image was not found.
Building image.......................................................................................................................................................................................................................................................................................................................
Failed to build Docker Image
NoneType: None
Error: Error building docker image: The command '/bin/sh -c mv /var/rapid/aws-lambda-rie-x86_64 /var/rapid/aws-lambda-rie && chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
This used to work just about 1 week ago and now it is showing this error all of sudden. Exact same codebase is git cloned in my co-workers' machines.
Windows
Mac M1
And this all works for them. I try to do this in my M1 laptop and it fails now.

It turns out you have to specifically specify what architecture you are using when defining your lambda functions in your CDK Python source code. You can achieve this by adding the architecture= argument like below.
fn = _lambda.Function(
self,
func_name,
runtime=_lambda.Runtime.PYTHON_3_9,
code=_lambda.Code.from_asset('api/lambda_fns'),
handler=f'{func_path}.handle',
layers=[self.layer],
role=role,
architecture=_lambda.Architecture.X86_64,
)

Related

CDK: which command should I run after changing my code?

I'm running aws serverless using aws cdk and aws-sdk.
I wrote my code and then ran the following commands:
cdk synth
cdk deploy
Now I update the code locally on my machine and want to push the changes.
Which command/s should I run now?
Should I run cdk destroy in between?
Thanks
Running cdk deploy will first synthesize the stacks and then deploy the changes. No need to run synth prior.
Deploying will apply the current stack and destroy any recourses that are no longer in the code, so no need to run destroy first.
Use cdk watch. Cdk will observe the files you specified in your cdk.json file and automatically deploy your changes, which also makes it much faster.
Here is the cdk watch documentation:
https://cdkworkshop.com/20-typescript/30-hello-cdk/300-cdk-watch.html#cdk-watch (start here)
https://aws.amazon.com/blogs/developer/increasing-development-speed-with-cdk-watch/
Just run the same commands again
cdk synth
cdk deploy

Using a bash script in AWS CICD Code Pipeline

In my company we have to use AWS native for the CICD process. I'm used to deploying my services via a bash script which makes deployments very flexible. However, I can't seem to find an option in AWS CodePipeline in which I can call on a script. In the build phase I am able to call on a buildspec.yml file, but in the deploy phase I would like to be able to call on a bash script. Does anyone know if that's possible using AWS CodePipeline?
This is what I put in the deployspec.yml to make it call on a bash script:
version: 0.2
env:
shell: bash
phases:
install:
runtime-versions:
python: 3.8
commands:
- source deployspec.sh && create-stack-name && upload-to-s3 && create-cf-stack

run golang gin api project using aws lambda

I am tring to run my go project using amazon lambda, this is my current main.go
https://gist.github.com/krakiun/61e4e4dc5ab91f557e481f0230ed3ba0
I tried several methods, but none worked
How i can make run this project in lambda, in this moment if i run using router.Run(cfg.HTTP.ListenAddr) is working without any error,
with this log.Fatal(gateway.ListenAndServe(cfg.HTTP.ListenAddr, router)) is die with this error :
expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined
exit status 1
How i can fix my code to run in aws lambda ?
You have to separate the environment
the below code for run at local
router.Run(cfg.HTTP.ListenAddr)
the lambda can only run on the AWS Lambda Function. You must deploy it to lambda
lambda.Start(router)
You can check the example at https://maxrohde.com/2021/05/01/lambda-go-starter-project/. And the source code is here: https://github.com/mxro/go-lambda-starter-project/tree/main/packages/lambda-go-gin

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?

Testing AWS Lambda functions locally - invalid function name?

So, I'm finally at the point where I can test my first Lambda function locally. As background, I've installed the AWS CLI under MacOS 10.14.2 (Mojave) and am able to access my AWS account. I've successfully zipped up my Lambda function and used 'aws lambda create-function' to deploy it.
I've installed aws-lambda-local (https://www.npmjs.com/package/aws-lambda-local) using 'npm install -g aws-lambda-local'.
But when I invoke the following from the Lambda function root:
lambda-local -l index.js -e event.json
I get the following error:
Invalid function name. It should be accessible from invocation place
Would someone please tell me why this is happening? I mean, the function name is most definitely valid.
Totally confused here!
According to the aws-lambda-local docs, there is no -l option. Use -f or --function to specify the file with the lambda function

Resources