|
| 1 | +--- |
| 2 | +title: AWS SAM CLI |
| 3 | + |
| 4 | +author_primary: Jason Andrews |
| 5 | +minutes_to_complete: 15 |
| 6 | + |
| 7 | +official_docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html |
| 8 | + |
| 9 | +additional_search_terms: |
| 10 | +- AWS |
| 11 | +- Lambda |
| 12 | + |
| 13 | +layout: installtoolsall |
| 14 | +multi_install: false |
| 15 | +multitool_install_part: false |
| 16 | +test_maintenance: false |
| 17 | +tool_install: true |
| 18 | +weight: 1 |
| 19 | +--- |
| 20 | + |
| 21 | +The AWS Serverless Application Model (SAM) CLI is an open-source command-line tool that you can use to build, test, and deploy serverless applications. The SAM CLI provides a Lambda-like execution environment that lets you locally build, test and debug applications defined by AWS SAM templates. |
| 22 | + |
| 23 | +It is available for a variety of operating systems and Linux distributions and supports the Arm architecture. |
| 24 | + |
| 25 | +## Before you begin |
| 26 | + |
| 27 | +Follow the instructions below to install and try the latest version of the AWS SAM CLI for Ubuntu on Arm. |
| 28 | + |
| 29 | +Confirm you are using an Arm machine by running: |
| 30 | + |
| 31 | +```bash { target="ubuntu:latest" } |
| 32 | +uname -m |
| 33 | +``` |
| 34 | + |
| 35 | +The output should be: |
| 36 | + |
| 37 | +```output |
| 38 | +aarch64 |
| 39 | +``` |
| 40 | + |
| 41 | +If you see a different result, you are not using an Arm computer running 64-bit Linux. |
| 42 | + |
| 43 | +Running the AWS SAM CLI requires Docker. Refer to the [Docker](/install-guides/docker/) install guide for installation instructions. Confirm Docker is running before installing the SAM CLI. |
| 44 | + |
| 45 | +Python and Python pip are also required to run the SAM CLI example. |
| 46 | + |
| 47 | +To install, run the following command: |
| 48 | + |
| 49 | +```console |
| 50 | +sudo apt install python-is-python3 python3-pip -y |
| 51 | +``` |
| 52 | + |
| 53 | +## Download and install the AWS SAM CLI |
| 54 | + |
| 55 | +There are two options to install the SAM CLI, from a zip file or using the Python `pip` command. Select your preferred method. |
| 56 | + |
| 57 | +### Install from zip file |
| 58 | + |
| 59 | +Use `wget` to download and install: |
| 60 | + |
| 61 | +```bash |
| 62 | +wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-arm64.zip |
| 63 | +unzip aws-sam-cli-linux-arm64.zip -d sam-install |
| 64 | +sudo ./sam-install/install |
| 65 | +``` |
| 66 | + |
| 67 | +### Install using Python pip |
| 68 | + |
| 69 | +Install the SAM CLI using `pip`: |
| 70 | + |
| 71 | +``` |
| 72 | +sudo apt install python3-venv -y |
| 73 | +python -m venv .venv |
| 74 | +source .venv/bin/activate |
| 75 | +pip install aws-sam-cli |
| 76 | +``` |
| 77 | + |
| 78 | +### Confirm the SAM CLI is installed: |
| 79 | + |
| 80 | +```bash |
| 81 | +sam --version |
| 82 | +``` |
| 83 | + |
| 84 | +The version should be printed: |
| 85 | + |
| 86 | +```output |
| 87 | +SAM CLI, version 1.125.0 |
| 88 | +``` |
| 89 | + |
| 90 | +## Example application |
| 91 | + |
| 92 | +Use the AWS SAM CLI to build and deploy a simple "Hello World" serverless application which includes the line `uname -m` to check the platform it is running on. |
| 93 | + |
| 94 | +1. Create the project |
| 95 | + |
| 96 | +Adjust the runtime argument if you have a different version of Python. |
| 97 | + |
| 98 | +```console |
| 99 | +sam init --runtime python3.12 --architecture arm64 --dependency-manager pip --app-template hello-world --name uname-app --no-interactive |
| 100 | +``` |
| 101 | + |
| 102 | +2. Change to the new directory |
| 103 | + |
| 104 | +```console |
| 105 | +cd uname-app |
| 106 | +``` |
| 107 | + |
| 108 | +3. Modify the `hello_world/app.py` file to include the command `uname -m` |
| 109 | + |
| 110 | +Use a text editor to replace the contents of the `hello_world/app.py` file with the code below: |
| 111 | + |
| 112 | +```python |
| 113 | +import json |
| 114 | +import os |
| 115 | + |
| 116 | +def lambda_handler(event, context): |
| 117 | + """Sample pure Lambda function |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + event: dict, required |
| 122 | + API Gateway Lambda Proxy Input Format |
| 123 | +
|
| 124 | + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format |
| 125 | +
|
| 126 | + context: object, required |
| 127 | + Lambda Context runtime methods and attributes |
| 128 | +
|
| 129 | + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html |
| 130 | +
|
| 131 | + Returns |
| 132 | + ------ |
| 133 | + API Gateway Lambda Proxy Output Format: dict |
| 134 | +
|
| 135 | + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html |
| 136 | + """ |
| 137 | + |
| 138 | + ret = os.popen('uname -m').read() |
| 139 | + |
| 140 | + return { |
| 141 | + "statusCode": 200, |
| 142 | + "body": json.dumps({ |
| 143 | + "message": ret, |
| 144 | + # "location": ip.text.replace("\n", "") |
| 145 | + }), |
| 146 | + } |
| 147 | +``` |
| 148 | + |
| 149 | +4. Build the application |
| 150 | + |
| 151 | +```console |
| 152 | +sam build |
| 153 | +``` |
| 154 | + |
| 155 | +5. Test the deployed application: |
| 156 | + |
| 157 | +```console |
| 158 | +sam local invoke "HelloWorldFunction" -e events/event.json |
| 159 | +``` |
| 160 | + |
| 161 | +The output below shows the results from the command `uname -m` and the value of `aarch64` confirms an Arm Linux computer. |
| 162 | + |
| 163 | +```output |
| 164 | +Invoking app.lambda_handler (python3.12) |
| 165 | +Local image was not found. |
| 166 | +Removing rapid images for repo public.ecr.aws/sam/emulation-python3.12 |
| 167 | +Building image........................................................................................................................ |
| 168 | +Using local image: public.ecr.aws/lambda/python:3.12-rapid-arm64. |
| 169 | + |
| 170 | +Mounting /home/ubuntu/uname-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container |
| 171 | +START RequestId: 7221da4d-346d-4e2e-831e-dcde1cb47b5b Version: $LATEST |
| 172 | +END RequestId: 513dbd6f-7fc0-4212-ae13-a9a4ce2f21f4 |
| 173 | +REPORT RequestId: 513dbd6f-7fc0-4212-ae13-a9a4ce2f21f4 Init Duration: 0.26 ms Duration: 84.22 ms Billed Duration: 85 ms Memory Size: 128 MB Max Memory Used: 128 MB |
| 174 | +{"statusCode": 200, "body": "{\"message\": \"aarch64\\n\"}"} |
| 175 | +``` |
| 176 | + |
| 177 | +You are ready to use the AWS SAM CLI to build more complex functions and deploy them into AWS. Make sure to select `arm64` as the architecture for your Lambda functions. |
| 178 | + |
0 commit comments