Testing and deploying your APIs
1. Testing and deploying your APIs
Welcome back! In this video we will look at how to deploy and test your APIs.2. Infrastructure-as-Code for developers
Modern cloud applications are deployed using Infrastructure-as-Code (IaC). IaC allows developers to define cloud resources using configuration files rather than manually creating infrastructure through the AWS Management Console (clickops).3. AWS CloudFormation
AWS CloudFormation is the foundational IaC service in AWS. In CloudFormation you define AWS resources using YAML or JSON templates.4. AWS Cloud Development Kit (CDK)
The AWS Cloud Development Kit (CDK) allows developers to define IaC using programming languages such as Python, TypeScript, or Java instead of YAML or JSON templates. CDK applications are synthesized into CloudFormation templates during deployment.5. AWS Serverless Application Model (SAM)
AWS Serverless Application Model (SAM) extends CloudFormation by introducing syntax for common serverless resources such as Lambda functions and API Gateway APIs. SAM simplifies deployment of serverless applications built on Lambda and API Gateway. SAM acts as a transformation layer that translates SAM syntax into verbose CloudFormation templates, which are then used to provision resources.6. SAM features
SAM provides a number of important capabilities. SAM auto-generates least-privilege IAM execution roles based on the resources you need and define, with the ability to fine tune as needed. SAM allows you to define your entire deployment pipeline as configuration and you can package and deploy applications using sam build and sam deploy. SAM provides sam validate to catch template errors before anything is deployed. SAM provides Local Lambda emulation using sam local start-lambda. This provides a persistent local endpoint.7. Testing with SAM
AWS SAM also provides tools for local testing and development. Developers can use the SAM CLI to build, invoke, and test serverless applications locally before deployment. AWS SAM allows developers to test Lambda functions and API Gateway integrations locally. You can do local Lambda testing using sam local invoke. This gives you a one-shot invocation versus the persistent local endpoint you get using sam local start-lambda. Emulate API Gateway using sam local start-api which allows developers to run API Gateway and Lambda integrations locally on their machine. This helps validate API behavior before deployment.8. Unit tests
AWS SAM can also be used with unit tests to validate Lambda function behavior. Local testing helps reduce deployment failures and speeds up development cycles. Developers commonly combine SAM with unit testing frameworks such as pytest (Python), JUnit (Java), and Jest (JavaScript /TypeScript). These frameworks are used to test Lambda function logic, validation, and error handling.9. Testing workflow
SAM simplifies creating test events using sam local generate-event. This outputs JSON files. You then invoke Lambda functions locally with sam local invoke FunctionName -e event.json. Combine this with test framework assertions to verify returned statusCode and body, headers, validation logic, and error handling before deployment.10. Mocking AWS services
Combine AWS SAM local testing with mocking libraries such as `moto` (Python) or `aws-sdk-client-mock` (JavaScript/TypeScript) to simulate AWS services during unit tests. These libraries allow Lambda functions running locally through AWS SAM to interact with simulated AWS services without making real AWS calls. This helps developers test application behavior safely and consistently while improving test speed, reducing costs, and validating error scenarios such as throttling or failed service responses.11. Continuous integration and continuous deployment (CI/CD)
Serverless applications built with AWS SAM are commonly deployed using Continuous Integration and Continuous Deployment (CI/CD) pipelines that automate building, testing, packaging, and deploying Lambda functions and API Gateway resources. AWS services commonly used in serverless CI/CD include AWS CodeCommit for storing application code and SAM templates, AWS CodeBuild for running tests and executing `sam build`, and AWS CodePipeline for orchestrating deployment workflows12. CI/CD workflow
A typical serverless CI/CD workflow looks like: First, Source code and SAM template committed to a repository. Then run automated testing and validation. Next is running `sam build` to package the application. You then run `sam deploy` to deploy Lambda functions and API Gateway resources. Finally, post-deployment monitoring using CloudWatch.13. Troubleshooting deployments
Validating and troubleshooting serverless deployments is an important operational skill. Deployment failures commonly occur because of the following: IAM permission issues, invalid SAM or CloudFormation templates, failed Lambda packaging, missing dependencies, and incorrect API Gateway configurations.14. Troubleshooting deployments
As AWS SAM deployments use AWS CloudFormation, this provides stack events and automatic rollback behavior to help identify deployment failures. If resource creation fails, CloudFormation commonly rolls back changes to restore the previous environment state.15. Troubleshooting deployments
To help you troubleshoot issues, developers will use CloudWatch Logs, API Gateway execution logs, Lambda logs as well as CloudWatch metrics.16. Let's practice!
OK, time to deploy your knowledge with a quick knowledge check.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.