Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære AWS SAM for Serverless Workloads | Section
AWS Foundations & Developer Toolkit

AWS SAM for Serverless Workloads

Sveip for å vise menyen

Deploying a Lambda Function with SAM

Let's walk through deploying a simple Lambda using AWS SAM.

Step 1: Initialize the Project

sam init

This sets up a project with:

  • template.yaml: defines your infrastructure;
  • hello_world/: contains your Lambda function code.

Step 2: Create the Lambda Function

Inside hello_world/app.py, define the function:

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "Hello from Lambda!"
    }

Step 3: Define Infrastructure in template.yaml

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
  • CodeUri: Points to the function code;
  • Handler: Specifies which file and function to run;
  • Runtime: Defines the language runtime.

Step 4: Build the Project

sam build

This packages the application and prepares it for deployment.

Step 5: Deploy the Lambda

sam deploy --guided

The --guided flag walks you through:

  • Stack Name – e.g., HelloWorldStack;
  • AWS Region – e.g., us-east-1;
  • Confirm changes before deploy? – Yes;
  • Allow SAM CLI IAM role creation? – Yes;
  • Preserve the state? – No;
  • Save arguments to a samconfig.toml file? – Yes.

Once confirmed, SAM deploys:

  • The Lambda function;
  • The IAM role;
  • API Gateway endpoint;
  • Other associated resources.

Deployment Output

During deployment, you'll see updates:

  • IAM Role Created;
  • REST API Created;
  • Lambda Permission Created;
  • Gateway Deployment Created.

Deployment finishes successfully in a few minutes.

Recap

With infrastructure as code:

  • You can consistently reproduce environments across regions or stages;
  • You eliminate manual provisioning;
  • You version and track infrastructure like application code.

That's the power of infrastructure as code!

1. What is the main advantage of using Infrastructure as Code (IaC)?

2. Which of the following tools is used for declarative Infrastructure as Code in AWS?

3. What does the Handler property in AWS SAM's template.yaml file specify?

4. Which AWS service is used to package and prepare the application for deployment

when using SAM?

5. What is the purpose of the sam deploy --guided command?

6. Which of the following is a feature of programmatic Infrastructure as Code?

7. What is the primary difference between declarative and programmatic Infrastructure

as Code?

question mark

What is the main advantage of using Infrastructure as Code (IaC)?

Velg det helt riktige svaret

question mark

Which of the following tools is used for declarative Infrastructure as Code in AWS?

Velg det helt riktige svaret

question mark

What does the Handler property in AWS SAM's template.yaml file specify?

Velg det helt riktige svaret

question mark

Which AWS service is used to package and prepare the application for deployment when using SAM?

Velg det helt riktige svaret

question mark

What is the purpose of the sam deploy --guided command?

Velg det helt riktige svaret

question mark

Which of the following is a feature of programmatic Infrastructure as Code?

Velg det helt riktige svaret

question mark

What is the primary difference between declarative and programmatic Infrastructure as Code?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 13

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 13
some-alt