Integration of API gateway with Lambda using CloudFormation

CloudFormation: AWS CloudFormation gives you an easy way to model a collection of related AWS and third-party resources, provision them quickly and consistently, and manage them throughout their life cycles, by treating infrastructure as code. A CloudFormation template describes your desired resources and their dependencies so you can launch and configure them together as a stack. You can use a template to create, update, and delete an entire stack as a single unit, as often as you need to, instead of managing resources individually. You can manage and provision stacks across multiple AWS accounts and AWS regions.



API Gateway: Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the "front door" for applications to access data, business logic, or functionality from your backend services. Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and server less workloads, as well as web applications.


Lambda: AWS Lambda is a server less compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic.

Step 1: First of all, we need to create an API Gateway REST API, a resource that contains a collection of API Gateway resources ("Type": "AWS::ApiGateway::RestApi").


Step 2: As with all AWS resources, the API Gateway needs permission to execute the Lambda function ("Type": "AWS::Lambda::Permission").


Step 3: Create a logging for the API that will store all the requests processed. This will be done through cloud watch by making a role for API gateway which has the policy to create and put logs in cloud watch.


Step 4: Now to integrate the API with Lambda, API gateway has the property known as:

"Integration": {

"Type": "AWS",

"IntegrationHttpMethod": "POST",

"Uri": {"Fn::Join" : ["",

["arn:aws:apigateway:", {"Ref":"AWS::Region"},":lambda:path/2015-03-31/functions/"{"Fn::GetAtt": ["GreetingLambda", "Arn"]}, "/invocations"]

]},


In this, we will specify our lambda function are in “URI” property so it will manage to trigger or call the lambda function. It will then be called integration because this property plays a major role behind all the architecture.


Step 5: API Gateway Deployment resource deploys an Amazon API Gateway RestApi resource to a stage so that clients can call the API over the Internet. It will help us stage our api according to our environment in which we are working ("Type": "AWS::ApiGateway::Deployment").


It's always preferable to use CloudFormation (or Terraform to be Cloud agnostic) as it's easier and better to provision resources as code due to reasons mentioned above mainly speed and ease of deployment.



References:

- https://aws.amazon.com/api-gateway/

- https://blog.jayway.com/2016/08/17/introduction-to-cloudformation-for-api-gateway

- https://bl.ocks.org/magnetikonline/c314952045eee8e8375b82bc7ec68e88


112 views0 comments

Recent Posts

See All