Using openapi definitions aws api gateway - amazon-web-services

I'm new to api gateway stuff and have created an api gateway using terraform on aws. Now I want to expose more apis on the same api gateway. I have used the body argument on Terraform which takes a definition file. If I use a new open api definition, my existing ones are replaced by Terraform. What is the best way in this situation. Appending new apis to openapi file or?
please advise.

Related

How to use CloudFromation to update a single API Gateway consists of multiple services

I have a single api gateway consists of several services's endpoints (let's say service A, B, C, D). When a single service A update, I would like to dynamically update the api gateway for service A only and keep service B, C, D unchanged. And I have a maven plugin to generate swagger file for each service.
To make this work, I have to solutions:
Use AWS CLI to do api gateway update in merge mode by swagger file of service A and do redeployment.
Merge all swagger files of service A, B, C, D to a single swagger file and upload this single swagger file to S3 bucket. Then I can use CloudFormation template to create api gateway (with S3BodyLocation to my swagger file in previous step) and redeploy.
My question is, is there an easier way that I can use CloudFormation to update api gateway for single service like aws cli without need to merge all services' swagger files? Or any other solution that can achieve my goal? If not, which of my solution is better? Thank you.
PS. I am also curious about how people using single api gateway for microservices, for me it looks like there is no way to avoid merging multi swagger files from all services before using it to create api gateway.
With AWS::Serverless::Api, you should use DefinitionBody property.
Here comes excellent example Realworld-serverless.
You could look at SAM which will help you to generate your API Gateway a little simpler than traditional CloudFormation but will still generate the CloudFormation stack for you.
Take a look here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html

How to properly expose APIs from Fargate tasks?

I deployed few microservices into Fargate. Each microservice will have around 30 API endpoints.
I have AWS ALB which will do the path based routing to the Fargate.
I created API Gateway APIs to externally expose the APIs. API Gateway integration method is HTTP and that points to the ALB endpoint.
Is this the proper way to setup the microservices? If not, please suggest the better approach.
And also, I want to automatically import the Swagger definition into API Gateway whenever there is a change in the Swagger definition. Swagger definition is exposed under /apidocs of each microservice. How do I automate the import of swagger definition into API gateway? Is there a commonly used approach?
Keep in mind you'll need to update both API GW and ALB paths. To import new swagger definition you'll need some kind of triggered event to:
Upload new swagger definition to S3.
Create an API gateway deployment.
Perform the path edition in ALB target group(s). This one could be done by triggering a CLI or API call (or sdk).
If you use CloudFormation, you could trigger an UPDATE containing all these new changes too. If so, also keep an eye on those resources parameters that may or not require a resource replacing.
Hope it helps.

how does serverless invoke in aws

I am new to this whole serverless framework. I created my first serverless function as documented here https://www.npmjs.com/package/serverless#quick-start. Next when I do a "serverless invoke" it works. I am confused how this works, the questions I have around this are
There does not seem to be an API gateway created so how can it invoke?
There are also stages mentioned in the serverless.yml file, I'm not sure what these translate to
Any help on this is highly appreciated.
First of all the default code comes with the AWS template, is only having a Lambda function declared. Let me try to answer your questions inline.
There does not seem to be an API gateway created so how can it invoke?
Yes, since there is no API Gateway created, its not possible to invoke the Lambda through URLs. However, it is possible to invoke the Lambda using AWS CLI or SDKs which is what Serverless Framework is providing with "serverless invoke". To create a API Gateway, you need to add an event object to the function code as shown below.
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
There are also stages mentioned in the serverless.yml file, I'm not
sure what these translate to
When you define a stage in serverless.yml file, after the deployment, it creates a stage in API Gateway including it in the API Gateway URL path as shown below.
https://your-api/<stage-you-defined>/resurce-methods
Note: that if you setup a custom certificate for API Gateway, then you have option to setup your own custom paths.
Also its important to note that, although API Gateway supported this feature to have different stages(e.g test, staging, production) of a Single API Gateway deployment, latest Serverless Framework doesn't use this feature. Instead when you define a new stages, it will deploy a whole new API Gateway with the new stage. Serverless Framework has the argument for separating the API Gateway and having a single stage to self-contain each stage for isolation.
You can attach an API gateway for invoking your lambda
Or
You can get event driven. Where your lambda gets invoked in response to some events like a new message in AWS SNS or when a new object gets created in S3
Or
You can have scheduled invocations using cloudwatch trigger events
For a comprehensive list of events that can invoke lambda see Invoking Lambda Functions
As documented in AWS Regions and Endpoints there are HTTPS endpoints for Lambda. For example, in the us-east-1 region the endpoint is https://lambda.us-east-1.amazonaws.com. This is how you're able to call a Lambda directly without the API gateway. API gateway can add additional functionality and puts a full HTTP protocol on top of a Lambda.

How to describe AWS GatewayAPI in file and import?

We are using Amazon Gateway API and currently we describe API endpoints manually through the web console.
Is it possible to create definition of API in some file(s) and import it?
Why do we need this:
We want every change in the API be reviewed (it's our development process) by other people.
In case if API is deleted or broken accidently, we want to be able to restore it easily.
By now the only solution I see is to write script, based on aws apigateway command line command that creates all resources and methods.
But probably there is a better way to do it?
Thanks!
P.S. It may partially overlaps with this question: exporting api definition from AWS api gateway.
I think the Swagger Importer feature of AWS API Gateway is what you are looking for: https://aws.amazon.com/about-aws/whats-new/2015/07/introducing-swagger-importer-easily-import-swagger-api-definitions-into-amazon-api-gateway/
You can export your API definition with API Gateway extensions into swagger format. Then you use the API Gateway API importer to import/update your API.
If you are backing your API Gateway endpoints with Lambda functions you may want to check out the serverless project https://github.com/serverless/serverless
Using this framework you have a JSON file that describes your endpoints and binds them to your Lambda code in the same project structure. The tool lets you deploy the endpoints or code from the command line. It also allows you to manage other AWS resources in a CloudFormation template in the same project structure and deploy it from the command line.

AWS API Gateway - HTTP Proxy for all Methods of a resource

Is there a way to configure the HTTP Proxy for all methods of resource at once so
FIRST:
GET,POST,PUT,DELETE of /v1/resource should all be forwarded to http://api.com/resource/{id}
If that is possible can I still map:
GET /v1/resource/schema to an S3 ?
or do i have to define them one by one.
The emergency way to save some time is probably to generate a swagger API documentation and create the API Gateway setup of it.
API Gateway doesn't support to set up multiple methods for one resource at once. As you already mentioned, you could create a Swagger template and use the API Gateway Importer tool (https://github.com/awslabs/aws-apigateway-importer).
Best,
Jurgen