Testing and deploying your APIs

Developing applications on AWS

Ricardo Sueiras

Principal Technologist

Infrastructure-as-Code for developers

 

iac

  • Cloud native apps deploy using Infrastructure-as-Code (IaC).
  • Define cloud resources in configuration files.
  • Avoids manual setup in the AWS Management Console (clickops).
Developing applications on AWS

AWS CloudFormation

 

cloudformation

  • The foundational IaC service in AWS.
  • Define AWS resources with YAML or JSON templates.
Developing applications on AWS

AWS Cloud Development Kit (CDK)

 

cdk

  • CDK defines IaC using code.
  • Write in Python, TypeScript, or Java instead of YAML or JSON.
  • Apps synthesize into CloudFormation templates at deploy time.
Developing applications on AWS

AWS Serverless Application Model (SAM)

  • Extends CloudFormation.
  • Simplified syntax for serverless resources and APIs.
  • Acts as a transformation layer.
  • Translates SAM syntax into full CloudFormation templates.

 

sam

Developing applications on AWS

SAM features

  • Auto-generates least-privilege IAM roles, with room to fine-tune.
  • Define your deployment pipeline as configuration.
  • Package and deploy with sam build and sam deploy.
  • Catch template errors early with sam validate.
  • Emulate Lambda locally with sam local start-lambda.
    • Provides a persistent local endpoint

 

sam features

Developing applications on AWS

Testing with SAM

  • Build, invoke, and test serverless apps locally before deploy.
  • Test Lambda functions and API Gateway integrations.
  • Invoke a function locally with sam local invoke.
    • One-shot invocation
  • Emulate API Gateway with sam local start-api.

 

testing with sam

Developing applications on AWS

Unit tests

 

unit tests

  • Use SAM with unit tests to validate Lambda behavior.
  • Local testing speeds up development cycles.
  • Pair with frameworks like pytest, JUnit, or Jest.
Developing applications on AWS

Testing workflow

 

test workflow

  • Generate test events with sam local generate-event.
  • It outputs JSON event files.
  • Invoke locally with sam local invoke FunctionName -e event.json.
  • Add framework assertions to verify the response before deploy.
Developing applications on AWS

Mocking AWS services

 

mocking tests

  • Combine local testing with mocking libraries like moto.
  • Interact with simulated AWS services, no real calls.
  • Improves test speed.
  • Reduces testing costs.
  • Validates error scenarios like throttling or failures.
Developing applications on AWS

Continuous integration and continuous deployment (CI/CD)

  • SAM apps are commonly deployed via CI/CD pipelines.
  • Automate building, testing, packaging, and deploying.
  • AWS CodeCommit stores code and SAM templates.
  • AWS CodeBuild runs tests and sam build.
  • AWS CodePipeline orchestrates the workflow.

 

cicd

Developing applications on AWS

CI/CD workflow

  • Commit source code and SAM template to a repo.
  • Run automated tests and validation.
  • Package the app with sam build.
  • Deploy resources with sam deploy.
  • Monitor after deployment with CloudWatch.

 

workflow

Developing applications on AWS

Troubleshooting deployments

  troubleshooting

  • Troubleshooting is a key operational skill.
  • Common deployment failures:
  • IAM permission issues.
  • Invalid SAM or CloudFormation templates.
  • Failed Lambda packaging or missing dependencies.
  • Incorrect API Gateway configurations.
Developing applications on AWS

Troubleshooting deployments

  troubleshooting

  • SAM deployments run on CloudFormation under the hood.
  • Provides stack events and automatic rollback.
  • Helps identify deployment failures.
  • Rolls back to restore the previous environment state.
Developing applications on AWS

Troubleshooting deployments

  troubleshooting

  • Troubleshoot issues using:
  • CloudWatch Logs.
  • API Gateway execution logs.
  • Lambda logs.
  • CloudWatch metrics.
Developing applications on AWS

Let's practice!

Developing applications on AWS

Preparing Video For Download...