How to connect specific AWS API Gateway stage to specific AWS lambda alias - api

I have AWS Lambda function and I invoke it calling AWS API Gateway via REST API. For Lambda function I configured two aliases with versioning - QA and Prod.
API Gateway is configured with Lambda Proxy Integration Request. I know about AWS API Gateway stages feature and I create two stages (QA and Prod respectively), but I don't see any settings for Lambda aliases in stage configuration.
How can I specify QA Lambda alias for QA stage and Prod Lambda alias for Prod API stage?

On the integration request in API Gateway you can add the alias name to the end of the ARN to make sure your endpoint points to the correct alias. Something like this
arn:aws:lambda:region:account-id:function:function-name:alias-name
You can't tell an API Gateway stage to always use a specific Lambda alias and have multiple Gateway stages that all point towards different aliases.
Every time you need to deploy to a Gateway stage, you'll have to make sure your undeployed API Gateway endpoints are configured to point to the correct Lambda alias before deploying to the stage that matches that environment.
This get tricky to maintain, so I would recommend treating your two different stages as completely different resources using the serverless framework or another framework.
Useful resources:
https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arn-syntax-lambda
https://serverless.com/framework/docs/providers/aws/guide/intro/

On your integration request, you will see the Lambda Function - click on it to edit and add :QA or :Prod after it and hit the checkmark, I think that will do it.
example: myLambda:Prod

Related

How to combine multiple API endpoints?

I joined a project, that already has built all the microservices backends, and they have many API endpoints, something like below:
example1.com/api/a
example2.com/api/b
example3.com/api/c
example4.com/api/d
Recently, the manager of the company asked me to aggregate all the endpoints into one, how can we have just one API endpoint?
Something like below:
example.com/api/a or b or c/*
Is this even possible without help of developers? I mean, no code side changes?
Some of the ideas I have are.
Nginx proxy in front
API GW (but not sure which one suite best for this)
If you just want to get one endpoint and not to aggregate and merge data in one service, AWS API Gateway will help you. It will be a single entry point for client applications and you can re-route multiple requests on multiple backend services on gateway without changing any code.
You can do some integration on API Gateway:
https://api-gw.example.com/users -> integration request on service: example1.com/api/a
https://api-gw.example.com/orders -> integration request on service: example2.com/api/b
Additionally, you can have single authorization mechanism for these resources on Gateway, like Cognito, AWS_IAM, or Custom Authorization.
If you need to aggregate some API responses, you can use lambdas or BFF Pattern.

Can AWS Lambda act as an API?

Trying to figure out the differences. Can AWS Lambda act as an API or vice versa? Would I still need to setup an API if I wanted to use Lambda functions or can I use Lambda functions right from the get go?
AWS Lambda can be used to serve an API. You could use API Gateway for setting up the API, and the serving can be done via a Lambda. It is a nice pattern.
Ref: https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html
And to your main question, I don't think you can directly configure an API in AWS Lambda service.
Alternatively, you can do it by setting a Function URL to your lambda.
It will require changes in how you receive input and send the output for the lambda and also has some limitations related to authentication. Still, it would be a more self-contained approach than the API Gateway integration if you need to expose your API publicly.
Lambda functions can now be accessed directly with a Function Url. They have minimal functionality when compared to Amazon API Gateway.
However, since the integration event is effective the same. It is very easy to go from Function Urls to API Gateway or ALB in the future if you require greater functionality.

Google Cloud Run API - accessing endpoint internally

Bear with me, I am still on training wheels with GCP
Scenario being
I have a Cloud Run instance serving an API (to be consumed internally)
A middleware running on Cloud Compute instance serving an API. This utilises API served by (1)
(2) needs to access (1)
I'm trying to figure out how to discover the internal DNS name or IP, so that the middleware (2) can be configured to access Cloud Run served API (1)
Intention being to create templates (environment config files) so that I can eventually automate the deployment of all layer.
With Cloud Run, you can't know before the first deployment the URL of the service. The pattern is
https://<serviceName>-<projectHash>.<region>.run.app
You can also have optionally a tag at the beginning, but it's not important here.
The bad part is the project Hash can't be calculated before the deployment (or I donc know the hash formula). Thus it's impossible to register in a DNS the URL in advance of the deployment.
However, if you use tools like terraform, you can get, as output, the URL of the service after the deployment and then register it in your DNS, as CNAME (Cloud Run is a managed service, you haven't a static IP).
You can also use the CLoud Run API to request the list of services on a project, pick the service that you want and get the URL of the service (get request to the service API)
A last word before you hit the wall, you talk about internal endpoint. Cloud Run url is public, and public only. Therefore, your middleware need to access to the internet to be able to request Cloud Run.
If your middleware is deployed on the same project, you can set the Cloud Run service ingress to internal to allow only resources from the VPCs of the current project to access to Cloud Run service.
But the URL is still public and you need an internet access to resolve and to access it.
If you don't wanna be bothered by service discovery and constructing the full URL, you can use runsd, a solution that I developed for Cloud Run that allows you to call a service by its name (e.g. http://hello).

Invoke AWS Step Function from AWS Lambda Proxy (AWS API Gateway) .NET SAM Template

I have a AWS API Gateway, deployed using SAM template. The API request comes to the Proxy Lambda Function. From there I need to call a AWS Step Function which invokes multiple Lambda Functions. I have multiple Solutions. Following Microservices pattern. Need to call one microservice from another. Each Microservice is in a seperate solution and the startup project is a ClassLibrary(.NET Core 2.1). Using SAM template and deploying it via AWS Toolkit for Visual Studio. Not using Fargate Containers and WebApi projects. Need to coordinate between API Gateways.
In your question you say: "The API request comes to the Proxy Lambda Function. From there I need to call a AWS Step Function". It is simple, here an example of api gateway that use a lambda like authorizer and in the method execution I call a step function. In your step function later you manage your flow and your and the lambdas that you need execute

same domain name for lambda function api endpoints(backend) and for frontend

How to use same domain name for front end and for lambda function endpoints
for Serverless framework ?
I am using reactjs for frontend design and for frontend hosting I am using s3 and Aws Dynamodb for lambda functions.
We do the same in our systems, AWS solved it long time back.
It is the cloud service called CloudFront, which lets you connect multiple origins including external origins that are outside of AWS cloud.
Created a simple architecture diagram to help you view the same.
Hope it helps.