Wdrażanie aplikacji SAM
Używaj SAM CLI do budowania, lokalnego testowania i wdrażania aplikacji serverless w AWS oraz zarządzania różnymi środowiskami.
Wdrażanie aplikacji SAM to bezpłatna lekcja Serverless Backend with AWS Lambda & API Gateway na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless Backend with AWS Lambda & API Gateway, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Introduction to SAM CLI
Welcome to deploying SAM applications! The AWS Serverless Application Model Command Line Interface (SAM CLI) is your best friend for building and managing serverless projects.
It extends AWS CloudFormation to simplify defining serverless resources. The SAM CLI provides commands to build, test, and deploy your serverless applications.
Building Your Application
Before deploying, your SAM application needs to be built. The sam build command takes your application code and dependencies, then prepares them for deployment.
- It packages dependencies (e.g., Python libraries).
- It transpiles code if needed (e.g., TypeScript to JavaScript).
- It creates a deployable artifact in a
.aws-sam/builddirectory.
Example Lambda Function
Let's look at a simple Python Lambda function and its template.yaml. The sam build command processes this to create deployable code.
After running sam build, the packaged code would be ready for local testing or deployment.
import json
def lambda_handler(event, context):
"""
A simple Lambda handler.
"""
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from your SAM Lambda!"
})
}Local Testing with `sam local invoke`
The SAM CLI allows you to test your Lambda functions locally without deploying them to AWS, saving time and costs. Use sam local invoke to simulate a Lambda invocation.
You can pass event data directly to your function, just like how AWS services (like API Gateway or S3) would trigger it.
Running a Lambda Locally
To test the Lambda function from the previous scene, you'd use sam local invoke with the logical ID of your function and an event file.
For example, sam local invoke MyHelloWorldFunction --event event.json would test a function named 'MyHelloWorldFunction' using the data in event.json.
{
"httpMethod": "GET",
"path": "/hello",
"queryStringParameters": null,
"headers": {
"Accept": "*/*"
},
"body": null
}Local API Testing with `sam local start-api`
If your SAM application includes an API Gateway, you can simulate the entire API locally using sam local start-api. This command starts a local web server that mimics API Gateway.
You can then send HTTP requests to http://127.0.0.1:3000 and test your API endpoints end-to-end, locally.
Preparing for Deployment: `sam deploy`
Once your application is built and tested locally, you're ready to deploy it to AWS. The sam deploy command is used for this.
It packages your application artifacts, uploads them to an S3 bucket, and then uses AWS CloudFormation to deploy your serverless resources.
Your First Guided Deployment
For your first deployment, using the --guided flag is highly recommended. It will walk you through setting up necessary parameters interactively.
- Stack Name: Unique identifier for your CloudFormation stack.
- AWS Region: Where to deploy your resources.
- S3 Bucket: For storing deployment artifacts.
- Capabilities: Acknowledging IAM resource creation.
Example: sam deploy --guided
Managing Multiple Environments
In real-world scenarios, you'll often have development, staging, and production environments. SAM CLI helps manage this by allowing you to specify different stack names and parameters.
- Use distinct Stack Names (e.g.,
my-app-dev,my-app-prod). - Use
--parameter-overridesto pass different values for environment-specific configurations (e.g., database names, API keys).
Quick Check
Which SAM CLI command is used to test your API Gateway endpoints locally?
Recap & Next Steps
Congratulations! You've learned the essential steps for building, testing, and deploying serverless applications using the SAM CLI.
sam build: Packages and prepares your application.sam local invoke: Tests individual Lambda functions locally.sam local start-api: Simulates API Gateway for local API testing.sam deploy: Deploys your application to AWS CloudFormation.
Mastering these commands is key to efficient serverless development!
Często zadawane pytania
Czy lekcja „Wdrażanie aplikacji SAM” jest bezpłatna?
Tak — pełny tekst „Wdrażanie aplikacji SAM” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless Backend with AWS Lambda & API Gateway, przejdź na CoddyKit PRO. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Co nauczysz się w „Wdrażanie aplikacji SAM”?
Używaj SAM CLI do budowania, lokalnego testowania i wdrażania aplikacji serverless w AWS oraz zarządzania różnymi środowiskami. Ćwiczysz Serverless Backend with AWS Lambda & API Gateway z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Serverless Backend with AWS Lambda & API Gateway?
Nie wymagamy żadnego doświadczenia. Serverless Backend with AWS Lambda & API Gateway w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Wdrażanie aplikacji SAM”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Serverless Backend with AWS Lambda & API Gateway?
Tak. Każda lekcja Serverless Backend with AWS Lambda & API Gateway zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do AWS SAM
- Definiowanie zasobów serverless
- Wdrażanie aplikacji SAM
- Lokalne testowanie i debugowanie za pomocą SAM CLI