Serverless Walkthrough on AWS - Part 1
Todd Bernson
Award Winning Technology Leader | AWS Ambassador | AWS Machine Learning Community Builder | Lifelong Learner | Data Analytics, ML, AI
Serverless computing is an innovative way of building applications where the cloud provider manages the infrastructure and servers, and the developers focus only on the application code. AWS Lambda is one of the most popular serverless compute services available on the market, which allows developers to write and run code without worrying about servers or infrastructure. Since learning is a vital part of our culture at Blue Sentry Cloud, we will explore using AWS Lambda and API Gateway to build a serverless architecture and how it benefits our future applications.
This first article will discuss what we will be building and what these services can do. The second article will show how we built this with Terraform and successfully ran our API call.
AWS Lambda
AWS Lambda is a compute service that runs your code in response to events and automatically manages the compute resources for you. It supports a wide range of programming languages such as Node.js, Python, Java, C#, and Go, among others. AWS Lambda functions are stateless, meaning they don't store data between invocations. Instead, they are designed to process a single request at a time and then return a response.
When you create an AWS Lambda function, you define a handler function that takes an input event and returns an output. The input event can be anything, such as an HTTP request, a file upload to S3, or a message from an AWS service like SNS or SQS. AWS Lambda automatically scales your function horizontally to handle a large number of requests in parallel. You can also specify a timeout for your function to limit its execution time and prevent it from running indefinitely.
API Gateway
AWS API Gateway is a fully managed service that makes creating, publishing, maintaining, and securing APIs at any scale easy. It supports REST and WebSocket APIs and lets you define your API's resources, methods, and integration points with other AWS services or Lambda functions. API Gateway also provides caching, throttling, and authentication features to help you control access to your APIs and ensure their performance and security.
When you create an API in API Gateway, you define the HTTP endpoints that clients can use to send requests to your API. You also specify the HTTP methods clients can use, such as GET, POST, PUT, and DELETE, and map each method to a corresponding Lambda function or integration with other AWS services. API Gateway automatically generates a unique URL for your API, which you can use to access your endpoints.
API Gateway and Lambda Integration
API Gateway and Lambda work together seamlessly to create a serverless architecture. When a client sends an HTTP request to an API Gateway endpoint, API Gateway maps the request to a corresponding Lambda function and passes the request payload as an input event to the function. The Lambda function processes the event, runs your code, and returns a response to API Gateway. API Gateway then formats the response into an HTTP response and sends it back to the client.
领英推荐
You can configure API Gateway to handle a variety of inputs and outputs, including query parameters, headers, and body content. You can also configure API Gateway to validate incoming requests and responses, transform data, and customize error messages. For example, you can use API Gateway to perform authentication and authorization checks, convert data formats between JSON and XML, and return custom HTTP error codes for specific situations.
Terraform Integration
Terraform is an infrastructure as code tool that enables you to describe your infrastructure in a declarative language and manage it programmatically. Terraform can create, update, and delete AWS resources such as Lambda functions, API Gateway APIs, IAM roles, and policies. Terraform simplifies managing infrastructure by treating it as code and automating the deployment process.
When you use Terraform to manage your AWS Lambda and API Gateway resources, you define your infrastructure as a set of declarative configurations. These configurations define the desired state of your infrastructure, and Terraform ensures that your resources are created, updated, or deleted to match the desired state. You can version control your infrastructure code, collaborate with other developers, and use Terraform to create a consistent, repeatable, and scalable deployment process.
We will use this same git repository to hold our lambda python code. This may not be desirable in a production environment.
Conclusion
AWS Lambda and API Gateway are powerful tools enabling you to build scalable, reliable, cost-effective serverless architectures. Combining AWS Lambda and API Gateway allows you to create APIs that process requests and return data without managing servers or infrastructure. This approach lets you focus on building your application logic, rather than managing infrastructure.
In addition, the integration of AWS Lambda and API Gateway provides a flexible and scalable microservices architecture. You can break down your application into smaller, independent services, each with its own function, and deploy them individually. This allows you to develop and deploy new features faster and scale each service separately according to usage.
Furthermore, Terraform simplifies managing AWS Lambda and API Gateway resources. You can use Terraform to automate the deployment and management of your serverless infrastructure, which reduces the likelihood of human error and ensures that your infrastructure is always up-to-date with the latest code and configurations. This approach lets you focus on developing your application logic, rather than managing infrastructure and deployments.
Overall, AWS Lambda, API Gateway, and Terraform provide a robust and flexible foundation for building serverless applications on AWS. By leveraging these services, you can build scalable, reliable, cost-effective applications that adapt to changing demands and requirements.