Demystifying APIs: How They Work with AWS Services.
Before you read this session, please read the previous session. Let's begin the world of cloud computing using Amazon web services...
In today's interconnected digital landscape, APIs (Application Programming Interfaces) are the backbone for facilitating smooth communication among diverse software systems. Let's delve deeper into the fundamental concepts of APIs and explore their functionality within AWS services, including API Gateway and S3 buckets.
-> Think of an API as a waiter at a restaurant. You tell the waiter what you want (request), and the waiter takes your order to the kitchen (server) and brings back your food (response). Similarly, an API takes requests from users and returns relevant data or performs actions.
-> AWS API Gateway acts as a gatekeeper for your APIs. It helps create, publish, maintain, monitor, and secure APIs at any scale. It's like having a centralized control point for managing the entry points (endpoints) to your services on AWS.
When we interact with a website or API, we use specific verb actions like GET, POST, PATCH, etc., along with a route/path/endpoint/resource path. This helps define what action we want to perform (like getting data, posting data, updating data, etc.) and where we want to perform it within the API's structure.
The "request-response" method is fundamental in network communication. Here's how it works: the first computer (client) sends a request for data or an action to the second computer (server) through the network. The server processes this request and responds to the client with the requested data or confirmation of the action performed. This back-and-forth communication is crucial for exchanging information between different systems in a network.
1. RESTful API: Imagine an e-commerce website where you can view products (GET), add items to your cart (POST), update your shipping address (PUT), or remove items from your cart (DELETE). These actions correspond to the standard HTTP methods used in RESTful APIs.
2. SOAP API: In a healthcare system, SOAP APIs can be used for exchanging patient information between different hospital systems. For example, retrieving patient medical records or updating treatment plans using XML-based SOAP messages.
3. GraphQL API: Consider a social media platform where users can query specific information about their friends, such as their posts, comments, and photos. GraphQL allows clients to request only the data they need, optimizing the data exchange process.
4. WebSocket API: A live chat application uses WebSocket APIs to enable real-time messaging between users. Messages are sent and received instantly over a persistent connection, providing a seamless chatting experience.
5. HTTP API: A serverless application for image resizing can use HTTP APIs to receive image URLs, process them, and return the resized images. HTTP APIs are lightweight and suitable for handling such requests efficiently.
6. AWS SDKs: An application that uploads files to an S3 bucket can use AWS SDKs (e.g., AWS SDK for Python) to interact with AWS services. The SDK provides pre-built functions and classes to easily manage S3 operations, such as uploading, downloading, and deleting files.
These examples demonstrate how different types of AWS APIs are used in various real-life scenarios to enable communication, data exchange, and functionality within applications and systems.
1. Choose an API type:
- Go to the AWS Management Console and navigate to API Gateway.
- Click on "Create API" and choose the type of API you want to create (REST, WebSocket, HTTP, etc.).
2. Create a new API and type the API name:
- Enter a name for your API, such as "MyLambdaAPI," and click on "Create API."
3. Create a Resource (Routing Path):
- In the API Gateway console, select your newly created API.
- Click on "Create Resource" to define a routing path. For example, if you want your API to respond to requests at "/myresource," create a resource with that path.
4. Create a Lambda function:
- Go to the AWS Lambda console and create a new Lambda function. Write your function code (e.g., a simple "Hello World" function).
- Remember to configure the function's permissions to allow API Gateway to invoke it.
5. Select Resource then click on Actions for creating methods:
- Back in the API Gateway console, select the resource you created in step 3.
- Click on "Create Method" and choose the HTTP method you want to use (GET, POST, etc.).
6. Choose integration type, lambda region, lambda function:
- After creating the method, choose "Lambda Function" as the integration type.
领英推荐
- Select the AWS region where your Lambda function is deployed and choose your Lambda function from the dropdown list.
7. Test API Gateway:
- Click on "Test" in the API Gateway console to test your API. Provide any required input parameters and see the response from your Lambda function.
8. After successful testing, deploy the API and get the invoke URL:
- Click on "Deploy API" and create a new stage (e.g., "prod").
- Once deployed, API Gateway will provide you with an invoke URL (e.g., https://abcdefghij.execute-api.us-east-1.amazonaws.com/prod/myresource).
9. Trigger the Lambda function using API Gateway:
- Use the invoke URL provided by API Gateway to make requests to your API. For example, if your resource path is "/myresource" and your API base URL is https://abcdefghij.execute-api.us-east-1.amazonaws.com/prod, hitting the URL https://abcdefghij.execute-api.us-east-1.amazonaws.com/prod/myresource will trigger your Lambda function.
10. Enable "Use Lambda Proxy integration" for event information:
- If you want to send event information to the Lambda log as input, go to your API Gateway console, select your method, and enable "Use Lambda Proxy integration."
- Redeploy your API Gateway after enabling this feature to apply the changes.
Following these steps will allow you to create an API Gateway, integrate it with a Lambda function, test the API, deploy it, and trigger the Lambda function using API Gateway.
When you hit the URL of an API that's integrated with Lambda using "Use Lambda Proxy integration," here's what happens:
1. Client to API Gateway Interaction:
- When you visit a URL linked to an API, you're like a customer visiting a shop (API Gateway).
- You request the shop (API Gateway), and it takes note of your request without changing anything.
2. Lambda Function Handling:
- Inside the shop (API Gateway), there's a worker (Lambda function) who receives your request exactly as you made it.
- This worker (Lambda function) can see details like your name, what you want, and any special requests (like your IP address).
3. Response from Lambda to Client:
- After the worker (Lambda function) processes your request, they give you a response.
- Normally, the shop (API Gateway) tells you if everything went well (status code 200) or if there was an issue.
- But with "Use Lambda Proxy integration," the worker (Lambda function) directly tells you how things went, without the shop (API Gateway) getting involved in the response details.
4. Return IP Address Example:
- For example, if you want to know your customer's address (IP address), the worker (Lambda function) can look it up and give it to you in their response.
- When you get the response, it includes the address (or any other information) the worker (Lambda function) provided.
So, "Use Lambda Proxy integration" means the shop (API Gateway) lets the worker (Lambda function) handle everything directly without interfering with the response details. This allows for more direct and specific responses tailored by the worker (Lambda function) based on your request.
When we hit the URL then every hit is an event for the lambda function and if we want some event information to send to the lambda log as input then we have to enable “Use Lambda Proxy integration” and then redeploy the API gateway.
Stay tuned for our next session, where we'll continue our journey into the vast world of cloud computing!
Exploring the depths of serverless tech reminds me of Aristotle's urging to pursue excellence. Just as AWS streamlines development, we too must continually evolve. ?? #InnovationThroughIntegration
Currently learning Linux | Kubernetes| Docker | Ansible | Jenkins | DSA
7 个月Good going
Senior Software Engineer @ LinkedIn | Front-end Development
7 个月Great post, would like to know your thoughts on frameworks like serverless, sst or anything else for this.