eolas/DevOps/AWS/AWS_SAM/AWS_SAM.md
2023-04-14 20:19:01 +01:00

3.2 KiB

categories tags
DevOps
Backend
AWS

AWS SAM

SAM stands for serverless application model. It is a framework developed by AWS to simplify the process of building, deploying and managing serverless applications. It provides a concise syntax for defining the components of a serverless application, such as Lambda functions, API gateway and database tables.

The SAM infrastructure is defined in a YAML file which is then deployed to AWS. SAM syntax gets transformed into CloudFormation during the deployment process. (CloudFormation is a broader and more robust AWS tool for large, highly scaleable infrastructures).

Key features of SAM

  • Single deployment configuration
  • Integration with development tools
  • Local testing and debugging
  • Built on AWS CloudFormation

Main technologies required

Docker

Whilst SAM can be used to create a deployable file for AWS it can also be run as a container for local development with Docker.

AWS CLI

This is installed using Python and allows you to interact directly with AWS via the command-line.

AWS SAM CLI

See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html

Setting up credentials for the AWS CLI

aws configure
AWS Access Key ID [None]: AK*******
AWS Secret Access Key [None]: ukp******
Default region name [None]:
Default output format [None]:

This information can be found in the Security Credentials section of the given IAM user:

Starting a SAM project

First create a directory for your project which will serve as the repository:

mkdir aws-sam-learning
cd aws-sam-learning

Then we can use the sam cli to bootstrap the project:

sam init --runtime nodejs16.x

We can just click through and accept the basic HelloWorld Lambda.

This will create the Lambda as well as an API Gateway trigger URL.

template.yaml

This is autogenerated and details the main constituents of the project. There are lots of fields but the most important are the following:

HelloWorldFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: hello-world/
    Handler: app.lambdaHandler
    Runtime: nodejs16.x
    Architectures:
      - x86_64
    Events:
      HelloWorld:
        Type: Api
        Properties:
          Path: /hello
          Method: get

This details the location of the handler function which is contained at the path hello-world/app.js:

exports.lambdaHandler = async (event, context) => {
  try {
    // const ret = await axios(url);
    response = {
      statusCode: 200,
      body: JSON.stringify({
        message: "hello world",
        // location: ret.data.trim()
      }),
    };
  } catch (err) {
    console.log(err);
    return err;
  }

  return response;
};

It also lists the get event that we can use to call API Gateway and trigger the Lambda.

The full template is below: