run golang gin api project using aws lambda - go

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

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

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: CodeDeploy for Lambda can't read appspec

I'm attempting to setup CodePipeline to manage the deployment of a very simple Lambda function.
I'm completely stuck on a problem with the deployment step, and cannot figure out what could be wrong.
When the pipeline attempts to run the CodeDeploy action, it fails with the error...
BundleType must be either YAML or JSON
This is my appspec...
version: 0.0
Resources:
- my-function:
Type: AWS::Lambda::Function
Properties:
Name: "my-function"
My pipeline doesn't have a build step, as it's just a simple js file, with no dependencies, so no build is required.
I've tried adding an action to deploy to S3, and I can confirm that the zip file that's being sent to s3 contains the appspec.yml and index.js and that these are both in the root.
Most of the examples I've seen use a buildspec, but I'm not sure why I would need this, or what it would even do if I had one.
There is nothing wrong with your setup, it is a shortcoming of the services that you cannot use CodeDeploy in a CodePipeline action to Deploy a Lambda function.
The reason is because CodeDeploy expects a JSON or YAML appspec file for the Lambda deployment, but currently CodePipeline supports ZIP as a bundle type so the error is thrown.
To workaround, customers deploy Lambda in a CodePipeline is via CloudFormation deploy action (SAM to be exact). Please see this tutorial on this recommended approach:
https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

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

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